| 1 | package session |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestParseCommands(t *testing.T) { |
| 9 | //commands := ParseCommands("wifi.recon on; asdf; \"asdf;\" asdf") |
| 10 | t.Run("handles a semicolon as a delimiter", func(t *testing.T) { |
| 11 | first := "wifi.recon on" |
| 12 | second := "wifi.ap" |
| 13 | cmd := fmt.Sprintf("%s; %s", first, second) |
| 14 | commands := ParseCommands(cmd) |
| 15 | if l := len(commands); l != 2 { |
| 16 | t.Fatalf("Expected 2 commands, got %d", l) |
| 17 | } |
| 18 | if got := commands[0]; got != first { |
| 19 | t.Fatalf("expected %s got %s", first, got) |
| 20 | } |
| 21 | if got := commands[1]; got != second { |
| 22 | t.Fatalf("expected %s got %s", second, got) |
| 23 | } |
| 24 | }) |
| 25 | t.Run("handles semicolon inside quotes", func(t *testing.T) { |
| 26 | cmd := "set ticker.commands \"clear; net.show\"" |
| 27 | commands := ParseCommands(cmd) |
| 28 | if l := len(commands); l != 1 { |
| 29 | t.Fatalf("expected 1 command, got %d", l) |
| 30 | } |
| 31 | // Expect double-quotes stripped |
| 32 | expected := "set ticker.commands clear; net.show" |
| 33 | if got := commands[0]; got != expected { |
| 34 | fmt.Println(got) |
| 35 | t.Fatalf("expected %s got %s", cmd, got) |
| 36 | } |
| 37 | }) |
| 38 | t.Run("handles semicolon inside single quotes", func(t *testing.T) { |
| 39 | cmd := "set ticker.commands 'clear; net.show'" |
| 40 | commands := ParseCommands(cmd) |
| 41 | if l := len(commands); l != 1 { |
| 42 | t.Fatalf("expected 1 command, got %d", l) |
| 43 | } |
| 44 | // Expect double-quotes stripped |
| 45 | expected := "set ticker.commands clear; net.show" |
| 46 | if got := commands[0]; got != expected { |
| 47 | fmt.Println(got) |
| 48 | t.Fatalf("expected %s got %s", cmd, got) |
| 49 | } |
| 50 | }) |
| 51 | t.Run("handles semicolon inside single quotes inside quote", func(t *testing.T) { |
| 52 | cmd := "set ticker.commands \"'clear; net.show'\"" |
| 53 | commands := ParseCommands(cmd) |
| 54 | if l := len(commands); l != 1 { |
| 55 | t.Fatalf("expected 1 command, got %d", l) |
| 56 | } |
| 57 | // Expect double-quotes stripped |
| 58 | expected := "set ticker.commands 'clear; net.show'" |
| 59 | if got := commands[0]; got != expected { |
| 60 | fmt.Println(got) |
| 61 | t.Fatalf("expected %s got %s", cmd, got) |
| 62 | } |
| 63 | }) |
| 64 | t.Run("handles semicolon inside quotes inside single quote", func(t *testing.T) { |
| 65 | cmd := "set ticker.commands '\"clear; net.show\"'" |
| 66 | commands := ParseCommands(cmd) |
| 67 | if l := len(commands); l != 1 { |
| 68 | t.Fatalf("expected 1 command, got %d", l) |
| 69 | } |
| 70 | // Expect double-quotes stripped |
| 71 | expected := "set ticker.commands \"clear; net.show\"" |
| 72 | if got := commands[0]; got != expected { |
| 73 | fmt.Println(got) |
| 74 | t.Fatalf("expected %s got %s", cmd, got) |
| 75 | } |
| 76 | }) |
| 77 | t.Run("handle mismatching quote", func(t *testing.T) { |
| 78 | cmd := "set ticker.commands \"clear; echo it's working ?\"" |
| 79 | commands := ParseCommands(cmd) |
| 80 | if l := len(commands); l != 1 { |
| 81 | t.Fatalf("expected 1 command, got %d", l) |
| 82 | } |
| 83 | // Expect double-quotes stripped |
| 84 | expected := "set ticker.commands clear; echo it's working ?" |
| 85 | if got := commands[0]; got != expected { |
| 86 | fmt.Println(got) |
| 87 | t.Fatalf("expected %s got %s", cmd, got) |
| 88 | } |
| 89 | }) |
| 90 | } |