-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_composer.py
331 lines (246 loc) · 8.86 KB
/
test_composer.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
"""
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import composer
import pytest
def check(combinator, n, name=None):
# Check combinator type
assert getattr(composer, combinator)(*['foo' for _ in range(n)]).type == name if name is not None else combinator
def empty():
return {}
class TestAction:
def test_combinator_type(self):
assert getattr(composer.action('foo'), 'type') == 'action'
def test_valid_and_invalid_names(self):
combos = [
{ "n": 42, "s": False, "e": "Name must be a string" },
{ "n": "", "s": False, "e": "Name is not valid" },
{ "n": " ", "s": False, "e": "Name is not valid" },
{ "n": "/", "s": False, "e": "Name is not valid" },
{ "n": "//", "s": False, "e": "Name is not valid" },
{ "n": "/a", "s": False, "e": "Name is not valid" },
{ "n": "/a/b/c/d", "s": False, "e": "Name is not valid" },
{ "n": "/a/b/c/d/", "s": False, "e": "Name is not valid" },
{ "n": "a/b/c/d", "s": False, "e": "Name is not valid" },
{ "n": "/a/ /b", "s": False, "e": "Name is not valid" },
{ "n": "a", "e": False, "s": "/_/a" },
{ "n": "a/b", "e": False, "s": "/_/a/b" },
{ "n": "a/b/c", "e": False, "s": "/a/b/c" },
{ "n": "/a/b", "e": False, "s": "/a/b" },
{ "n": "/a/b/c", "e": False, "s": "/a/b/c" }
]
for combo in combos:
if combo["s"] is not False:
# good cases
assert composer.action(combo["n"]).name == combo["s"]
else:
# error cases
try:
composer.action(combo["n"])
assert False
except composer.ComposerError as error:
assert error.message.startswith(combo["e"])
def test_valid_and_invalid_options(self):
composer.action('foo', {})
try:
composer.action('foo', 42)
assert False
except composer.ComposerError as error:
assert error.message.startswith('Invalid argument')
class TestComposition:
def test_combinator_type(self):
assert getattr(composer.composition('foo'), 'type') == 'composition'
def test_valid_and_invalid_names(self):
combos = [
{ "n": 42, "s": False, "e": "Name must be a string" },
{ "n": "", "s": False, "e": "Name is not valid" },
{ "n": " ", "s": False, "e": "Name is not valid" },
{ "n": "/", "s": False, "e": "Name is not valid" },
{ "n": "//", "s": False, "e": "Name is not valid" },
{ "n": "/a", "s": False, "e": "Name is not valid" },
{ "n": "/a/b/c/d", "s": False, "e": "Name is not valid" },
{ "n": "/a/b/c/d/", "s": False, "e": "Name is not valid" },
{ "n": "a/b/c/d", "s": False, "e": "Name is not valid" },
{ "n": "/a/ /b", "s": False, "e": "Name is not valid" },
{ "n": "a", "e": False, "s": "/_/a" },
{ "n": "a/b", "e": False, "s": "/_/a/b" },
{ "n": "a/b/c", "e": False, "s": "/a/b/c" },
{ "n": "/a/b", "e": False, "s": "/a/b" },
{ "n": "/a/b/c", "e": False, "s": "/a/b/c" }
]
for combo in combos:
if combo["s"] is not False:
# good cases
assert composer.composition(combo["n"]).name == combo["s"]
else:
# error cases
try:
composer.composition(combo["n"])
assert False
except composer.ComposerError as error:
assert error.message.startswith(combo["e"])
class TestFunction:
def test_check(self):
check('function', 1)
def test_function(self):
composer.function(lambda : {})
def test_string(self):
composer.function('lambda : {}')
def test_number_invalid(self):
try:
composer.function(42)
except composer.ComposerError as error:
assert error.message.startswith('Invalid argument')
class TestLiteral:
def test_check(self):
check('literal', 1)
def test_boolean(self):
composer.literal(True)
def test_number(self):
composer.literal(42)
def test_string(self):
composer.literal('foo')
def test_dict(self):
composer.literal({ 'foo':42 })
def test_function_invalid(self):
try:
composer.literal(lambda : {})
except composer.ComposerError as error:
assert error.message.startswith('Invalid argument')
class TestValue:
def test_check(self):
check('value', 1)
def test_boolean(self):
composer.value(True)
def test_number(self):
composer.value(42)
def test_string(self):
composer.value('foo')
def test_dict(self):
composer.value({ 'foo':42 })
def test_function_invalid(self):
try:
composer.value(lambda : {})
except composer.ComposerError as error:
assert error.message.startswith('Invalid argument')
class TestParse:
def test_combinator_type(self):
composer.parse({
'type': 'sequence',
'components': [{
'type': 'action',
'name': 'echo'
}, {
'type': 'action',
'name': 'echo'
}]
}).type == 'sequence'
class TestTask:
def test_check(self):
check('task', 1, 'action')
def test_string(self):
composer.task('isNotOne')
def test_function(self):
composer.task(empty)
def test_lambda(self):
composer.task(lambda : {})
def test_none(self):
composer.task(None)
def test_boolean_invalid(self):
try:
composer.task(False)
assert False
except composer.ComposerError as error:
assert error.message.startswith('Invalid argument')
def test_dict_invalid(self):
try:
composer.task({ "foo": 42 })
assert False
except composer.ComposerError as error:
assert error.message.startswith('Invalid argument')
class TestLet:
def test_variable_argument_count(self):
composer.let({})
composer.let({}, 'foo')
composer.let({}, 'foo', 'foo')
def test_combinator_type(self):
assert composer.let({}).type == 'let'
class TestRepeat:
def test_variable_argument_count(self):
composer.repeat(42)
composer.repeat(42, 'foo')
composer.repeat(42, 'foo', 'foo')
def test_combinator_type(self):
assert composer.repeat(42).type == 'repeat'
class TestRetry:
def test_variable_argument_count(self):
composer.retry(42)
composer.retry(42, 'foo')
composer.retry(42, 'foo', 'foo')
def test_combinator_type(self):
assert composer.retry(42).type == 'retry'
class TestWhen:
def test_check(self):
check('when', 2)
class TestWhenNoSave:
def test_check(self):
check('when_nosave', 2)
class TestLoop:
def test_check(self):
check('loop', 2)
class TestLoopNoSave:
def test_check(self):
check('loop_nosave', 2)
class TestDoLoop:
def test_check(self):
check('doloop', 2)
class TestDoLoopNoSave:
def test_check(self):
check('doloop_nosave', 2)
class TestDo:
def test_check(self):
check('do', 2)
class TestEnsure:
def test_check(self):
check('ensure', 2)
class TestExec:
def test_check(self):
check('exec', 0)
class TestEmpty:
def test_check(self):
check('empty', 0)
class TestMask:
def test_check(self):
check('mask', 0)
class TestAsync:
def test_check(self):
check('async', 0)
class TestMap:
def test_check(self):
check('map', 0)
class TestRetain:
def test_check(self):
check('retain', 0)
class TestRetainCatch:
def test_check(self):
check('retain_catch', 0)
class TestSequence:
def test_check(self):
check('sequence', 0)
class TestSeq:
def test_check(self):
check('seq', 0)
class TestMerge:
def test_check(self):
check('merge', 0)