-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
149 lines (112 loc) · 6.83 KB
/
tests.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
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
import unittest
from lua_ast.ast import (Assignment, Block, Boolean, FunctionCall,
LiteralString, MethodCall, NamedField, nil, semicolon,
Table, UnnamedField, Var)
from lua_ast import parse
from lua_ast import printer
class ParserTestCase(unittest.TestCase):
def assertParsesFirstStatement(self, code, expected):
self.assertEqual(parse(code)[0], expected)
def assertParsesBlock(self, code, expected):
self.assertEqual(parse(code), expected)
def test_string(self):
self.assertParsesFirstStatement('x = "lorem ipsum"', [Assignment([Var('x')], [LiteralString("lorem ipsum")])])
def test_comments_are_ignored_at_the_end_of_line(self):
self.assertParsesFirstStatement('x = "lorem ipsum" -- comment',
[Assignment([Var('x')], [LiteralString("lorem ipsum")])])
def test_mutilple_string_assignments(self):
self.assertParsesBlock('x = "lorem ipsum"; y = "lipsum"',
Block(Assignment([Var('x')],
[LiteralString('lorem ipsum')]),
semicolon,
Assignment([Var('y')],
[LiteralString('lipsum')])))
def test_comments_lines_are_ignored(self):
self.assertParsesBlock('x = "lorem ipsum"\n'
'-- comment\n'
'-- \n'
'-- another comment\n'
'y = "lipsum"',
Block(Assignment([Var('x')],
[LiteralString('lorem ipsum')]),
Assignment([Var('y')],
[LiteralString('lipsum')])))
def test_nil(self):
self.assertParsesFirstStatement('x = nil', [Assignment([Var('x')], [nil])])
def test_empty_table(self):
self.assertParsesFirstStatement('x = {}', [Assignment([Var('x')], [Table()])])
def test_table_with_unnamed_fields(self):
self.assertParsesFirstStatement('x = {"lorem ipsum", true}',
[Assignment([Var('x')],
[Table(UnnamedField(LiteralString(value='lorem ipsum')),
UnnamedField(Boolean('true')))])])
def test_table_with_named_fields(self):
self.assertParsesFirstStatement('x = {field1="lorem ipsum", field2=true}',
[Assignment([Var('x')],
[Table(NamedField('field1', LiteralString('lorem ipsum')),
NamedField('field2', Boolean('true')))])])
def test_function_call_without_args(self):
self.assertParsesFirstStatement('f()',
[FunctionCall(Var('f'), [])])
def test_method_call_without_args(self):
self.assertParsesFirstStatement('o:f()',
[MethodCall(Var('o'), Var('f'), [])])
def test_function_call_with_args_list(self):
self.assertParsesFirstStatement('f(a)',
[FunctionCall(Var('f'), [Var('a')])])
def test_method_call_with_args_list(self):
self.assertParsesFirstStatement('o:f(a)',
[MethodCall(Var('o'), Var('f'), [Var('a')])])
def test_function_call_with_arg_string(self):
self.assertParsesFirstStatement('f "string"',
[FunctionCall(Var('f'), [LiteralString('string')])])
def test_method_call_with_arg_string(self):
self.assertParsesFirstStatement('o:f "string"',
[MethodCall(Var('o'), Var('f'), [LiteralString('string')])])
def test_function_chain_of_two_calls(self):
self.assertParsesFirstStatement('f(a)(b)',
[FunctionCall(FunctionCall(Var('f'), [Var('a')]), [Var('b')])])
def test_method_chain_of_two_calls(self):
self.assertParsesFirstStatement('o:m1(a):m2(b)',
[MethodCall(MethodCall(Var('o'), Var('m1'), [Var('a')]), Var('m2'), [Var('b')])])
def test_mixed_chain_of_two_calls(self):
self.assertParsesFirstStatement('o:m1(a)(b)',
[FunctionCall(MethodCall(Var('o'), Var('m1'), [Var('a')]), [Var('b')])])
def test_function_call_chain(self):
self.assertParsesFirstStatement('f(a)(b)(c)',
[FunctionCall(FunctionCall(FunctionCall(Var('f'), [Var('a')]), [Var('b')]), [Var('c')])])
def test_method_call_chain(self):
self.assertParsesFirstStatement('o:m1(a):m2(b, c):m3(d, e, f)',
[MethodCall(MethodCall(MethodCall(Var('o'), Var('m1'),
[Var('a')]),
Var('m2'), [Var('b'), Var('c')]),
Var('m3'), [Var('d'), Var('e'), Var('f')])])
def test_function_call_chain_with_strings_arguments(self):
self.assertParsesFirstStatement('f "a" "b" "c"',
[FunctionCall(FunctionCall(FunctionCall(Var('f'), [LiteralString('a')]), [LiteralString('b')]), [LiteralString('c')])])
def assertPrints(self, node, expected):
self.assertEqual(printer.ast_to_string(node), expected)
def test_print_assignment(self):
self.assertPrints(Assignment([Var('x')], [LiteralString('test')]), "x = 'test'")
def test_print_assignments(self):
self.assertPrints(parse("x = 'first'; y = 'second'"), "x = 'first'\ny = 'second'")
def test_print_functioncall_without_args(self):
self.assertPrints(parse("f()"), "f()")
def test_print_functioncall_with_single_arg(self):
self.assertPrints(parse("f 'argument'"), "f('argument')")
def test_print_functioncall_with_multiple_args(self):
self.assertPrints(parse("f('argument',true,false)"), "f('argument', true, false)")
def test_print_functioncall_chain(self):
self.assertPrints(parse("f(a)(b, c, d)(e)"), "f(a)(b, c, d)(e)")
def test_print_method(self):
self.assertPrints(parse("o:m()"), "o:m()")
def test_print_methodcall_chain(self):
self.assertPrints(parse("o:m1(a):m2(b, c, d):m3(e)"), "o:m1(a):m2(b, c, d):m3(e)")
def test_print_empty_table(self):
self.assertPrints(Table(), "{}")
def test_print_table_with_unnamed_fields(self):
self.assertPrints(parse("x = {true, false, 'test', nil}"), "x = {\n\ttrue,\n\tfalse,\n\t'test',\n\tnil\n}")
def test_print_table_with_named_fields(self):
self.assertPrints(parse("x = {true = '1', false = '2'}"), "x = {\n\ttrue = '1',\n\tfalse = '2'\n}")
if __name__ == '__main__':
unittest.main()