|
| 1 | +package parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestParseCliArguments(t *testing.T) { |
| 8 | + |
| 9 | + t.Run("Correctly gets commands", func(t *testing.T) { |
| 10 | + commands, _, _ := ParseCliArguments([]string{"test_command1", "test_command2"}) |
| 11 | + |
| 12 | + if len(commands) != 2 { |
| 13 | + t.Fatalf("Invalid command length. Expected %v got %v\n", 2, len(commands)) |
| 14 | + } |
| 15 | + |
| 16 | + if commands[0] != "test_command1" { |
| 17 | + t.Fatalf("Error in commands expected %v got %v\n", "test_command1", commands[0]) |
| 18 | + } |
| 19 | + |
| 20 | + if commands[1] != "test_command2" { |
| 21 | + t.Fatalf("Error in commands expected %v got %v\n", "test_command2", commands[1]) |
| 22 | + } |
| 23 | + }) |
| 24 | + |
| 25 | + t.Run("Correctly creates flags that have values", func(t *testing.T) { |
| 26 | + _, flags, _ := ParseCliArguments([]string{"test_command1", "--foo", "bar", "--baz", "foo"}) |
| 27 | + |
| 28 | + if len(flags) != 2 { |
| 29 | + t.Fatalf("Invalid flags length. Expected %v got %v\n", 2, len(flags)) |
| 30 | + } |
| 31 | + |
| 32 | + if flags["foo"] != "bar" { |
| 33 | + t.Fatalf("Error in flags expected %v got %v\n", "bar", flags["foo"]) |
| 34 | + } |
| 35 | + |
| 36 | + if flags["baz"] != "foo" { |
| 37 | + t.Fatalf("Error in flags expected %v got %v\n", "foo", flags["baz"]) |
| 38 | + } |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("Correctly creates flags that are booleans", func(t *testing.T) { |
| 42 | + _, flags, _ := ParseCliArguments([]string{"test_command1", "--foo", "--baz", "foo"}) |
| 43 | + |
| 44 | + if len(flags) != 2 { |
| 45 | + t.Fatalf("Invalid flags length. Expected %v got %v\n", 2, len(flags)) |
| 46 | + } |
| 47 | + |
| 48 | + if flags["foo"] != true { |
| 49 | + t.Fatalf("Error in flags expected %v got %v\n", true, flags["foo"]) |
| 50 | + } |
| 51 | + |
| 52 | + if flags["baz"] != "foo" { |
| 53 | + t.Fatalf("Error in flags expected %v got %v\n", "foo", flags["baz"]) |
| 54 | + } |
| 55 | + }) |
| 56 | + t.Run("Correctly errors if you enter too many values for a given flag", func(t *testing.T) { |
| 57 | + _, _, err := ParseCliArguments([]string{"test_command1", "--foo", "value1", "value2", "--bar"}) |
| 58 | + |
| 59 | + if err == nil { |
| 60 | + t.Fatalf("Error was expected but not received") |
| 61 | + } |
| 62 | + |
| 63 | + if err.Error() != "invalid number of parameters. Unexpected value value2" { |
| 64 | + t.Fatalf("Error message incorrect. Expected \"%v\" got \"%v\"", "invalid number of parameters. Unexpected value value2", err.Error()) |
| 65 | + } |
| 66 | + }) |
| 67 | + t.Run("Correctly errors if you try to pass in the same flag twice", func(t *testing.T) { |
| 68 | + _, _, err := ParseCliArguments([]string{"test_command1", "--foo", "value1", "--foo", "--bar"}) |
| 69 | + |
| 70 | + if err == nil { |
| 71 | + t.Fatalf("Error was expected but not received") |
| 72 | + } |
| 73 | + |
| 74 | + if err.Error() != "flag foo was set multiple times" { |
| 75 | + t.Fatalf("Error message incorrect. Expected \"%v\" got \"%v\"", "flag foo was set multiple times", err.Error()) |
| 76 | + } |
| 77 | + }) |
| 78 | + t.Run("Correctly errors if you try to pass in the same flag twice, duplicate flag is the last value", func(t *testing.T) { |
| 79 | + _, _, err := ParseCliArguments([]string{"test_command1", "--foo", "value1", "--bar", "--foo"}) |
| 80 | + |
| 81 | + if err == nil { |
| 82 | + t.Fatalf("Error was expected but not received") |
| 83 | + } |
| 84 | + |
| 85 | + if err.Error() != "flag foo was set multiple times" { |
| 86 | + t.Fatalf("Error message incorrect. Expected \"%v\" got \"%v\"", "flag foo was set multiple times", err.Error()) |
| 87 | + } |
| 88 | + }) |
| 89 | + t.Run("Correctly errors if you pass in empty -", func(t *testing.T) { |
| 90 | + _, _, err := ParseCliArguments([]string{"test_command1", "--", "value1", "--bar", "--foo"}) |
| 91 | + |
| 92 | + if err == nil { |
| 93 | + t.Fatalf("Error was expected but not received") |
| 94 | + } |
| 95 | + |
| 96 | + if err.Error() != "empty flag was passed in" { |
| 97 | + t.Fatalf("Error message incorrect. Expected \"%v\" got \"%v\"", "empty flag was passed in", err.Error()) |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | +} |
0 commit comments