-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer_test.go
59 lines (54 loc) · 1.29 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
package main
import (
"bytes"
"testing"
)
func TestLexer(t *testing.T) {
testMap := map[string][]Token{
"1.3+2.343": {
NewToken(Number, []byte("1.3")...),
NewToken(Plus, '+'),
NewToken(Number, []byte("2.343")...),
},
"(<><=>= ===> = != !)": {
NewToken(LeftBrace, '('),
NewToken(LT, '<'),
NewToken(GT, '>'),
NewToken(LTEQ, '<', '='),
NewToken(GTEQ, '>', '='),
NewToken(Eq, '=', '='),
NewToken(Assign, '='),
NewToken(GT, '>'),
NewToken(Assign, '='),
NewToken(NotEq, '!', '='),
NewToken(Not, '!'),
NewToken(RightBrace, ')'),
},
}
for k, v := range testMap {
if !matchLexialAnalysis(t, k, v) {
t.Fail()
return
}
}
}
func matchLexialAnalysis(t *testing.T, expr string, expectedTokens []Token) bool {
lexer := NewLexer(bytes.NewReader([]byte(expr)))
err := lexer.Analyze()
if err != nil {
t.Error("error occured in lexical analysis ", err)
}
for i := range expectedTokens {
if len(expectedTokens) != len(lexer.tokenList) {
t.Errorf("varing list of tokens, expected: %d, actual: %d", (expectedTokens), (lexer.tokenList))
return false
}
expected := expectedTokens[i]
actual := lexer.tokenList[i]
if !expected.Equals(actual) {
t.Errorf("%s didn't matched with expected %s", actual, expected)
return false
}
}
return true
}