8 "github.com/hashicorp/hcl/json/token"
11 var f100 = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
13 type tokenPair struct {
18 var tokenLists = map[string][]tokenPair{
19 "operator": []tokenPair{
29 {token.BOOL, "false"},
31 "string": []tokenPair{
32 {token.STRING, `" "`},
33 {token.STRING, `"a"`},
34 {token.STRING, `"本"`},
35 {token.STRING, `"${file(\"foo\")}"`},
36 {token.STRING, `"\a"`},
37 {token.STRING, `"\b"`},
38 {token.STRING, `"\f"`},
39 {token.STRING, `"\n"`},
40 {token.STRING, `"\r"`},
41 {token.STRING, `"\t"`},
42 {token.STRING, `"\v"`},
43 {token.STRING, `"\""`},
44 {token.STRING, `"\000"`},
45 {token.STRING, `"\777"`},
46 {token.STRING, `"\x00"`},
47 {token.STRING, `"\xff"`},
48 {token.STRING, `"\u0000"`},
49 {token.STRING, `"\ufA16"`},
50 {token.STRING, `"\U00000000"`},
51 {token.STRING, `"\U0000ffAB"`},
52 {token.STRING, `"` + f100 + `"`},
54 "number": []tokenPair{
59 {token.NUMBER, "1234567890"},
63 {token.NUMBER, "-42"},
64 {token.NUMBER, "-1234567890"},
70 {token.FLOAT, "01234567890."},
74 {token.FLOAT, ".0123456789"},
77 {token.FLOAT, "42.0"},
78 {token.FLOAT, "01234567890.0"},
81 {token.FLOAT, "42e0"},
82 {token.FLOAT, "01234567890e0"},
85 {token.FLOAT, "42E0"},
86 {token.FLOAT, "01234567890E0"},
87 {token.FLOAT, "0e+10"},
88 {token.FLOAT, "1e-10"},
89 {token.FLOAT, "42e+10"},
90 {token.FLOAT, "01234567890e-10"},
91 {token.FLOAT, "0E+10"},
92 {token.FLOAT, "1E-10"},
93 {token.FLOAT, "42E+10"},
94 {token.FLOAT, "01234567890E-10"},
95 {token.FLOAT, "01.8e0"},
96 {token.FLOAT, "1.4e0"},
97 {token.FLOAT, "42.2e0"},
98 {token.FLOAT, "01234567890.12e0"},
99 {token.FLOAT, "0.E0"},
100 {token.FLOAT, "1.12E0"},
101 {token.FLOAT, "42.123E0"},
102 {token.FLOAT, "01234567890.213E0"},
103 {token.FLOAT, "0.2e+10"},
104 {token.FLOAT, "1.2e-10"},
105 {token.FLOAT, "42.54e+10"},
106 {token.FLOAT, "01234567890.98e-10"},
107 {token.FLOAT, "0.1E+10"},
108 {token.FLOAT, "1.1E-10"},
109 {token.FLOAT, "42.1E+10"},
110 {token.FLOAT, "01234567890.1E-10"},
111 {token.FLOAT, "-0.0"},
112 {token.FLOAT, "-1.0"},
113 {token.FLOAT, "-42.0"},
114 {token.FLOAT, "-01234567890.0"},
115 {token.FLOAT, "-0e0"},
116 {token.FLOAT, "-1e0"},
117 {token.FLOAT, "-42e0"},
118 {token.FLOAT, "-01234567890e0"},
119 {token.FLOAT, "-0E0"},
120 {token.FLOAT, "-1E0"},
121 {token.FLOAT, "-42E0"},
122 {token.FLOAT, "-01234567890E0"},
123 {token.FLOAT, "-0e+10"},
124 {token.FLOAT, "-1e-10"},
125 {token.FLOAT, "-42e+10"},
126 {token.FLOAT, "-01234567890e-10"},
127 {token.FLOAT, "-0E+10"},
128 {token.FLOAT, "-1E-10"},
129 {token.FLOAT, "-42E+10"},
130 {token.FLOAT, "-01234567890E-10"},
131 {token.FLOAT, "-01.8e0"},
132 {token.FLOAT, "-1.4e0"},
133 {token.FLOAT, "-42.2e0"},
134 {token.FLOAT, "-01234567890.12e0"},
135 {token.FLOAT, "-0.E0"},
136 {token.FLOAT, "-1.12E0"},
137 {token.FLOAT, "-42.123E0"},
138 {token.FLOAT, "-01234567890.213E0"},
139 {token.FLOAT, "-0.2e+10"},
140 {token.FLOAT, "-1.2e-10"},
141 {token.FLOAT, "-42.54e+10"},
142 {token.FLOAT, "-01234567890.98e-10"},
143 {token.FLOAT, "-0.1E+10"},
144 {token.FLOAT, "-1.1E-10"},
145 {token.FLOAT, "-42.1E+10"},
146 {token.FLOAT, "-01234567890.1E-10"},
150 var orderedTokenLists = []string{
159 func TestPosition(t *testing.T) {
160 // create artifical source code
161 buf := new(bytes.Buffer)
163 for _, listName := range orderedTokenLists {
164 for _, ident := range tokenLists[listName] {
165 fmt.Fprintf(buf, "\t\t\t\t%s\n", ident.text)
169 s := New(buf.Bytes())
171 pos := token.Pos{"", 4, 1, 5}
173 for _, listName := range orderedTokenLists {
175 for _, k := range tokenLists[listName] {
177 // fmt.Printf("[%q] s = %+v:%+v\n", k.text, curPos.Offset, curPos.Column)
179 if curPos.Offset != pos.Offset {
180 t.Fatalf("offset = %d, want %d for %q", curPos.Offset, pos.Offset, k.text)
182 if curPos.Line != pos.Line {
183 t.Fatalf("line = %d, want %d for %q", curPos.Line, pos.Line, k.text)
185 if curPos.Column != pos.Column {
186 t.Fatalf("column = %d, want %d for %q", curPos.Column, pos.Column, k.text)
188 pos.Offset += 4 + len(k.text) + 1 // 4 tabs + token bytes + newline
189 pos.Line += countNewlines(k.text) + 1 // each token is on a new line
191 s.Error = func(pos token.Pos, msg string) {
192 t.Errorf("error %q for %q", msg, k.text)
198 // make sure there were no token-internal errors reported by scanner
199 if s.ErrorCount != 0 {
200 t.Errorf("%d errors", s.ErrorCount)
204 func TestComment(t *testing.T) {
205 testTokenList(t, tokenLists["comment"])
208 func TestOperator(t *testing.T) {
209 testTokenList(t, tokenLists["operator"])
212 func TestBool(t *testing.T) {
213 testTokenList(t, tokenLists["bool"])
216 func TestIdent(t *testing.T) {
217 testTokenList(t, tokenLists["ident"])
220 func TestString(t *testing.T) {
221 testTokenList(t, tokenLists["string"])
224 func TestNumber(t *testing.T) {
225 testTokenList(t, tokenLists["number"])
228 func TestFloat(t *testing.T) {
229 testTokenList(t, tokenLists["float"])
232 func TestRealExample(t *testing.T) {
238 "description": "bar",
239 "depends_on": ["something"]
244 literals := []struct {
249 {token.STRING, `"variable"`},
252 {token.STRING, `"foo"`},
255 {token.STRING, `"default"`},
257 {token.STRING, `"bar"`},
259 {token.STRING, `"description"`},
261 {token.STRING, `"bar"`},
263 {token.STRING, `"depends_on"`},
266 {token.STRING, `"something"`},
274 s := New([]byte(complexReal))
275 for _, l := range literals {
277 if l.tokenType != tok.Type {
278 t.Errorf("got: %s want %s for %s\n", tok, l.tokenType, tok.String())
281 if l.literal != tok.Text {
282 t.Errorf("got: %s want %s\n", tok, l.literal)
288 func TestError(t *testing.T) {
289 testError(t, "\x80", "1:1", "illegal UTF-8 encoding", token.ILLEGAL)
290 testError(t, "\xff", "1:1", "illegal UTF-8 encoding", token.ILLEGAL)
292 testError(t, `"ab`+"\x80", "1:4", "illegal UTF-8 encoding", token.STRING)
293 testError(t, `"abc`+"\xff", "1:5", "illegal UTF-8 encoding", token.STRING)
295 testError(t, `01238`, "1:7", "numbers cannot start with 0", token.NUMBER)
296 testError(t, `01238123`, "1:10", "numbers cannot start with 0", token.NUMBER)
297 testError(t, `'aa'`, "1:1", "illegal char: '", token.ILLEGAL)
299 testError(t, `"`, "1:2", "literal not terminated", token.STRING)
300 testError(t, `"abc`, "1:5", "literal not terminated", token.STRING)
301 testError(t, `"abc`+"\n", "1:5", "literal not terminated", token.STRING)
304 func testError(t *testing.T, src, pos, msg string, tok token.Type) {
305 s := New([]byte(src))
308 s.Error = func(p token.Pos, m string) {
310 if pos != p.String() {
311 t.Errorf("pos = %q, want %q for %q", p, pos, src)
315 t.Errorf("msg = %q, want %q for %q", m, msg, src)
323 t.Errorf("tok = %s, want %s for %q", tk, tok, src)
326 t.Errorf("error handler not called for %q", src)
328 if s.ErrorCount == 0 {
329 t.Errorf("count = %d, want > 0 for %q", s.ErrorCount, src)
333 func testTokenList(t *testing.T, tokenList []tokenPair) {
334 // create artifical source code
335 buf := new(bytes.Buffer)
336 for _, ident := range tokenList {
337 fmt.Fprintf(buf, "%s\n", ident.text)
340 s := New(buf.Bytes())
341 for _, ident := range tokenList {
343 if tok.Type != ident.tok {
344 t.Errorf("tok = %q want %q for %q\n", tok, ident.tok, ident.text)
347 if tok.Text != ident.text {
348 t.Errorf("text = %q want %q", tok.String(), ident.text)
354 func countNewlines(s string) int {
356 for _, ch := range s {