-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer_test.go
More file actions
154 lines (149 loc) · 3.91 KB
/
tokenizer_test.go
File metadata and controls
154 lines (149 loc) · 3.91 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package goxpath
import (
"strings"
"testing"
)
func TestNCName(t *testing.T) {
testdata := []struct {
input token
output bool
}{
{token{tokNumber, 1}, false},
}
for _, td := range testdata {
got := td.input.isNCName()
if expected := td.output; got != expected {
t.Errorf("%v isNCName() = %t, want %t", td.input, got, expected)
}
}
}
func TestGetQName(t *testing.T) {
testdata := []struct {
input string
output string
}{
{"hello", "hello"},
{"abc*def", "abc"},
{"abc:def", "abc:def"},
{"abc:def:ghi", "abc:def"},
{"abc_def", "abc_def"},
{"abc-def", "abc-def"},
{"abc·def", "abc·def"},
{"abc‿def", "abc‿def"},
{"a123", "a123"},
}
for _, td := range testdata {
sr := strings.NewReader(td.input)
res, err := getQName(sr)
if err != nil {
t.Error(err.Error())
}
if got, expected := res, td.output; got != expected {
t.Errorf("getWord(%s) = %s, want %s", td.input, res, expected)
}
}
}
func TestGetDelimitedString(t *testing.T) {
testdata := []struct {
input string
output string
}{
{`"hello"`, `hello`},
{`'hello'`, `hello`},
{`'he"llo'`, `he"llo`},
{`"he'llo"`, `he'llo`},
}
for _, td := range testdata {
sr := strings.NewReader(td.input)
res, err := getDelimitedString(sr)
if err != nil {
t.Error(err.Error())
}
if got, expected := res, td.output; got != expected {
t.Errorf("getDelimitedString(%s) = %s, want %s", td.input, res, expected)
}
}
}
func TestGetAxis(t *testing.T) {
testdata := []struct {
input string
output []token
}{
{`child::sub`, []token{{"child", tokDoubleColon}, {"sub", tokQName}}},
}
for _, td := range testdata {
toklist, err := stringToTokenlist(td.input)
if err != nil {
t.Error(err)
}
toks := toklist.toks
if len(toks) != len(td.output) {
t.Errorf("len(toks) = %d (%v), want %d (%v)", len(toks), toks, len(td.output), td.output)
} else {
for i, tok := range toks {
expected := td.output[i]
valMatch := tok.Value == expected.Value
if !valMatch {
if fa, aOk := ToFloat64(tok.Value); aOk {
if fb, bOk := ToFloat64(expected.Value); bOk {
valMatch = fa == fb
}
}
}
if tok.Typ != expected.Typ || !valMatch {
t.Errorf("tok[%d] = %v, want %v", i, tok, expected)
}
}
}
}
}
func TestOperator(t *testing.T) {
testdata := []struct {
input string
output []token
}{
{`< (:comment (:nested :) :) `, []token{{`<`, tokOperator}}},
{`"hello"`, []token{{"hello", tokString}}},
{`'hello'`, []token{{"hello", tokString}}},
{`< `, []token{{`<`, tokOperator}}},
{`<= `, []token{{`<=`, tokOperator}}},
{`> `, []token{{`>`, tokOperator}}},
{`>= `, []token{{`>=`, tokOperator}}},
{`!= `, []token{{`!=`, tokOperator}}},
{`<< `, []token{{`<<`, tokOperator}}},
{`>> `, []token{{`>>`, tokOperator}}},
{`/ `, []token{{`/`, tokOperator}}},
{`// `, []token{{`//`, tokOperator}}},
{`: `, []token{{`:`, tokOperator}}},
{`:: `, []token{{`::`, tokOperator}}},
{`.`, []token{{`.`, tokOperator}}},
{`(1,2)`, []token{{'(', tokOpenParen}, {1.0, tokNumber}, {`,`, tokComma}, {2.0, tokNumber}, {')', tokCloseParen}}},
{`$hello`, []token{{"hello", tokVarname}}},
{`a("a",'/')`, []token{{"a", tokQName}, {'(', tokOpenParen}, {"a", tokString}, {`,`, tokComma}, {"/", tokString}, {')', tokCloseParen}}},
}
for _, td := range testdata {
toklist, err := stringToTokenlist(td.input)
if err != nil {
t.Error(err)
}
toks := toklist.toks
if len(toks) != len(td.output) {
t.Errorf("len(toks) = %d (%v), want %d (%v)", len(toks), toks, len(td.output), td.output)
} else {
for i, tok := range toks {
expected := td.output[i]
valMatch := tok.Value == expected.Value
if !valMatch {
if fa, aOk := ToFloat64(tok.Value); aOk {
if fb, bOk := ToFloat64(expected.Value); bOk {
valMatch = fa == fb
}
}
}
if tok.Typ != expected.Typ || !valMatch {
t.Errorf("tok[%d] = %v, want %v", i, tok, expected)
}
}
}
}
}