forked from fernomac/ion-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skipper_test.go
178 lines (145 loc) · 4.54 KB
/
skipper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package ion
import (
"testing"
)
func TestSkipNumber(t *testing.T) {
test, testErr := testSkip(t, (*tokenizer).skipNumber)
test("", -1)
test("0", -1)
test("-1234567890,", ',')
test("1.2 ", ' ')
test("1d45\n", '\n')
test("1.4e-12//", '/')
testErr("1.2d3d", "ion: unexpected rune 'd' (offset 5)")
}
func TestSkipBinary(t *testing.T) {
test, testErr := testSkip(t, (*tokenizer).skipBinary)
test("0b0", -1)
test("-0b10 ", ' ')
test("0b010101,", ',')
testErr("0b2", "ion: unexpected rune '2' (offset 2)")
}
func TestSkipHex(t *testing.T) {
test, testErr := testSkip(t, (*tokenizer).skipHex)
test("0x0", -1)
test("-0x0F ", ' ')
test("0x1234567890abcdefABCDEF,", ',')
testErr("0x0G", "ion: unexpected rune 'G' (offset 3)")
}
func TestSkipTimestamp(t *testing.T) {
test, testErr := testSkip(t, (*tokenizer).skipTimestamp)
test("2001T", -1)
test("2001-01T,", ',')
test("2001-01-02}", '}')
test("2001-01-02T ", ' ')
test("2001-01-02T+00:00\t", '\t')
test("2001-01-02T-00:00\n", '\n')
test("2001-01-02T03:04+00:00 ", ' ')
test("2001-01-02T03:04-00:00 ", ' ')
test("2001-01-02T03:04Z ", ' ')
test("2001-01-02T03:04z ", ' ')
test("2001-01-02T03:04:05Z ", ' ')
test("2001-01-02T03:04:05+00:00 ", ' ')
test("2001-01-02T03:04:05.666Z ", ' ')
test("2001-01-02T03:04:05.666666z ", ' ')
testErr("", "ion: unexpected end of input (offset 0)")
testErr("2001", "ion: unexpected end of input (offset 4)")
testErr("2001z", "ion: unexpected rune 'z' (offset 4)")
testErr("20011", "ion: unexpected rune '1' (offset 4)")
testErr("2001-0", "ion: unexpected end of input (offset 6)")
testErr("2001-01", "ion: unexpected end of input (offset 7)")
testErr("2001-01-02Tz", "ion: unexpected rune 'z' (offset 11)")
testErr("2001-01-02T03", "ion: unexpected end of input (offset 13)")
testErr("2001-01-02T03z", "ion: unexpected rune 'z' (offset 13)")
testErr("2001-01-02T03:04x ", "ion: unexpected rune 'x' (offset 16)")
testErr("2001-01-02T03:04:05x ", "ion: unexpected rune 'x' (offset 19)")
}
func TestSkipSymbol(t *testing.T) {
test, _ := testSkip(t, (*tokenizer).skipSymbol)
test("f", -1)
test("foo:", ':')
test("foo,", ',')
test("foo ", ' ')
test("foo\n", '\n')
test("foo]", ']')
test("foo}", '}')
test("foo)", ')')
test("foo\\n", '\\')
}
func TestSkipSymbolQuoted(t *testing.T) {
test, testErr := testSkip(t, (*tokenizer).skipSymbolQuoted)
test("'", -1)
test("foo',", ',')
test("foo\\'bar':", ':')
test("foo\\\nbar',", ',')
testErr("foo", "ion: unexpected end of input (offset 3)")
testErr("foo\n", "ion: unexpected rune '\\n' (offset 3)")
}
func TestSkipSymbolOperator(t *testing.T) {
test, _ := testSkip(t, (*tokenizer).skipSymbolOperator)
test("+", -1)
test("++", -1)
test("+= ", ' ')
test("%b", 'b')
}
func TestSkipString(t *testing.T) {
test, testErr := testSkip(t, (*tokenizer).skipString)
test("\"", -1)
test("\",", ',')
test("foo\\\"bar\"], \"\"", ']')
test("foo\\\nbar\" \t\t\t", ' ')
testErr("foobar", "ion: unexpected end of input (offset 6)")
testErr("foobar\n", "ion: unexpected rune '\\n' (offset 6)")
}
func TestSkipLongString(t *testing.T) {
test, _ := testSkip(t, (*tokenizer).skipLongString)
test("'''", -1)
test("''',", ',')
test("abc''',", ',')
test("abc''' }", '}')
test("abc''' /*more*/ '''def'''\t//more\r\n]", ']')
}
func TestSkipBlob(t *testing.T) {
test, testErr := testSkip(t, (*tokenizer).skipBlob)
test("}}", -1)
test("oogboog}},{{}}", ',')
test("'''not encoded'''}}\n", '\n')
testErr("", "ion: unexpected end of input (offset 1)")
testErr("oogboog", "ion: unexpected end of input (offset 7)")
testErr("oogboog}", "ion: unexpected end of input (offset 8)")
testErr("oog}{boog", "ion: unexpected rune '{' (offset 4)")
}
func TestSkipList(t *testing.T) {
test, testErr := testSkip(t, (*tokenizer).skipList)
test("]", -1)
test("[]],", ',')
test("[123, \"]\", ']']] ", ' ')
testErr("abc, def, ", "ion: unexpected end of input (offset 10)")
}
type skipFunc func(*tokenizer) (int, error)
type skipTestFunc func(string, int)
type skipTestErrFunc func(string, string)
func testSkip(t *testing.T, f skipFunc) (skipTestFunc, skipTestErrFunc) {
test := func(str string, ec int) {
t.Run(str, func(t *testing.T) {
tok := tokenizeString(str)
c, err := f(tok)
if err != nil {
t.Fatal(err)
}
if c != ec {
t.Errorf("expected '%c', got '%c'", ec, c)
}
})
}
testErr := func(str string, e string) {
t.Run(str, func(t *testing.T) {
tok := tokenizeString(str)
_, err := f(tok)
if err == nil || err.Error() != e {
t.Errorf("expected err=%v, got err=%v", e, err)
}
})
}
return test, testErr
}