-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner_test.go
105 lines (97 loc) · 2.05 KB
/
scanner_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
package hjson
import (
"bytes"
"testing"
)
func TestScanner(t *testing.T) {
type token struct {
tokenType int
literal string
}
testCases := []struct {
json string
tokens []token
}{
{
json: `[123, "token"]`,
tokens: []token{
{tokenLBracket, "["},
{tokenNumber, "123"},
{tokenComma, ","},
{tokenString, "token"},
{tokenRBracket, "]"},
{tokenEOF, ""},
},
},
{
json: `{"xx":"k\"ey", "value":[1, 2, true, null]}`,
tokens: []token{
{tokenLBrace, "{"},
{tokenString, "xx"},
{tokenColon, ":"},
{tokenString, `k\"ey`},
{tokenComma, ","},
{tokenString, "value"},
{tokenColon, ":"},
{tokenLBracket, "["},
{tokenNumber, "1"},
{tokenComma, ","},
{tokenNumber, "2"},
{tokenComma, ","},
{tokenTrue, "true"},
{tokenComma, ","},
{tokenNull, "null"},
{tokenRBracket, "]"},
{tokenRBrace, "}"},
{tokenEOF, ""},
},
},
{
json: `{"key":"\"\\ha\/\b\f\n\r\t"}`,
tokens: []token{
{tokenLBrace, "{"},
{tokenString, "key"},
{tokenColon, ":"},
{tokenString, `\"\\ha\/\b\f\n\r\t`},
{tokenRBrace, "}"},
},
},
{
json: `{"key":"\"hh"}`,
tokens: []token{
{tokenLBrace, "{"},
{tokenString, "key"},
{tokenColon, ":"},
{tokenString, `\"hh`},
{tokenRBrace, "}"},
},
},
{
json: `[12,12.12, 12e+1, 12e-1,12.12e+1]`,
tokens: []token{
{tokenLBracket, "["},
{tokenNumber, "12"},
{tokenComma, ","},
{tokenNumber, "12.12"},
{tokenComma, ","},
{tokenNumber, "12e+1"},
{tokenComma, ","},
{tokenNumber, "12e-1"},
{tokenComma, ","},
{tokenNumber, "12.12e+1"},
{tokenRBracket, "]"},
{tokenEOF, ""},
},
},
}
for i, tc := range testCases {
scanner := newScanner(bytes.NewBufferString(tc.json))
for _, token := range tc.tokens {
tok, literal := scanner.nextToken()
if token.tokenType != tok || token.literal != literal {
t.Fatalf("case:%d expect:<%s %s> got:<%s %s>\n", i, tokenTable[token.tokenType],
token.literal, tokenTable[tok], literal)
}
}
}
}