forked from dropbox/stone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_generator.py
More file actions
215 lines (185 loc) · 6.21 KB
/
Copy pathtest_generator.py
File metadata and controls
215 lines (185 loc) · 6.21 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from __future__ import absolute_import, division, print_function, unicode_literals
import argparse
import unittest
from stone.api import (
ApiNamespace,
ApiRoute,
)
from stone.data_type import (
List,
Boolean,
String,
Struct,
StructField,
)
from stone.generator import CodeGenerator
class Tester(CodeGenerator):
"""A no-op generator used to test helper methods."""
def generate(self, api):
pass
class TesterCmdline(CodeGenerator):
cmdline_parser = argparse.ArgumentParser()
cmdline_parser.add_argument('-v', '--verbose', action='store_true')
def generate(self, api):
pass
class TestGenerator(unittest.TestCase):
"""
Tests the interface exposed to Generators.
"""
def test_api_namespace(self):
ns = ApiNamespace('files')
a1 = Struct('A1', None, ns)
a1.set_attributes(None, [StructField('f1', Boolean(), None, None)])
a2 = Struct('A2', None, ns)
a2.set_attributes(None, [StructField('f2', Boolean(), None, None)])
l = List(a1)
s = String()
route = ApiRoute('test/route', None)
route.set_attributes(None, None, l, a2, s, None)
ns.add_route(route)
# Test that only user-defined types are returned.
route_io = ns.get_route_io_data_types()
self.assertIn(a1, route_io)
self.assertIn(a2, route_io)
self.assertNotIn(l, route_io)
self.assertNotIn(s, route_io)
def test_code_generator_helpers(self):
t = Tester(None, [])
self.assertEqual(t.filter_out_none_valued_keys({}), {})
self.assertEqual(t.filter_out_none_valued_keys({'a': None}), {})
self.assertEqual(t.filter_out_none_valued_keys({'a': None, 'b': 3}), {'b': 3})
def test_code_generator_basic_emitters(self):
t = Tester(None, [])
# Check basic emit
t.emit('hello')
self.assertEqual(t.output_buffer_to_string(), 'hello\n')
t.clear_output_buffer()
# Check that newlines are disallowed in emit
self.assertRaises(AssertionError, lambda: t.emit('hello\n'))
# Check indent context manager
t.emit('hello')
with t.indent():
t.emit('world')
with t.indent():
t.emit('!')
expected = """\
hello
world
!
"""
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
# --------------------------------------------------------
# Check text wrapping emitter
with t.indent():
t.emit_wrapped_text('Colorless green ideas sleep furiously',
prefix='$', initial_prefix='>',
subsequent_prefix='|', width=13)
expected = """\
$>Colorless
$|green
$|ideas
$|sleep
$|furiously
"""
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
def test_code_generator_list_gen(self):
t = Tester(None, [])
t.generate_multiline_list(['a=1', 'b=2'])
expected = """\
(a=1,
b=2)
"""
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
t.generate_multiline_list(['a=1', 'b=2'], 'def __init__', after=':')
expected = """\
def __init__(a=1,
b=2):
"""
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
t.generate_multiline_list(['a=1', 'b=2'], before='function_to_call', compact=False)
expected = """\
function_to_call(
a=1,
b=2,
)
"""
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
t.generate_multiline_list(['a=1', 'b=2'], 'function_to_call',
compact=False, skip_last_sep=True)
expected = """\
function_to_call(
a=1,
b=2
)
"""
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
t.generate_multiline_list(['a=1', 'b=2'], 'def func', ':',
compact=False, skip_last_sep=True)
expected = """\
def func(
a=1,
b=2
):
"""
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
t.generate_multiline_list(['a=1'], 'function_to_call', compact=False)
expected = 'function_to_call(a=1)\n'
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
t.generate_multiline_list(['a=1'], 'function_to_call', compact=True)
expected = 'function_to_call(a=1)\n'
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
t.generate_multiline_list([], 'function_to_call', compact=False)
expected = 'function_to_call()\n'
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
t.generate_multiline_list([], 'function_to_call', compact=True)
expected = 'function_to_call()\n'
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
# Test delimiter
t.generate_multiline_list(['String'], 'List', delim=('<', '>'), compact=True)
expected = 'List<String>\n'
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
def test_code_generator_block_gen(self):
t = Tester(None, [])
with t.block('int sq(int x)', ';'):
t.emit('return x*x;')
expected = """\
int sq(int x) {
return x*x;
};
"""
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
with t.block('int sq(int x)', allman=True):
t.emit('return x*x;')
expected = """\
int sq(int x)
{
return x*x;
}
"""
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
with t.block('int sq(int x)', delim=('<', '>'), dent=8):
t.emit('return x*x;')
expected = """\
int sq(int x) <
return x*x;
>
"""
self.assertEqual(t.output_buffer_to_string(), expected)
t.clear_output_buffer()
def test_generator_cmdline(self):
t = TesterCmdline(None, ['-v'])
self.assertTrue(t.args.verbose)