|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+package tokenize
|
|
|
2
|
+
|
|
|
3
|
+import "fmt"
|
|
|
4
|
+import "testing"
|
|
|
5
|
+
|
|
|
6
|
+func Test_tokenize(t *testing.T) {
|
|
|
7
|
+ var test_cases = []struct {
|
|
|
8
|
+ test_str string
|
|
|
9
|
+ want_result tokenizeResult
|
|
|
10
|
+ }{
|
|
|
11
|
+
|
|
|
12
|
+ // emptiness
|
|
|
13
|
+ {"", tokenizeResult{tokens: []string{}}},
|
|
|
14
|
+
|
|
|
15
|
+ // unquoted whitespace
|
|
|
16
|
+ {" ", tokenizeResult{tokens: []string{}}},
|
|
|
17
|
+ {"\t", tokenizeResult{tokens: []string{}}},
|
|
|
18
|
+ {"\n", tokenizeResult{tokens: []string{}}},
|
|
|
19
|
+ {" \t\n", tokenizeResult{tokens: []string{}}},
|
|
|
20
|
+ {" \tfoo", tokenizeResult{tokens: []string{"foo"}}},
|
|
|
21
|
+ {"foo ", tokenizeResult{tokens: []string{"foo"}}},
|
|
|
22
|
+ {"foo bar", tokenizeResult{tokens: []string{"foo", "bar"}}},
|
|
|
23
|
+ {"foo\nbar", tokenizeResult{tokens: []string{"foo", "bar"}}},
|
|
|
24
|
+ {"foo\tbar", tokenizeResult{tokens: []string{"foo", "bar"}}},
|
|
|
25
|
+
|
|
|
26
|
+ // single quotes
|
|
|
27
|
+ {"'", tokenizeResult{tokens: []string{}, code: tokenizeResultCodeMissingEndSingleQuote}},
|
|
|
28
|
+ {"''", tokenizeResult{tokens: []string{""}}},
|
|
|
29
|
+ {"fo''o", tokenizeResult{tokens: []string{"foo"}}},
|
|
|
30
|
+ {"foo '' bar", tokenizeResult{tokens: []string{"foo", "", "bar"}}},
|
|
|
31
|
+ {"foo 'and' bar", tokenizeResult{tokens: []string{"foo", "and", "bar"}}},
|
|
|
32
|
+ {"foo '\\\t\n' bar", tokenizeResult{tokens: []string{"foo", "\\\t\n", "bar"}}},
|
|
|
33
|
+ {"foo ' space bar '", tokenizeResult{tokens: []string{"foo", " space bar "}}},
|
|
|
34
|
+ {"foo 'John \"Spaceman\" Doe'", tokenizeResult{tokens: []string{"foo", "John \"Spaceman\" Doe"}}},
|
|
|
35
|
+
|
|
|
36
|
+ //
|
|
|
37
|
+ }
|
|
|
38
|
+ for _, tc := range test_cases {
|
|
|
39
|
+ t.Run(fmt.Sprintf("%q", tc.test_str), func(t *testing.T) {
|
|
|
40
|
+ have_result := tokenize(tc.test_str)
|
|
|
41
|
+ if have_result.code != tc.want_result.code {
|
|
|
42
|
+ t.Errorf("unexpected result .code: got %s, want %s in %v", have_result.code, tc.want_result.code, have_result)
|
|
|
43
|
+ return
|
|
|
44
|
+ }
|
|
|
45
|
+ if have_result.err_loc != tc.want_result.err_loc {
|
|
|
46
|
+ t.Errorf("unexpected result .err_loc: got %d, want %d in %v", have_result.err_loc, tc.want_result.err_loc, have_result)
|
|
|
47
|
+ return
|
|
|
48
|
+ }
|
|
|
49
|
+ if len(have_result.tokens) != len(tc.want_result.tokens) {
|
|
|
50
|
+ t.Errorf("unexpected number of result .tokens: got %d, want %d in %v", len(have_result.tokens), len(tc.want_result.tokens), have_result)
|
|
|
51
|
+ return
|
|
|
52
|
+ }
|
|
|
53
|
+ for i := range have_result.tokens {
|
|
|
54
|
+ if have_result.tokens[i] == tc.want_result.tokens[i] {
|
|
|
55
|
+ continue
|
|
|
56
|
+ }
|
|
|
57
|
+ 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)
|
|
|
58
|
+ return
|
|
|
59
|
+ }
|
|
|
60
|
+ })
|
|
|
61
|
+ }
|
|
|
62
|
+}
|
|
|
63
|
+
|
|
|
64
|
+func Test_reader_tossUntilNeitherOf(t *testing.T) {
|
|
|
65
|
+ var test_cases = []struct {
|
|
|
66
|
+ test_data string
|
|
|
67
|
+ test_needles string
|
|
|
68
|
+ test_startpos uint
|
|
|
69
|
+ want_endpos uint
|
|
|
70
|
+ want_ok bool
|
|
|
71
|
+ }{
|
|
|
72
|
+ {"", "", 0, 0, false},
|
|
|
73
|
+ {"", "x", 0, 0, false},
|
|
|
74
|
+ {"", "xy", 0, 0, false},
|
|
|
75
|
+ {"x", "", 0, 0, false},
|
|
|
76
|
+ {"x", "x", 0, 1, true},
|
|
|
77
|
+ {"x", "xy", 0, 1, true},
|
|
|
78
|
+ {"x", "yx", 0, 1, true},
|
|
|
79
|
+ {"xa", "x", 0, 1, true},
|
|
|
80
|
+ {"xa", "xy", 0, 1, true},
|
|
|
81
|
+ {"xa", "yx", 0, 1, true},
|
|
|
82
|
+ {"xya", "x", 0, 1, true},
|
|
|
83
|
+ {"xya", "xy", 0, 2, true},
|
|
|
84
|
+ {"xya", "yx", 0, 2, true},
|
|
|
85
|
+ {"xxya", "x", 1, 2, true},
|
|
|
86
|
+ {"xxya", "xy", 1, 3, true},
|
|
|
87
|
+ {"xxya", "yx", 1, 3, true},
|
|
|
88
|
+ }
|
|
|
89
|
+ for _, tc := range test_cases {
|
|
|
90
|
+ t.Run(fmt.Sprintf("%q[%d:]-%q", tc.test_data, tc.test_startpos, tc.test_needles), func(t *testing.T) {
|
|
|
91
|
+ test_reader := reader{data: tc.test_data}
|
|
|
92
|
+ test_reader.pos = tc.test_startpos
|
|
|
93
|
+ have_ok := test_reader.tossUntilNeitherOf(tc.test_needles)
|
|
|
94
|
+ if test_reader.pos != tc.want_endpos {
|
|
|
95
|
+ t.Errorf("unexpected position after toss: got %d, want %d", test_reader.pos, tc.want_endpos)
|
|
|
96
|
+ return
|
|
|
97
|
+ }
|
|
|
98
|
+ if have_ok != tc.want_ok {
|
|
|
99
|
+ t.Errorf("unexpected ok: got %v, want %v", have_ok, tc.want_ok)
|
|
|
100
|
+ return
|
|
|
101
|
+ }
|
|
|
102
|
+ })
|
|
|
103
|
+ }
|
|
|
104
|
+}
|