|
|
@@ -39,7 +39,7 @@ func Test_tokenize(t *testing.T) {
|
|
39
|
39
|
{"fo\"\"o", tokenizeResult{tokens: []string{"foo"}}},
|
|
40
|
40
|
{"foo \"\" bar", tokenizeResult{tokens: []string{"foo", "", "bar"}}},
|
|
41
|
41
|
{"foo \"and\" bar", tokenizeResult{tokens: []string{"foo", "and", "bar"}}},
|
|
42
|
|
- {"foo \"\\\t\n\" bar", tokenizeResult{tokens: []string{"foo", "\\\t\n", "bar"}}},
|
|
|
42
|
+ {"foo \"\\\\\t\n\" bar", tokenizeResult{tokens: []string{"foo", "\\\t\n", "bar"}}},
|
|
43
|
43
|
{"foo \" space bar \"", tokenizeResult{tokens: []string{"foo", " space bar "}}},
|
|
44
|
44
|
{"foo \"Joe's lunch\"", tokenizeResult{tokens: []string{"foo", "Joe's lunch"}}},
|
|
45
|
45
|
|
|
|
@@ -58,6 +58,19 @@ func Test_tokenize(t *testing.T) {
|
|
58
|
58
|
{"foo \\\\ bar", tokenizeResult{tokens: []string{"foo", "\\", "bar"}}},
|
|
59
|
59
|
{"foo \\'bar\\' baz", tokenizeResult{tokens: []string{"foo", "'bar'", "baz"}}},
|
|
60
|
60
|
|
|
|
61
|
+ // backslash within double quotes
|
|
|
62
|
+ {"\"\\", tokenizeResult{tokens: []string{}, code: tokenizeResultCodeMissingEscapedCharacter}},
|
|
|
63
|
+ {"\"\\\"", tokenizeResult{tokens: []string{}, code: tokenizeResultCodeMissingEndDoubleQuote, err_loc: 2}},
|
|
|
64
|
+ {"\" \\\" \"", tokenizeResult{tokens: []string{" \" "}}},
|
|
|
65
|
+ {"\" \\\\ \"", tokenizeResult{tokens: []string{" \\ "}}},
|
|
|
66
|
+ {"\" \\x \"", tokenizeResult{tokens: []string{" \\x "}}},
|
|
|
67
|
+ {"fo\"o\\\\b\"ar", tokenizeResult{tokens: []string{"foo\\bar"}}},
|
|
|
68
|
+ {"fo\"o \\\\ \"bar\" b\"az", tokenizeResult{tokens: []string{"foo \\ bar baz"}}},
|
|
|
69
|
+
|
|
|
70
|
+ // CC cases
|
|
|
71
|
+ {"cat \"/tmp/fox/'f 2'\" \"/tmp/fox/'f \\73'\" \"/tmp/fox/'f \\21\\'\"",
|
|
|
72
|
+ tokenizeResult{tokens: []string{"cat", "/tmp/fox/'f 2'", "/tmp/fox/'f \\73'", "/tmp/fox/'f \\21\\'"}},
|
|
|
73
|
+ },
|
|
61
|
74
|
//
|
|
62
|
75
|
}
|
|
63
|
76
|
for _, tc := range test_cases {
|
|
|
@@ -80,6 +93,9 @@ func Test_tokenize(t *testing.T) {
|
|
80
|
93
|
continue
|
|
81
|
94
|
}
|
|
82
|
95
|
t.Errorf("unexpected token in result .tokens[%d]: got %q, want %q in %v", i, have_result.tokens[i], tc.want_result.tokens[i], have_result)
|
|
|
96
|
+ t.Errorf(" .. test_str: ⟅%s⟆", tc.test_str)
|
|
|
97
|
+ t.Errorf(" .. got: ⟅%s⟆", have_result.tokens[i])
|
|
|
98
|
+ t.Errorf(" .. want: ⟅%s⟆", tc.want_result.tokens[i])
|
|
83
|
99
|
return
|
|
84
|
100
|
}
|
|
85
|
101
|
})
|
|
|
@@ -127,3 +143,50 @@ func Test_reader_tossUntilNeitherOf(t *testing.T) {
|
|
127
|
143
|
})
|
|
128
|
144
|
}
|
|
129
|
145
|
}
|
|
|
146
|
+
|
|
|
147
|
+func Test_reader_takeUntilAnyOf(t *testing.T) {
|
|
|
148
|
+ var test_cases = []struct {
|
|
|
149
|
+ test_startpos uint
|
|
|
150
|
+ test_data string
|
|
|
151
|
+ test_needles string
|
|
|
152
|
+ want_endpos uint
|
|
|
153
|
+ want_ok bool
|
|
|
154
|
+ }{
|
|
|
155
|
+ {0, "", "", 0, false},
|
|
|
156
|
+ {0, "", "x", 0, false},
|
|
|
157
|
+ {0, "", "xy", 0, false},
|
|
|
158
|
+ {0, "x", "", 1, true},
|
|
|
159
|
+ {0, "x", "x", 0, false},
|
|
|
160
|
+ {0, "x", "xy", 0, false},
|
|
|
161
|
+ {0, "x", "yx", 0, false},
|
|
|
162
|
+ {0, "xa", "x", 0, false},
|
|
|
163
|
+ {0, "xa", "xy", 0, false},
|
|
|
164
|
+ {0, "xa", "yx", 0, false},
|
|
|
165
|
+ {0, "xya", "x", 0, false},
|
|
|
166
|
+ {0, "xya", "xy", 0, false},
|
|
|
167
|
+ {0, "xya", "yx", 0, false},
|
|
|
168
|
+ {0, "ax", "x", 1, true},
|
|
|
169
|
+ {0, "ax", "xy", 1, true},
|
|
|
170
|
+ {0, "ax", "yx", 1, true},
|
|
|
171
|
+ {0, "axy", "x", 1, true},
|
|
|
172
|
+ {0, "axy", "xy", 1, true},
|
|
|
173
|
+ {0, "axy", "yx", 1, true},
|
|
|
174
|
+ {0, "abxa", "x", 2, true},
|
|
|
175
|
+ {0, "aboa", "x", 4, true},
|
|
|
176
|
+ }
|
|
|
177
|
+ for _, tc := range test_cases {
|
|
|
178
|
+ t.Run(fmt.Sprintf("%q[%d:]-%q", tc.test_data, tc.test_startpos, tc.test_needles), func(t *testing.T) {
|
|
|
179
|
+ test_reader := reader{data: tc.test_data}
|
|
|
180
|
+ test_reader.pos = tc.test_startpos
|
|
|
181
|
+ _, have_ok := test_reader.takeUntilAnyOf(tc.test_needles)
|
|
|
182
|
+ if test_reader.pos != tc.want_endpos {
|
|
|
183
|
+ t.Errorf("unexpected position after take: got %d, want %d", test_reader.pos, tc.want_endpos)
|
|
|
184
|
+ return
|
|
|
185
|
+ }
|
|
|
186
|
+ if have_ok != tc.want_ok {
|
|
|
187
|
+ t.Errorf("unexpected ok: got %v, want %v", have_ok, tc.want_ok)
|
|
|
188
|
+ return
|
|
|
189
|
+ }
|
|
|
190
|
+ })
|
|
|
191
|
+ }
|
|
|
192
|
+}
|