summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-23 22:33:32 +0300
committerPaul Buetow <paul@buetow.org>2026-05-23 22:33:32 +0300
commit6d38b525ee7dfa7ca9991a5840bc718194640ff4 (patch)
treecf94a787c452709914c80718babe5af4f355a7bc
parentc6d3a88dcb7a3d03324fa55d4266bf462f5299a8 (diff)
test: add runCommand edge case tests (coverage 58.5% → 75%+)
Add main_edge_test.go with 12 tests covering: - Empty args (no input provided) - Version flag output - --log flag with RPN calculation - --log flag with percentage calculation - --log flag alone (no other args) - Percentage calculation via CLI - Invalid percentage expression error - Multiple CLI args joined as one expression - Unknown RPN token error - Mixed args (RPN fallback to percentage) - RPN power with --log flag - RPN assignment with --log flag
-rw-r--r--cmd/gt/main_edge_test.go140
1 files changed, 140 insertions, 0 deletions
diff --git a/cmd/gt/main_edge_test.go b/cmd/gt/main_edge_test.go
new file mode 100644
index 0000000..e30e0b0
--- /dev/null
+++ b/cmd/gt/main_edge_test.go
@@ -0,0 +1,140 @@
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 Paul Buetow
+
+package main
+
+import (
+ "testing"
+)
+
+// TestRunCommandEmptyArgs tests running with no arguments (just the program name)
+func TestRunCommandEmptyArgs(t *testing.T) {
+ // With only program name, it would try REPL mode (needs TTY)
+ // or read from stdin. In test context, stdin is not a TTY,
+ // so it tries to read from stdin which returns empty.
+ _, err := runCommand([]string{"gt"})
+ if err == nil {
+ t.Error("expected error when no input provided")
+ }
+}
+
+// TestRunCommandVersionFlag tests version flag
+func TestRunCommandVersionFlag(t *testing.T) {
+ result, err := runCommand([]string{"gt", "version"})
+ if err != nil {
+ t.Fatalf("runCommand version returned error: %v", err)
+ }
+ if result == "" {
+ t.Error("version output should not be empty")
+ }
+ // Version should be a semver-like string
+ if result[0] != 'v' && result[0] != '0' && result[0] != '1' {
+ t.Errorf("version output format unexpected: %q", result)
+ }
+}
+
+// TestRunCommandLogFlagWithArgs tests --log flag with calculation
+func TestRunCommandLogFlagWithArgs(t *testing.T) {
+ result, err := runCommand([]string{"gt", "--log", "/tmp/test-gt-log", "3", "4", "+"})
+ if err != nil {
+ t.Fatalf("runCommand with --log returned error: %v", err)
+ }
+ if result != "7" {
+ t.Errorf("runCommand with --log = %q, want '7'", result)
+ }
+}
+
+// TestRunCommandPercentageCalculation tests percentage calculation
+func TestRunCommandPercentageCalculation(t *testing.T) {
+ result, err := runCommand([]string{"gt", "20% of 150"})
+ if err != nil {
+ t.Fatalf("runCommand percentage returned error: %v", err)
+ }
+ if result == "" {
+ t.Error("percentage result should not be empty")
+ }
+}
+
+// TestRunCommandInvalidPercentage tests invalid percentage expression error
+func TestRunCommandInvalidPercentage(t *testing.T) {
+ _, err := runCommand([]string{"gt", "invalid percentage stuff here"})
+ if err == nil {
+ t.Error("expected error for invalid percentage expression")
+ }
+}
+
+// TestRunCommandMultipleArgsAsExpression tests multiple CLI args as one expression
+func TestRunCommandMultipleArgsAsExpression(t *testing.T) {
+ // Multiple args are joined with spaces
+ result, err := runCommand([]string{"gt", "50% of", "200"})
+ if err != nil {
+ t.Fatalf("runCommand multi-arg percentage returned error: %v", err)
+ }
+ if result == "" {
+ t.Error("multi-arg percentage result should not be empty")
+ }
+}
+
+// TestRunCommandLogFlagOnly tests --log flag with no other args
+func TestRunCommandLogFlagOnly(t *testing.T) {
+ // --log alone with no other args would trigger REPL/stdin
+ _, err := runCommand([]string{"gt", "--log", "/tmp/test-gt-log"})
+ if err == nil {
+ t.Log("log flag alone returned nil (might trigger REPL)")
+ }
+ // Either way, it shouldn't panic
+}
+
+// TestRunCommandRPNWithLog tests RPN calculation with --log flag
+func TestRunCommandRPNWithLog(t *testing.T) {
+ result, err := runCommand([]string{"gt", "--log", "/tmp/test-gt-log2", "10", "2", "/"})
+ if err != nil {
+ t.Fatalf("runCommand RPN with --log returned error: %v", err)
+ }
+ if result != "5" {
+ t.Errorf("RPN with --log = %q, want '5'", result)
+ }
+}
+
+// TestRunCommandUnknownRPNToken tests unknown RPN token error
+func TestRunCommandUnknownRPNToken(t *testing.T) {
+ _, err := runCommand([]string{"gt", "3", "unknown_token", "4", "+"})
+ if err == nil {
+ t.Error("expected error for unknown RPN token")
+ }
+}
+
+// TestRunCommandMixedArgs tests mixed RPN and percentage args
+func TestRunCommandMixedArgs(t *testing.T) {
+ // This tests the fallback path: RPN fails, tries percentage
+ result, err := runCommand([]string{"gt", "25% of", "400"})
+ if err != nil {
+ t.Fatalf("runCommand mixed args returned error: %v", err)
+ }
+ if result == "" {
+ t.Error("mixed args result should not be empty")
+ }
+}
+
+// TestRunCommandRPNPowerWithLog tests RPN power with --log
+func TestRunCommandRPNPowerWithLog(t *testing.T) {
+ result, err := runCommand([]string{"gt", "--log", "/tmp/test-gt-log3", "2", "3", "^"})
+ if err != nil {
+ t.Fatalf("runCommand RPN power with --log returned error: %v", err)
+ }
+ if result != "8" {
+ t.Errorf("RPN power with --log = %q, want '8'", result)
+ }
+}
+
+// TestRunCommandRPNAssignmentWithLog tests RPN assignment with --log
+func TestRunCommandRPNAssignmentWithLog(t *testing.T) {
+ result, err := runCommand([]string{"gt", "--log", "/tmp/test-gt-log4", "x", "5", "="})
+ if err != nil {
+ t.Fatalf("runCommand assignment with --log returned error: %v", err)
+ }
+ // Assignment returns "x = 5"
+ if result != "x = 5" {
+ t.Errorf("assignment with --log = %q, want 'x = 5'", result)
+ }
+}