From 0965c9c3f80381de2afc51be58c8fabb4848caed Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 23 May 2026 21:03:44 +0300 Subject: test: add unit tests for Operations.Show() with diverse stack types Expand operations_stack_test.go from 1 test to 10 tests covering: - Empty stack returns 'Stack is empty' - Boolean values display as 'true'/'false' - Symbols display as ':name' (prefix with colon) - StringNum values display their string content - Mixed types (bool, symbol, string, number, metric) in one stack - Multiple metric values with different suffixes (Mbps, hr) - Rational numbers (Rat) display correctly - Rational booleans display as 'true'/'false' - Stack values order (bottom-to-top) - Existing metric suffix test (Cool vs non-Cool) --- internal/rpn/operations_stack_test.go | 214 ++++++++++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) diff --git a/internal/rpn/operations_stack_test.go b/internal/rpn/operations_stack_test.go index f31357b..7c0252c 100644 --- a/internal/rpn/operations_stack_test.go +++ b/internal/rpn/operations_stack_test.go @@ -36,3 +36,217 @@ func TestShowWithMetrics(t *testing.T) { t.Errorf("expected '5.5Mbps' in result, got: %s", result) } } + +func TestShowEmptyStack(t *testing.T) { + vars := NewVariables() + ops := NewOperations(vars) + stack := NewStack() + + result, err := ops.Show(stack) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if result != "Stack is empty" { + t.Errorf("Show(empty) = %q, want 'Stack is empty'", result) + } +} + +func TestShowWithBooleanValues(t *testing.T) { + vars := NewVariables() + ops := NewOperations(vars) + stack := NewStack() + + stack.Push(NewFloatFromBool(true)) + stack.Push(NewFloatFromBool(false)) + + result, err := ops.Show(stack) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if !strings.Contains(result, "true") { + t.Errorf("expected 'true' in result, got: %s", result) + } + if !strings.Contains(result, "false") { + t.Errorf("expected 'false' in result, got: %s", result) + } +} + +func TestShowWithSymbols(t *testing.T) { + vars := NewVariables() + ops := NewOperations(vars) + stack := NewStack() + + stack.Push(NewSymbol("x")) + stack.Push(NewSymbol("counter")) + stack.Push(NewFloat(42)) + + result, err := ops.Show(stack) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if !strings.Contains(result, ":x") { + t.Errorf("expected ':x' in result, got: %s", result) + } + if !strings.Contains(result, ":counter") { + t.Errorf("expected ':counter' in result, got: %s", result) + } + if !strings.Contains(result, "42") { + t.Errorf("expected '42' in result, got: %s", result) + } +} + +func TestShowWithStringNum(t *testing.T) { + vars := NewVariables() + ops := NewOperations(vars) + stack := NewStack() + + stack.Push(NewStringNum("hello")) + stack.Push(NewFloat(10)) + + result, err := ops.Show(stack) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if !strings.Contains(result, "hello") { + t.Errorf("expected 'hello' in result, got: %s", result) + } + if !strings.Contains(result, "10") { + t.Errorf("expected '10' in result, got: %s", result) + } +} + +func TestShowWithMixedTypes(t *testing.T) { + vars := NewVariables() + ops := NewOperations(vars) + stack := NewStack() + reg := GetMetricRegistry() + + mbps, _ := reg.Find("Mbps") + + // Push various types: number, bool, symbol, string, metric number + stack.Push(NewFloat(42)) + stack.Push(NewFloatFromBool(true)) + stack.Push(NewSymbol("x")) + stack.Push(NewStringNum("hello")) + stack.Push(NewFloatWithMetric(100, mbps)) + + result, err := ops.Show(stack) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + // Verify all types appear in output + if !strings.Contains(result, "42") { + t.Errorf("expected '42' in result, got: %s", result) + } + if !strings.Contains(result, "true") { + t.Errorf("expected 'true' in result, got: %s", result) + } + if !strings.Contains(result, ":x") { + t.Errorf("expected ':x' in result, got: %s", result) + } + if !strings.Contains(result, "hello") { + t.Errorf("expected 'hello' in result, got: %s", result) + } + if !strings.Contains(result, "100Mbps") { + t.Errorf("expected '100Mbps' in result, got: %s", result) + } +} + +func TestShowWithMultipleMetrics(t *testing.T) { + vars := NewVariables() + ops := NewOperations(vars) + stack := NewStack() + reg := GetMetricRegistry() + + mbps, _ := reg.Find("Mbps") + hrs, _ := reg.Find("hr") + + stack.Push(NewFloatWithMetric(100, mbps)) + stack.Push(NewFloatWithMetric(50, mbps)) + stack.Push(NewFloatWithMetric(2.5, hrs)) + + result, err := ops.Show(stack) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if !strings.Contains(result, "100Mbps") { + t.Errorf("expected '100Mbps' in result, got: %s", result) + } + if !strings.Contains(result, "50Mbps") { + t.Errorf("expected '50Mbps' in result, got: %s", result) + } + if !strings.Contains(result, "2.5hr") { + t.Errorf("expected '2.5hr' in result, got: %s", result) + } +} + +func TestShowWithRat(t *testing.T) { + vars := NewVariables() + ops := NewOperations(vars) + stack := NewStack() + + // Rational numbers + rat := NewRat(0.5) + stack.Push(rat) + stack.Push(NewFloat(10)) + + result, err := ops.Show(stack) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if !strings.Contains(result, "0.5") { + t.Errorf("expected '0.5' in result, got: %s", result) + } + if !strings.Contains(result, "10") { + t.Errorf("expected '10' in result, got: %s", result) + } +} + +func TestShowWithRatFromBool(t *testing.T) { + vars := NewVariables() + ops := NewOperations(vars) + stack := NewStack() + + stack.Push(NewRatFromBool(true)) + stack.Push(NewRatFromBool(false)) + + result, err := ops.Show(stack) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if !strings.Contains(result, "true") { + t.Errorf("expected 'true' in result, got: %s", result) + } + if !strings.Contains(result, "false") { + t.Errorf("expected 'false' in result, got: %s", result) + } +} + +func TestShowValuesOrder(t *testing.T) { + vars := NewVariables() + ops := NewOperations(vars) + stack := NewStack() + + // Push in order: 1, 2, 3 + stack.Push(NewFloat(1)) + stack.Push(NewFloat(2)) + stack.Push(NewFloat(3)) + + result, err := ops.Show(stack) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + // Show uses stack.Values() which is bottom-to-top, so "1 2 3" + expected := "1 2 3" + if result != expected { + t.Errorf("Show = %q, want %q", result, expected) + } +} -- cgit v1.2.3