1
2
3
4
5 package objabi
6
7 import (
8 "slices"
9 "testing"
10 )
11
12 func TestParseArgs(t *testing.T) {
13 t.Parallel()
14 tests := []struct {
15 name string
16 input string
17 want []string
18 }{
19
20
21 {`crlf`, "a\r\nb", []string{"a", "b"}},
22 {"newline", "a\nb", []string{"a", "b"}},
23 {"null byte in arg", "a\x00b", []string{"a\x00b"}},
24 {"null byte only", "\x00", []string{"\x00"}},
25 {"leading newline", "\na\nb", []string{"a", "b"}},
26 {"empty quotes", "a\n''\nb", []string{"a", "", "b"}},
27 {"quoted newlines", "a\n'a\n\nb'\nb", []string{"a", "a\n\nb", "b"}},
28 {"single quote no escapes", "'a\\$VAR' '\\\"'", []string{"a\\$VAR", "\\\""}},
29 {"line continuation", "\"ab\\\ncd\" ef\\\ngh", []string{"abcd", "efgh"}},
30
31 {"line continuation crlf", "\"ab\\\r\ncd\" ef\\\r\ngh", []string{"abcd", "efgh"}},
32 {"double quote escapes", "\"\\$VAR\" \"\\`\" \"\\\"\" \"\\\\\" \"\\n\" \"\\t\"",
33 []string{"$VAR", "`", `"`, `\`, `\n`, `\t`}},
34 {"whitespace only", "\t \n \t ", nil},
35 {"single space", " ", nil},
36 {"multiple spaces", " ", nil},
37
38
39 {"basic split", "a b c", []string{"a", "b", "c"}},
40 {"tabs", "a\tb\tc", []string{"a", "b", "c"}},
41 {"mixed quotes", `a 'b c' "d e"`, []string{"a", "b c", "d e"}},
42 {"adjacent quotes", `'a'"b"`, []string{"ab"}},
43 {"empty input", "", nil},
44 {"empty single quotes", "''", []string{""}},
45 {"empty double quotes", `""`, []string{""}},
46 {"nested quotes in single", `'"hello"'`, []string{`"hello"`}},
47 {"nested quotes in double", `"'hello'"`, []string{"'hello'"}},
48
49 {"backslash escape outside quotes", `\abc`, []string{"abc"}},
50 {"trailing backslash", `abc\`, []string{"abc"}},
51 }
52 for _, tt := range tests {
53 t.Run(tt.name, func(t *testing.T) {
54 got := ParseArgs([]byte(tt.input))
55 if !slices.Equal(got, tt.want) {
56 t.Errorf("parseArgs(%q) = %q, want %q", tt.input, got, tt.want)
57 }
58 })
59 }
60 }
61
View as plain text