-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_lexer.py
67 lines (52 loc) · 1.58 KB
/
test_lexer.py
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
import _lexer
import _token
def test_next_token():
input = '=+()},;'
lexer = _lexer.Lexer(input)
assert lexer.next_token().token_type == _token.TokenType.ASSIGN
assert lexer.next_token().token_type == _token.TokenType.PLUS
def test_complex_token():
source_code = """
let five = 5;
let ten = 10;
let add = fn(x, y) {
x + y;
};
let result = add(five, ten);
"""
lexer = _lexer.Lexer(source_code)
assert lexer.next_token().token_type == _token.TokenType.LET
assert lexer.next_token().token_type == _token.TokenType.IDENT
assert lexer.next_token().token_type == _token.TokenType.ASSIGN
assert lexer.next_token().token_type == _token.TokenType.INT
assert lexer.next_token().token_type == _token.TokenType.SEMICOLON
assert lexer.next_token().token_type == _token.TokenType.LET
def test_function_calling():
lexer = _lexer.Lexer('== !=')
assert lexer.next_token().token_type == _token.TokenType.EQ
assert lexer.next_token().token_type == _token.TokenType.NOT_EQ
# def test_invalid_inputs():
# source_code = """
# let five = 5;
# let ten = 10;
# let add = fn(x, y) {
# x + y;
# };
# let result = add(five, ten);
# !-/*5;
# 5 < 10 > 5;
# if (5 < 10) {
# return true;
# } else {
# return false;
# }
# // [...]
# """
# lexer = Lexer(source_code)
# while True:
# token = lexer.next_token()
# assert not token.token_type == TokenType.ILLEGAL
# if token.token_type == TokenType.EOF:
# break
# # if token.token_type == TokenType.ILLEGAL:
# # break