forked from dropbox/stone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_backend.py
More file actions
executable file
·230 lines (195 loc) · 6.73 KB
/
test_backend.py
File metadata and controls
executable file
·230 lines (195 loc) · 6.73 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
from __future__ import absolute_import, division, print_function, unicode_literals
import unittest
from stone.ir import (
ApiNamespace,
ApiRoute,
List,
Boolean,
String,
Struct,
StructField,
)
from stone.backend import CodeBackend
_MYPY = False
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression
# Hack to get around some of Python 2's standard library modules that
# accept ascii-encodable unicode literals in lieu of strs, but where
# actually passing such literals results in errors with mypy --py2. See
# <https://github.com/python/typeshed/issues/756> and
# <https://github.com/python/mypy/issues/2536>.
import importlib
argparse = importlib.import_module(str('argparse')) # type: typing.Any
class _Tester(CodeBackend):
"""A no-op backend used to test helper methods."""
def generate(self, api):
pass
class _TesterCmdline(CodeBackend):
cmdline_parser = argparse.ArgumentParser()
cmdline_parser.add_argument('-v', '--verbose', action='store_true')
def generate(self, api):
pass
class TestBackend(unittest.TestCase):
"""
Tests the interface exposed to backends.
"""
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)])
l1 = List(a1)
s = String()
route = ApiRoute('test/route', None)
route.set_attributes(None, None, l1, 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(l1, route_io)
self.assertNotIn(s, route_io)
def test_code_backend_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_backend_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_backend_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_backend_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_backend_cmdline(self):
t = _TesterCmdline(None, ['-v'])
self.assertTrue(t.args.verbose)
if __name__ == '__main__':
unittest.main()