-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer_test.go
More file actions
181 lines (133 loc) · 3.37 KB
/
lexer_test.go
File metadata and controls
181 lines (133 loc) · 3.37 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bytes"
"testing"
)
// -------------------------- Utilities
// Creates a new lexer that reads from a given string
func newTestLexer(in string) *Lexer {
return NewLexer(
bytes.NewReader(
[]byte(in),
),
)
}
// Tests if two tokens match eachother, reports a test failure if they do not
func matchTokens(in string, want Token, got Token, t *testing.T) {
if want != got {
t.Fatalf(`Lexer(%s) returned %T%s... expected %T%s`, in, got, got, want, want)
}
}
func matchErrors(in string, want error, got error, t *testing.T) {
if got == nil {
t.Fatalf(`Lexer(%s) did not return an error... expected %s`, in, want)
} else if want != got {
t.Fatalf(`Lexer(%s) returned %s... expected %s`, in, got, want)
}
}
// -------------------------- Keywords
// Test when a correct def keyword is passed to the lexer
func TestLexerDef(t *testing.T) {
in := "def"
lexer := newTestLexer(in)
ch := lexer.Tokens()
go lexer.Run()
want := DefToken{}
got := <-ch
matchTokens(in, want, got, t)
}
// Test when a correct extern keyword is passed to the lexer
func TestLexerExtern(t *testing.T) {
in := "extern"
lexer := newTestLexer(in)
ch := lexer.Tokens()
go lexer.Run()
want := ExternToken{}
got := <-ch
matchTokens(in, want, got, t)
}
// -------------------------- Primary
// Test when a correct identifier is passed to the lexer
func TestLexerIdentifier(t *testing.T) {
in := "hello"
lexer := newTestLexer(in)
ch := lexer.Tokens()
go lexer.Run()
want := IdentifierToken{"hello"}
got := <-ch
matchTokens(in, want, got, t)
}
// Test when a correct int is passed to the lexer
func TestLexerInt(t *testing.T) {
in := "9"
lexer := newTestLexer(in)
ch := lexer.Tokens()
go lexer.Run()
want := NumberToken{"9"}
got := <-ch
matchTokens(in, want, got, t)
}
// Test when a correct hex number is passed to the lexer
func TestLexerHex(t *testing.T) {
in := "0xFF"
lexer := newTestLexer(in)
ch := lexer.Tokens()
go lexer.Run()
want := NumberToken{"0xFF"}
got := <-ch
matchTokens(in, want, got, t)
}
// Test when a correct float is passed to the lexer
func TestLexerFloat(t *testing.T) {
in := "9.3"
lexer := newTestLexer(in)
ch := lexer.Tokens()
go lexer.Run()
want := NumberToken{"9.3"}
got := <-ch
matchTokens(in, want, got, t)
}
// Test when an incorrect float is passed to the lexer
func TestLexerBadFloat(t *testing.T) {
in := "9.3.3.3.3"
lexer := newTestLexer(in)
ch := lexer.Tokens()
go lexer.Run()
// TODO decide what the goal for this test actually is
want := NumberToken{"9.3"}
// Consume the token to trigger the error
got := <-ch
matchTokens(in, want, got, t)
}
// -------------------------- EOF Marker
// Test if the lexer properly returns an EOF token after lexing everything else
func TestLexerEOF(t *testing.T) {
in := "def"
lexer := newTestLexer(in)
ch := lexer.Tokens()
go lexer.Run()
want := EOFToken{}
var got Token
got = <-ch // this should be a deftoken
got = <-ch
matchTokens(in, want, got, t)
}
// Test if the lexer can properly handle an empty input
func TestEmpty(t *testing.T) {
in := ""
lexer := newTestLexer(in)
ch := lexer.Tokens()
go lexer.Run()
want := EOFToken{}
got := <-ch
matchTokens(in, want, got, t)
}
func TestLexSpace(t *testing.T) {
in := "\n \r \n extern"
lexer := newTestLexer(in)
ch := lexer.Tokens()
go lexer.Run()
want := ExternToken{}
got := <-ch
matchTokens(in, want, got, t)
}