-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer_test.py
More file actions
83 lines (58 loc) · 1.92 KB
/
lexer_test.py
File metadata and controls
83 lines (58 loc) · 1.92 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
"""Tests for lexer.py"""
import mock
import unittest
import lexer
from kk_token import TOKENS
import util
__author__ = 'dan.barella@gmail.com (Dan Barella)'
def mock_file(lines):
"""Returns a mock file-like object with lines as its contents.
Args:
lines (list of (list of str)): The contents of the mock file
Returns (mock.MagicMock of file): A mock file whose readlines method returns
the argument lines.
"""
m = mock.MagicMock(spec=file)
m.readlines.return_value = lines
return m
class TestLexer(unittest.TestCase):
def test_single_character(self):
"""Test a single character."""
# We shall inject this text field into our lexer
source = mock_file([
['a'],
])
expected_token_kinds = [TOKENS[...]]
l = lexer.Lexer(source)
count = 0
for token, kind in zip(l, expected_token_kinds):
self.assertTrue(token.is_kind(kind))
count += 1
self.assertEqual(1, count) # Make sure that only one token was emitted
def test_single_escape_sequence(self):
"""Test a single escape sequence."""
source = mock_file([
["'", "n"],
])
expected_token_kinds = [TOKENS[...]]
l = lexer.Lexer(source)
count = 0
for token, kind in zip(l, expected_token_kinds):
self.assertTrue(token.is_kind(kind))
count += 1
self.assertEqual(1, count) # Make sure that only one token was emitted
def test_multiple_escape_sequences(self):
"""Test a multiple escape sequences."""
source = mock_file([
["'", "n", "v"],
["^", "'", "<"],
])
expected_token_kinds = [TOKENS[...], TOKENS["v"], TOKENS["<"], TOKENS[...]]
l = lexer.Lexer(source)
count = 0
for token, kind in zip(l, expected_token_kinds):
self.assertTrue(token.is_kind(kind))
count += 1
self.assertEqual(4, count) # Make sure that only one token was emitted
if __name__ == '__main__':
unittest.main()