-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer_test.go
85 lines (78 loc) · 1.73 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package lexer
import (
"testing"
"github.com/usamaroman/uman/token"
)
func TestNextToken(t *testing.T) {
input := `:= == != ;,+-*/
() { }
<= > < >=
true = "true"
тест: строка = "тест"
номер: число = 5
5 6
истина
цикл (;)
[1, 2]
`
tests := []struct {
expectedType token.TokenType
expectedLiteral string
}{
{token.COLON, ":"},
{token.ASSIGN, "="},
{token.EQUALS, "=="},
{token.NEQ, "!="},
{token.SEMICOLON, ";"},
{token.COMMA, ","},
{token.PLUS, "+"},
{token.MINUS, "-"},
{token.ASTERISK, "*"},
{token.SLASH, "/"},
{token.LPAREN, "("},
{token.RPAREN, ")"},
{token.LBRACE, "{"},
{token.RBRACE, "}"},
{token.ELT, "<="},
{token.GT, ">"},
{token.LT, "<"},
{token.EGT, ">="},
{token.IDENT, "true"},
{token.ASSIGN, "="},
{token.STRING_VAL, "true"},
{token.IDENT, "тест"},
{token.COLON, ":"},
{token.STRING, "строка"},
{token.ASSIGN, "="},
{token.STRING_VAL, "тест"},
{token.IDENT, "номер"},
{token.COLON, ":"},
{token.INT, "число"},
{token.ASSIGN, "="},
{token.INT_VAL, "5"},
{token.INT_VAL, "5"},
{token.INT_VAL, "6"},
{token.TRUE, "истина"},
{token.FOR, "цикл"},
{token.LPAREN, "("},
{token.SEMICOLON, ";"},
{token.RPAREN, ")"},
{token.LBRACKET, "["},
{token.INT_VAL, "1"},
{token.COMMA, ","},
{token.INT_VAL, "2"},
{token.RBRACKET, "]"},
}
l := New(input)
for i, tt := range tests {
tok := l.NextToken()
if tok.Type != tt.expectedType {
t.Fatalf("tests[%d] - tokentype wrong. expected=%q, got=%q",
i, tt.expectedType, tok.Type)
}
if tok.Literal != tt.expectedLiteral {
t.Fatalf("tests[%d] - literal wrong. expected=%q, got=%q",
i, tt.expectedLiteral, tok.Literal)
}
}
}