/
lexer_test.go
63 lines (57 loc) · 1.55 KB
/
lexer_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
package filter
import (
"strconv"
"strings"
"testing"
)
func testScanner(t *testing.T, src string, expectTok int) {
s := new(Scanner)
s.Init(src)
tok, lit, _ := s.Scan()
if tok != expectTok {
t.Errorf("Expect Scanner{%q}.Scan() = %#v, _ want %#v", src, tok, expectTok)
}
switch lit.(type) {
case string:
if lit != strings.Replace(src, "\"", "", -1) {
t.Errorf("Expect Scanner{%q}.Scan() = _, %#v want %#v", src, lit, src)
}
case int:
s, _ := strconv.Atoi(src)
if lit != s {
t.Errorf("Expect Scanner{%q}.Scan() = _, %#v want %#v", src, lit, src)
}
case bool:
s, _ := strconv.ParseBool(src)
if lit != s {
t.Errorf("Expect Scanner{%q}.Scan() = _, %#v want %#v", src, lit, src)
}
default:
t.Errorf("Expect Scanner{%q}.Scan() = _, %#v want %#v", src, lit, src)
}
tok, lit, _ = s.Scan()
if tok != EOF {
t.Errorf("Expect Scanner{%q}.Scan() = %#v, _ want %#v", src, tok, EOF)
}
}
func TestScanner(t *testing.T) {
testScanner(t, "ham", IDENT)
testScanner(t, "ham123", IDENT)
testScanner(t, "ham-123", IDENT)
testScanner(t, "ham_123", IDENT)
testScanner(t, "ham.123", IDENT)
testScanner(t, "ham:123", IDENT)
testScanner(t, "ham//123", IDENT)
testScanner(t, "\"ham\"", VALUE)
testScanner(t, "\"ham spam\"", VALUE)
testScanner(t, "\"ham@spam\"", VALUE)
testScanner(t, "\"ham (spam)\"", VALUE)
testScanner(t, "123", NUMBER)
testScanner(t, "123", NUMBER)
testScanner(t, "true", TRUE)
testScanner(t, "false", FALSE)
testScanner(t, "(", LPAREN)
testScanner(t, ")", RPAREN)
testScanner(t, "[", LBOXP)
testScanner(t, "]", RBOXP)
}