-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_kigen.py
191 lines (154 loc) · 5.3 KB
/
test_kigen.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
import os
import sys
import unittest
import kigen
_test_block = """This is just a test
# KIGEN_start foo_func arg1:a arg2:b arg3:c
# Yeah, just a comment
# WOO!
# KIGEN_end
Some useless stuff
// C style inline comments work too
// KIGEN_start bar_func arg1:a arg2:c arg3:e
int main() {
dothebusiness();
}
// KIGEN_end
Need to test empty blocks
## KIGEN_start baz_func
## KIGEN_end
"""
_test_block_content = [
"This is just a test",
"Some useless stuff\n// C style inline comments work too",
"\nNeed to test empty blocks"
]
_test_block2 = """# KIGEN_start foo_func
# KIGEN_end
This is just a test
# KIGEN_start foo_func arg1:a arg2:b arg3:c
# Yeah, just a comment
# WOO!
# KIGEN_end
Some useless stuff
// C style inline comments work too
// KIGEN_start bar_func arg1:a arg2:c arg3:e
int main() {
dothebusiness();
}
// KIGEN_end
Need to test empty blocks
## KIGEN_start baz_func
## KIGEN_end
"""
_test_block2_content = [
'',
"This is just a test",
"Some useless stuff\n// C style inline comments work too",
"\nNeed to test empty blocks"
]
_test_block3 = """This is just a test
# KIGEN_start foo_func arg1:a arg2:b arg3:c
# KIGEN_end
Some useless stuff
// C style inline comments work too
// KIGEN_start bar_func arg1:a arg2:c arg3:e
// KIGEN_end
Need to test empty blocks
## KIGEN_start baz_func
## KIGEN_end
"""
_block_positions = (
(1, 4),
(7, 11),
(14, 15)
)
_block_funcs = (
'foo_func',
'bar_func',
'baz_func'
)
_block_args = (
{'arg1': 'a', 'arg2': 'b', 'arg3': 'c'},
{'arg1': 'a', 'arg2': 'c', 'arg3': 'e'},
{}
)
_module_dir_data = {
'expected_modules': [
'foo',
'bar',
'baz'
]
}
_basic_template = "Hello {{ name }}"
_basic_args = {'name': 'Larry'}
_basic_expectation = "Hello Larry"
TEST_DATA_DIR = os.path.join('.', 'test_collateral')
class TestKiGen(unittest.TestCase):
def setUp(self):
self._starting_modules = list(sys.modules.keys())
def tearDown(self):
sysmod_keys = list(sys.modules.keys())
for key in sysmod_keys:
if key not in self._starting_modules:
sys.modules.pop(key)
def test_block_extraction(self):
blocks = kigen.extract_blocks(_test_block)
assert len(blocks) == len(_block_positions)
for idx, block in enumerate(blocks):
assert block.start == _block_positions[idx][0]
assert block.end == _block_positions[idx][1]
assert block.command.module == _block_funcs[idx]
assert len(block.command.args) == len(_block_args[idx])
for arg in block.command.args:
assert block.command.args[arg] == _block_args[idx][arg]
def test_module_extraction(self):
mod_space = kigen.enumerate_modules_in_dir(TEST_DATA_DIR)
assert sorted(mod_space.modules) == sorted(_module_dir_data['expected_modules'])
def test_split_at_blocks_lead_in(self):
blocks = kigen.extract_blocks(_test_block)
file_chunks = kigen.split_file_at_blocks(_test_block, blocks)
assert file_chunks == _test_block_content
def test_split_at_blocks_no_lead_in(self):
blocks = kigen.extract_blocks(_test_block2)
file_chunks = kigen.split_file_at_blocks(_test_block2, blocks)
assert file_chunks == _test_block2_content
def test_basic_expansion(self):
result = kigen.expand_template(_basic_template, _basic_args)
assert result == _basic_expectation
def test_module_loading(self):
mod_space = kigen.enumerate_modules_in_dir(TEST_DATA_DIR)
modules = kigen.load_modules(TEST_DATA_DIR, mod_space.modules)
assert sorted(modules.keys()) == sorted(mod_space.modules)
def test_command_round_trip(self):
original = "# KIGEN_start foo arg1:a arg2:b"
_, command = kigen.extract_command(original)
reconstructed = ("# KIGEN_start {}"
.format(kigen.command_to_cmdstr(command)))
assert original == reconstructed
def test_block_start_string(self):
original = "# KIGEN_start foo arg1:a arg2:b\n# KIGEN_end"
blocks = kigen.extract_blocks(original)
assert len(blocks) == 1
boi = blocks[0]
start_str = kigen.block_to_start_string(boi)
assert start_str == "# KIGEN_start foo arg1:a arg2:b"
def test_recombine(self):
blocks = kigen.extract_blocks(_test_block3)
render_blocks = ['{}\n{}'.format(kigen.block_to_start_string(x),
kigen.block_to_end_string(x))
for x in blocks]
file_chunks = kigen.split_file_at_blocks(_test_block3, blocks)
result = kigen.recombine(file_chunks, render_blocks)
assert result.strip() == _test_block3.strip()
def test_render_file(self):
modules = kigen.build_module_dict(TEST_DATA_DIR)
autogen_file_path = os.path.join(TEST_DATA_DIR, 'file_to_autogen.rb')
compare_file_path = os.path.join(TEST_DATA_DIR, 'file_to_compare.rb')
with open(compare_file_path) as ifile:
compare_file_data = ifile.read()
with open(autogen_file_path) as ifile:
autogen_file_data = ifile.read()
expanded_autogen_file = kigen.render_file(autogen_file_data, modules)
assert expanded_autogen_file.strip() == compare_file_data.strip()