summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-22 13:18:17 +0300
committerPaul Buetow <paul@buetow.org>2026-05-22 13:18:17 +0300
commit8ec573ecce84114dfe3887f95cef4383312ac791 (patch)
treed8b4b65e462631a15729a3f97bd5252e8e01cc45
parentdb1d8bf0e3a158178f875d926a080c5b2273ea39 (diff)
feat(rpn): show metric suffix in stack display
Task 9d: Operations.Show() now appends metric suffix for non-Cool metrics (e.g., '100Mbps 42 5.5Mbps'). Cool numbers display as plain numbers. Added TestShowWithMetrics.
-rw-r--r--internal/rpn/operations_stack.go13
-rw-r--r--internal/rpn/operations_stack_test.go38
2 files changed, 47 insertions, 4 deletions
diff --git a/internal/rpn/operations_stack.go b/internal/rpn/operations_stack.go
index 2505e37..e8752a6 100644
--- a/internal/rpn/operations_stack.go
+++ b/internal/rpn/operations_stack.go
@@ -52,6 +52,8 @@ func (o *Operations) Pop(stack *Stack) error {
}
// Show returns the current stack as a formatted string using the Number interface.
+// Numbers with non-Cool metrics display with the metric suffix (e.g., "100Mbps").
+// Cool numbers display as plain numbers. Booleans show as "true"/"false".
func (o *Operations) Show(stack *Stack) (string, error) {
if stack.Len() == 0 {
return "Stack is empty", nil
@@ -63,10 +65,13 @@ func (o *Operations) Show(stack *Stack) (string, error) {
if i > 0 {
result += " "
}
- // Use val.String() to format values correctly:
- // - Boolean values show as "true"/"false"
- // - Number values show with appropriate precision
- result += val.String()
+ // Append metric suffix for non-Cool metrics
+ m := val.Metric()
+ if m != nil && m.Category != Universal {
+ result += val.String() + m.Name
+ } else {
+ result += val.String()
+ }
}
return result, nil
}
diff --git a/internal/rpn/operations_stack_test.go b/internal/rpn/operations_stack_test.go
new file mode 100644
index 0000000..f31357b
--- /dev/null
+++ b/internal/rpn/operations_stack_test.go
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 Paul Buetow
+
+package rpn
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestShowWithMetrics(t *testing.T) {
+ reg := GetMetricRegistry()
+ vars := NewVariables()
+ ops := NewOperations(vars)
+ stack := NewStack()
+
+ mbps, _ := reg.Find("Mbps")
+ cool, _ := reg.Find("Cool")
+
+ stack.Push(NewFloatWithMetric(100, mbps))
+ stack.Push(NewFloatWithMetric(42, cool))
+ stack.Push(NewFloatWithMetric(5.5, mbps))
+
+ result, err := ops.Show(stack)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ // Non-Cool shows metric suffix, Cool doesn't
+ if !strings.Contains(result, "100Mbps") {
+ t.Errorf("expected '100Mbps' in result, got: %s", result)
+ }
+ if !strings.Contains(result, "42") {
+ t.Errorf("expected '42' (plain) in result, got: %s", result)
+ }
+ if !strings.Contains(result, "5.5Mbps") {
+ t.Errorf("expected '5.5Mbps' in result, got: %s", result)
+ }
+}