-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex_test.go
126 lines (116 loc) · 2.38 KB
/
lex_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
package main
import (
"testing"
"unicode/utf8"
)
// Tables for the tests
var eqnData = []struct {
in string
out rune
}{
{"", eof},
{"+", '+'},
{"/", '/'},
{"-", '-'},
{"^", '^'},
{"$$", '$'},
{"2 * 3", '2'},
{"a = 45.67", 'a'},
{"j = 12", 'j'},
{"XyZ = 12", 'X'},
{"-2", '-'},
}
var charData = []struct {
in string
out rune
sidx int
width int
}{
{"", eof, 0, 0},
{"+", '+', 1, 1},
{"/", '/', 1, 1},
{"-", '-', 1, 1},
{"^", '^', 1, 1},
{"$$", '$', 1, 1},
{"2", '2', 1, 1},
{"a", 'a', 1, 1},
{"X", 'X', 1, 1},
{"-2", '-', 1, 1},
{"3.14", '3', 1, 1},
{"⌘", '⌘', 3, 3},
{"本", '本', 3, 3},
}
func TestMatch(t *testing.T) {
// skip empty string
for _, td := range eqnData[1:] {
lxr := &HocLex{src: td.in}
in, _ := utf8.DecodeRuneInString(td.in)
gotb := lxr.match(in)
if !gotb {
t.Errorf("lexer.match err for %s: Got %t, Exp: %t\n", td.in, gotb, true)
}
}
}
func TestRead(t *testing.T) {
for _, td := range charData {
lxr := &HocLex{src: td.in}
gotc := lxr.read()
if gotc != td.out {
t.Errorf("lexer.read err for %s: Got %q, Exp: %q\n", td.in, gotc, td.out)
}
if (lxr.sidx != lxr.width) || lxr.width != td.width {
t.Errorf("lexer.read err for %s: lxr.width: %d, lxr.sidx: %d, exp: %d\n",
td.in, lxr.width, lxr.sidx, td.width)
}
}
}
func TestBackup(t *testing.T) {
for _, td := range charData {
lxr := &HocLex{src: td.in}
_ = lxr.read()
lxr.backup()
if lxr.sidx != (td.sidx - lxr.width) {
t.Errorf("lexer.backup err for %s: lxr.width: %d, lxr.sidx: %d, exp: %d\n",
td.in, lxr.width, lxr.sidx, (td.sidx - lxr.width))
}
}
}
func TestIsIdentChar(t *testing.T) {
var charTestData = []struct {
in rune // careful, casting to rune
out bool
}{
{'_', true},
{'a', true},
{'j', true},
{'⌘', false},
{6, false},
{6 - 'a', true},
{255, true},
}
for _, tt := range charTestData {
got := isIdentChar(tt.in)
if got != tt.out {
t.Errorf("isIdentChar err for %q: Got %t, Exp: %t\n", tt.in, got, tt.out)
}
}
}
func TestIsSpace(t *testing.T) {
var charTestData = []struct {
in rune // careful, casting to rune
out bool
}{
{'_', false},
{'a', false},
{' ', true},
{'\t', true},
{'\n', false}, // handled by the grammar
{255, false},
}
for _, tt := range charTestData {
got := isSpace(tt.in)
if got != tt.out {
t.Errorf("isSpace err for %q: Got %t, Exp: %t\n", tt.in, got, tt.out)
}
}
}