Debian Codegraph @

parsing/kdl/internal/tokenizer_test.go

raw symbols SCIP
1 package internal
2
3 import (
4 "testing"
5 )
6
7 func collectTokens(t *testing.T, input string) []Token {
8 t.Helper()
9 tok := NewTokenizer(input)
10 var tokens []Token
11 for {
12 token, err := tok.NextToken()
13 if err != nil {
14 t.Fatalf("unexpected error: %v", err)
15 }
16 tokens = append(tokens, token)
17 if token.Type == TokenEOF {
18 break
19 }
20 }
21 return tokens
22 }
23
24 func TestTokenizer_BareIdentifier(t *testing.T) {
25 tokens := collectTokens(t, "node")
26 if len(tokens) != 2 {
27 t.Fatalf("expected 2 tokens, got %d", len(tokens))
28 }
29 if tokens[0].Type != TokenIdentifier || tokens[0].Value != "node" {
30 t.Errorf("expected identifier 'node', got %v %q", tokens[0].Type, tokens[0].Value)
31 }
32 }
33
34 func TestTokenizer_QuotedString(t *testing.T) {
35 tokens := collectTokens(t, `"hello world"`)
36 if tokens[0].Type != TokenQuotedString || tokens[0].Value != "hello world" {
37 t.Errorf("expected quoted string 'hello world', got %v %q", tokens[0].Type, tokens[0].Value)
38 }
39 }
40
41 func TestTokenizer_StringEscapes(t *testing.T) {
42 tokens := collectTokens(t, `"hello\nworld\t!"`)
43 if tokens[0].Value != "hello\nworld\t!" {
44 t.Errorf("expected escaped string, got %q", tokens[0].Value)
45 }
46 }
47
48 func TestTokenizer_UnicodeEscape(t *testing.T) {
49 tokens := collectTokens(t, `"\u{1F600}"`)
50 if tokens[0].Value != "\U0001F600" {
51 t.Errorf("expected unicode char, got %q", tokens[0].Value)
52 }
53 }
54
55 func TestTokenizer_Integer(t *testing.T) {
56 tokens := collectTokens(t, "42")
57 if tokens[0].Type != TokenInteger || tokens[0].Value != "42" {
58 t.Errorf("expected integer 42, got %v %q", tokens[0].Type, tokens[0].Value)
59 }
60 }
61
62 func TestTokenizer_NegativeInteger(t *testing.T) {
63 tokens := collectTokens(t, "-7")
64 if tokens[0].Type != TokenInteger || tokens[0].Value != "-7" {
65 t.Errorf("expected integer -7, got %v %q", tokens[0].Type, tokens[0].Value)
66 }
67 }
68
69 func TestTokenizer_Float(t *testing.T) {
70 tokens := collectTokens(t, "3.14")
71 if tokens[0].Type != TokenFloat || tokens[0].Value != "3.14" {
72 t.Errorf("expected float 3.14, got %v %q", tokens[0].Type, tokens[0].Value)
73 }
74 }
75
76 func TestTokenizer_FloatExponent(t *testing.T) {
77 tokens := collectTokens(t, "1.5e10")
78 if tokens[0].Type != TokenFloat || tokens[0].Value != "1.5e10" {
79 t.Errorf("expected float 1.5e10, got %v %q", tokens[0].Type, tokens[0].Value)
80 }
81 }
82
83 func TestTokenizer_HexInt(t *testing.T) {
84 tokens := collectTokens(t, "0xff")
85 if tokens[0].Type != TokenHexInt || tokens[0].Value != "0xff" {
86 t.Errorf("expected hex int, got %v %q", tokens[0].Type, tokens[0].Value)
87 }
88 }
89
90 func TestTokenizer_OctalInt(t *testing.T) {
91 tokens := collectTokens(t, "0o77")
92 if tokens[0].Type != TokenOctalInt || tokens[0].Value != "0o77" {
93 t.Errorf("expected octal int, got %v %q", tokens[0].Type, tokens[0].Value)
94 }
95 }
96
97 func TestTokenizer_BinaryInt(t *testing.T) {
98 tokens := collectTokens(t, "0b1010")
99 if tokens[0].Type != TokenBinaryInt || tokens[0].Value != "0b1010" {
100 t.Errorf("expected binary int, got %v %q", tokens[0].Type, tokens[0].Value)
101 }
102 }
103
104 func TestTokenizer_UnderscoreInNumbers(t *testing.T) {
105 tokens := collectTokens(t, "1_000_000")
106 if tokens[0].Type != TokenInteger || tokens[0].Value != "1000000" {
107 t.Errorf("expected integer 1000000, got %v %q", tokens[0].Type, tokens[0].Value)
108 }
109 }
110
111 func TestTokenizer_V1True(t *testing.T) {
112 tok := NewTokenizer("true")
113 token, err := tok.NextToken()
114 if err != nil {
115 t.Fatal(err)
116 }
117 if token.Type != TokenTrue || token.Value != "true" {
118 t.Errorf("expected v1 true, got %v %q", token.Type, token.Value)
119 }
120 if tok.Version != Version1 {
121 t.Errorf("expected version 1, got %v", tok.Version)
122 }
123 }
124
125 func TestTokenizer_V1False(t *testing.T) {
126 tok := NewTokenizer("false")
127 token, err := tok.NextToken()
128 if err != nil {
129 t.Fatal(err)
130 }
131 if token.Type != TokenFalse || token.Value != "false" {
132 t.Errorf("expected v1 false, got %v %q", token.Type, token.Value)
133 }
134 }
135
136 func TestTokenizer_V1Null(t *testing.T) {
137 tok := NewTokenizer("null")
138 token, err := tok.NextToken()
139 if err != nil {
140 t.Fatal(err)
141 }
142 if token.Type != TokenNull || token.Value != "null" {
143 t.Errorf("expected v1 null, got %v %q", token.Type, token.Value)
144 }
145 }
146
147 func TestTokenizer_V2True(t *testing.T) {
148 tok := NewTokenizer("#true")
149 token, err := tok.NextToken()
150 if err != nil {
151 t.Fatal(err)
152 }
153 if token.Type != TokenTrue || token.Value != "#true" {
154 t.Errorf("expected v2 true, got %v %q", token.Type, token.Value)
155 }
156 if tok.Version != Version2 {
157 t.Errorf("expected version 2, got %v", tok.Version)
158 }
159 }
160
161 func TestTokenizer_V2False(t *testing.T) {
162 tok := NewTokenizer("#false")
163 token, err := tok.NextToken()
164 if err != nil {
165 t.Fatal(err)
166 }
167 if token.Type != TokenFalse || token.Value != "#false" {
168 t.Errorf("expected v2 false, got %v %q", token.Type, token.Value)
169 }
170 }
171
172 func TestTokenizer_V2Null(t *testing.T) {
173 tok := NewTokenizer("#null")
174 token, err := tok.NextToken()
175 if err != nil {
176 t.Fatal(err)
177 }
178 if token.Type != TokenNull || token.Value != "#null" {
179 t.Errorf("expected v2 null, got %v %q", token.Type, token.Value)
180 }
181 }
182
183 func TestTokenizer_V2Inf(t *testing.T) {
184 tok := NewTokenizer("#inf")
185 token, err := tok.NextToken()
186 if err != nil {
187 t.Fatal(err)
188 }
189 if token.Type != TokenInf {
190 t.Errorf("expected inf, got %v", token.Type)
191 }
192 }
193
194 func TestTokenizer_V2NegInf(t *testing.T) {
195 tok := NewTokenizer("#-inf")
196 token, err := tok.NextToken()
197 if err != nil {
198 t.Fatal(err)
199 }
200 if token.Type != TokenNegInf {
201 t.Errorf("expected -inf, got %v", token.Type)
202 }
203 }
204
205 func TestTokenizer_V2NaN(t *testing.T) {
206 tok := NewTokenizer("#nan")
207 token, err := tok.NextToken()
208 if err != nil {
209 t.Fatal(err)
210 }
211 if token.Type != TokenNaN {
212 t.Errorf("expected nan, got %v", token.Type)
213 }
214 }
215
216 func TestTokenizer_V1RawString(t *testing.T) {
217 tok := NewTokenizer(`r"hello world"`)
218 token, err := tok.NextToken()
219 if err != nil {
220 t.Fatal(err)
221 }
222 if token.Type != TokenRawString || token.Value != "hello world" {
223 t.Errorf("expected raw string 'hello world', got %v %q", token.Type, token.Value)
224 }
225 if tok.Version != Version1 {
226 t.Errorf("expected version 1, got %v", tok.Version)
227 }
228 }
229
230 func TestTokenizer_V1RawStringWithHashes(t *testing.T) {
231 tok := NewTokenizer(`r#"hello "world""#`)
232 token, err := tok.NextToken()
233 if err != nil {
234 t.Fatal(err)
235 }
236 if token.Type != TokenRawString || token.Value != `hello "world"` {
237 t.Errorf("expected raw string, got %v %q", token.Type, token.Value)
238 }
239 }
240
241 func TestTokenizer_V2RawString(t *testing.T) {
242 tok := NewTokenizer(`#"hello world"#`)
243 token, err := tok.NextToken()
244 if err != nil {
245 t.Fatal(err)
246 }
247 if token.Type != TokenRawString || token.Value != "hello world" {
248 t.Errorf("expected raw string 'hello world', got %v %q", token.Type, token.Value)
249 }
250 if tok.Version != Version2 {
251 t.Errorf("expected version 2, got %v", tok.Version)
252 }
253 }
254
255 func TestTokenizer_V2RawStringDoubleHash(t *testing.T) {
256 tok := NewTokenizer(`##"contains #"quotes"# inside"##`)
257 token, err := tok.NextToken()
258 if err != nil {
259 t.Fatal(err)
260 }
261 if token.Type != TokenRawString || token.Value != `contains #"quotes"# inside` {
262 t.Errorf("expected raw string, got %v %q", token.Type, token.Value)
263 }
264 }
265
266 func TestTokenizer_MultiLineString(t *testing.T) {
267 input := "\"\"\"\n hello\n world\n \"\"\""
268 tok := NewTokenizer(input)
269 token, err := tok.NextToken()
270 if err != nil {
271 t.Fatal(err)
272 }
273 if token.Type != TokenMultiLineString {
274 t.Errorf("expected multi-line string, got %v", token.Type)
275 }
276 if token.Value != "hello\nworld" {
277 t.Errorf("expected dedented content, got %q", token.Value)
278 }
279 }
280
281 func TestTokenizer_Braces(t *testing.T) {
282 tokens := collectTokens(t, "{ }")
283 if tokens[0].Type != TokenOpenBrace {
284 t.Errorf("expected open brace, got %v", tokens[0].Type)
285 }
286 if tokens[1].Type != TokenCloseBrace {
287 t.Errorf("expected close brace, got %v", tokens[1].Type)
288 }
289 }
290
291 func TestTokenizer_Equals(t *testing.T) {
292 tokens := collectTokens(t, "key=val")
293 if tokens[1].Type != TokenEquals {
294 t.Errorf("expected equals, got %v", tokens[1].Type)
295 }
296 }
297
298 func TestTokenizer_SlashDash(t *testing.T) {
299 tokens := collectTokens(t, "/-")
300 if tokens[0].Type != TokenSlashDash {
301 t.Errorf("expected slash-dash, got %v", tokens[0].Type)
302 }
303 }
304
305 func TestTokenizer_LineComment(t *testing.T) {
306 tokens := collectTokens(t, "node // comment\nother")
307 types := tokenTypes(tokens)
308 expected := []TokenType{TokenIdentifier, TokenNewline, TokenIdentifier, TokenEOF}
309 assertTokenTypes(t, types, expected)
310 }
311
312 func TestTokenizer_BlockComment(t *testing.T) {
313 tokens := collectTokens(t, "node /* comment */ other")
314 if tokens[0].Type != TokenIdentifier || tokens[0].Value != "node" {
315 t.Errorf("expected 'node', got %v %q", tokens[0].Type, tokens[0].Value)
316 }
317 if tokens[1].Type != TokenIdentifier || tokens[1].Value != "other" {
318 t.Errorf("expected 'other', got %v %q", tokens[1].Type, tokens[1].Value)
319 }
320 }
321
322 func TestTokenizer_NestedBlockComment(t *testing.T) {
323 tokens := collectTokens(t, "node /* outer /* inner */ outer */ other")
324 if tokens[0].Value != "node" {
325 t.Errorf("expected 'node', got %q", tokens[0].Value)
326 }
327 if tokens[1].Value != "other" {
328 t.Errorf("expected 'other', got %q", tokens[1].Value)
329 }
330 }
331
332 func TestTokenizer_LineContinuation(t *testing.T) {
333 tokens := collectTokens(t, "node \\\n arg")
334 if tokens[0].Type != TokenIdentifier || tokens[0].Value != "node" {
335 t.Errorf("expected 'node', got %v %q", tokens[0].Type, tokens[0].Value)
336 }
337 if tokens[1].Type != TokenIdentifier || tokens[1].Value != "arg" {
338 t.Errorf("expected 'arg', got %v %q", tokens[1].Type, tokens[1].Value)
339 }
340 }
341
342 func TestTokenizer_Semicolon(t *testing.T) {
343 tokens := collectTokens(t, "a; b")
344 types := tokenTypes(tokens)
345 expected := []TokenType{TokenIdentifier, TokenSemicolon, TokenIdentifier, TokenEOF}
346 assertTokenTypes(t, types, expected)
347 }
348
349 func TestTokenizer_TypeAnnotation(t *testing.T) {
350 tokens := collectTokens(t, "(u8)123")
351 types := tokenTypes(tokens)
352 expected := []TokenType{TokenOpenParen, TokenIdentifier, TokenCloseParen, TokenInteger, TokenEOF}
353 assertTokenTypes(t, types, expected)
354 }
355
356 func TestTokenizer_EscapeS(t *testing.T) {
357 tok := NewTokenizer(`"\s"`)
358 token, err := tok.NextToken()
359 if err != nil {
360 t.Fatal(err)
361 }
362 if token.Value != " " {
363 t.Errorf(`expected space from \s, got %q`, token.Value)
364 }
365 if tok.Version != Version2 {
366 t.Errorf("expected version 2, got %v", tok.Version)
367 }
368 }
369
370 func tokenTypes(tokens []Token) []TokenType {
371 types := make([]TokenType, len(tokens))
372 for i, t := range tokens {
373 types[i] = t.Type
374 }
375 return types
376 }
377
378 func assertTokenTypes(t *testing.T, got, expected []TokenType) {
379 t.Helper()
380 if len(got) != len(expected) {
381 t.Errorf("expected %d tokens, got %d", len(expected), len(got))
382 return
383 }
384 for i := range expected {
385 if got[i] != expected[i] {
386 t.Errorf("token %d: expected %v, got %v", i, expected[i], got[i])
387 }
388 }
389 }