|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestSplitCommandAndArgs(t *testing.T) { |
| 10 | + var parseErrorContent = "error parsing command:" |
| 11 | + var noCommandErrContent = "no command contained in" |
| 12 | + |
| 13 | + tests := []struct { |
| 14 | + input string |
| 15 | + expectedCommand string |
| 16 | + expectedArgs []string |
| 17 | + expectedErrContent string |
| 18 | + }{ |
| 19 | + // Test case 0 - emtpy command |
| 20 | + { |
| 21 | + input: ``, |
| 22 | + expectedCommand: ``, |
| 23 | + expectedArgs: nil, |
| 24 | + expectedErrContent: noCommandErrContent, |
| 25 | + }, |
| 26 | + // Test case 1 - command without arguments |
| 27 | + { |
| 28 | + input: `command`, |
| 29 | + expectedCommand: `command`, |
| 30 | + expectedArgs: nil, |
| 31 | + expectedErrContent: ``, |
| 32 | + }, |
| 33 | + // Test case 2 - command with single argument |
| 34 | + { |
| 35 | + input: `command arg1`, |
| 36 | + expectedCommand: `command`, |
| 37 | + expectedArgs: []string{`arg1`}, |
| 38 | + expectedErrContent: ``, |
| 39 | + }, |
| 40 | + // Test case 3 - command with multiple arguments |
| 41 | + { |
| 42 | + input: `command arg1 arg2`, |
| 43 | + expectedCommand: `command`, |
| 44 | + expectedArgs: []string{`arg1`, `arg2`}, |
| 45 | + expectedErrContent: ``, |
| 46 | + }, |
| 47 | + // Test case 4 - command with single argument with space character - in quotes |
| 48 | + { |
| 49 | + input: `command "arg1 arg1"`, |
| 50 | + expectedCommand: `command`, |
| 51 | + expectedArgs: []string{`arg1 arg1`}, |
| 52 | + expectedErrContent: ``, |
| 53 | + }, |
| 54 | + // Test case 4 - command with single argument with space character - escaped |
| 55 | + { |
| 56 | + input: `command arg1\ arg1`, |
| 57 | + expectedCommand: `command`, |
| 58 | + expectedArgs: []string{`arg1 arg1`}, |
| 59 | + expectedErrContent: ``, |
| 60 | + }, |
| 61 | + // Test case 6 - command with escaped quote character |
| 62 | + { |
| 63 | + input: `command "arg1 \" arg1"`, |
| 64 | + expectedCommand: `command`, |
| 65 | + expectedArgs: []string{`arg1 " arg1`}, |
| 66 | + expectedErrContent: ``, |
| 67 | + }, |
| 68 | + // Test case 7 - command with escaped backslash |
| 69 | + { |
| 70 | + input: `command '\arg1'`, |
| 71 | + expectedCommand: `command`, |
| 72 | + expectedArgs: []string{`\arg1`}, |
| 73 | + expectedErrContent: ``, |
| 74 | + }, |
| 75 | + // Test case 8 - command with comments |
| 76 | + { |
| 77 | + input: `command arg1 #comment1 comment2`, |
| 78 | + expectedCommand: `command`, |
| 79 | + expectedArgs: []string{`arg1`}, |
| 80 | + expectedErrContent: "", |
| 81 | + }, |
| 82 | + // Test case 9 - command with multiple spaces and tab character |
| 83 | + { |
| 84 | + input: "command arg1 arg2\targ3", |
| 85 | + expectedCommand: `command`, |
| 86 | + expectedArgs: []string{`arg1`, `arg2`, "arg3"}, |
| 87 | + expectedErrContent: "", |
| 88 | + }, |
| 89 | + // Test case 10 - command with unclosed quotes |
| 90 | + { |
| 91 | + input: `command "arg1 arg2`, |
| 92 | + expectedCommand: "", |
| 93 | + expectedArgs: nil, |
| 94 | + expectedErrContent: parseErrorContent, |
| 95 | + }, |
| 96 | + // Test case 11 - command with unclosed quotes |
| 97 | + { |
| 98 | + input: `command 'arg1 arg2"`, |
| 99 | + expectedCommand: "", |
| 100 | + expectedArgs: nil, |
| 101 | + expectedErrContent: parseErrorContent, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for i, test := range tests { |
| 106 | + errorPrefix := fmt.Sprintf("Test [%d]: ", i) |
| 107 | + errorSuffix := fmt.Sprintf(" Command to parse: [%s]", test.input) |
| 108 | + actualCommand, actualArgs, actualErr := SplitCommandAndArgs(test.input) |
| 109 | + |
| 110 | + // test if error matches expectation |
| 111 | + if test.expectedErrContent != "" { |
| 112 | + if actualErr == nil { |
| 113 | + t.Errorf(errorPrefix+"Expected error with content [%s], found no error."+errorSuffix, test.expectedErrContent) |
| 114 | + } else if !strings.Contains(actualErr.Error(), test.expectedErrContent) { |
| 115 | + t.Errorf(errorPrefix+"Expected error with content [%s], found [%v]."+errorSuffix, test.expectedErrContent, actualErr) |
| 116 | + } |
| 117 | + } else if actualErr != nil { |
| 118 | + t.Errorf(errorPrefix+"Expected no error, found [%v]."+errorSuffix, actualErr) |
| 119 | + } |
| 120 | + |
| 121 | + // test if command matches |
| 122 | + if test.expectedCommand != actualCommand { |
| 123 | + t.Errorf("Expected command: [%s], actual: [%s]."+errorSuffix, test.expectedCommand, actualCommand) |
| 124 | + } |
| 125 | + |
| 126 | + // test if arguments match |
| 127 | + if len(test.expectedArgs) != len(actualArgs) { |
| 128 | + t.Errorf("Wrong number of arguments! Expected [%v], actual [%v]."+errorSuffix, test.expectedArgs, actualArgs) |
| 129 | + } |
| 130 | + |
| 131 | + for j, actualArg := range actualArgs { |
| 132 | + expectedArg := test.expectedArgs[j] |
| 133 | + if actualArg != expectedArg { |
| 134 | + t.Errorf(errorPrefix+"Argument at position [%d] differ! Expected [%s], actual [%s]"+errorSuffix, j, expectedArg, actualArg) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments