-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_parser_types.py
More file actions
563 lines (438 loc) · 14.5 KB
/
test_parser_types.py
File metadata and controls
563 lines (438 loc) · 14.5 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import pytest
from mcfunction import nodes, parser_types
from mcfunction.exceptions import ParserException
from mcfunction.util import get
def test_any():
any = parser_types.Any()
parts = iter(('part0', 'part1'))
node = any.parse(parts)
assert node.value == 'part0'
assert get(parts) == 'part1'
def test_block():
block = parser_types.Block()
parts = iter((
'test:block',
'test:block[power=69]{value:69}',
'#test:blocks',
'not quite valid block'
))
node = block.parse(parts)
assert not node.is_tag
assert node.namespace == 'test'
assert node.name == 'block'
assert node.blockstates is None
assert node.datatags is None
node = block.parse(parts)
assert not node.is_tag
assert node.namespace == 'test'
assert node.name == 'block'
assert node.blockstates == '[power=69]'
assert node.datatags == '{value:69}'
node = block.parse(parts)
assert node.is_tag
assert node.namespace == 'test'
assert node.name == 'blocks'
assert node.blockstates is None
assert node.datatags is None
with pytest.raises(ParserException, match='expected valid block, not .*'):
block.parse(parts)
# TODO: Coordinate test
# NOTE: it already has 100% coverage thanks to Position(), Position2d(), ...
# tests
def test_greedyany():
greedyany = parser_types.GreedyAny()
parts = iter(('part0', 'part1'))
node = greedyany.parse(parts)
assert node.value == 'part0 part1'
with pytest.raises(ParserException, match='too few arguments'):
greedyany.parse(parts)
# ensure all parts have been used
with pytest.raises(StopIteration):
get(parts)
def test_double():
double = parser_types.Double()
parts = iter(('69', '6.9', 'not a double'))
node = double.parse(parts)
assert node.value == 69
node = double.parse(parts)
assert node.value == 6.9
with pytest.raises(ParserException, match='expected double, not .*'):
double.parse(parts)
def test_entity():
entity = parser_types.Entity()
parts = iter((
'TestUser',
'0-0-0-0-0',
'@r',
'@e[type=player]',
'@everyone',
'a',
'something invalid',
'TestUser['
))
node = entity.parse(parts)
assert node.selector == 'TestUser'
assert not node.conditions
node = entity.parse(parts)
assert node.selector == '0-0-0-0-0'
assert not node.conditions
node = entity.parse(parts)
assert node.selector == '@r'
assert not node.conditions
node = entity.parse(parts)
assert node.selector == '@e'
assert node.conditions[0].name == 'type'
assert node.conditions[0].value == 'player'
# @everyone
with pytest.raises(ParserException, match='invalid selector: .*'):
entity.parse(parts)
# a (too short)
with pytest.raises(ParserException, match='invalid username: .*'):
entity.parse(parts)
# something invalid (space)
with pytest.raises(ParserException, match='invalid username: .*'):
entity.parse(parts)
# TestUser[ (malformed)
with pytest.raises(ParserException,
match='expected \']\' at the end of valid entity: .*'):
entity.parse(parts)
def test_function():
function = parser_types.Function()
parts = iter((
'test:function',
'test:directory/function',
'#test:events/on_tick',
'something invalid'
))
node = function.parse(parts)
assert not node.is_tag
assert node.namespace == 'test'
assert node.name == 'function'
node = function.parse(parts)
assert not node.is_tag
assert node.namespace == 'test'
assert node.name == 'directory/function'
node = function.parse(parts)
assert node.is_tag
assert node.namespace == 'test'
assert node.name == 'events/on_tick'
with pytest.raises(ParserException):
function.parse(parts)
def test_ip_address():
ip = parser_types.IPAddress()
parts = iter((
'127.0.0.1',
'0.0.0.0',
'255.255.255.255',
'256.255.255.255',
'-1.0.0.0',
'i.i.i.i',
'invalid'
))
node = ip.parse(parts)
assert node.value == '127.0.0.1'
node = ip.parse(parts)
assert node.value == '0.0.0.0'
node = ip.parse(parts)
assert node.value == '255.255.255.255'
with pytest.raises(ParserException, match='invalid ip address'):
ip.parse(parts)
with pytest.raises(ParserException, match='invalid ip address'):
ip.parse(parts)
with pytest.raises(ParserException, match='non number in ip address'):
ip.parse(parts)
with pytest.raises(ParserException, match='malformed ip address'):
ip.parse(parts)
def test_integer():
integer = parser_types.Integer()
parts = iter((
'69',
'6.9',
'not a number'
))
node = integer.parse(parts)
assert node.value == 69
with pytest.raises(ParserException, match='expected integer, not .*'):
integer.parse(parts)
with pytest.raises(ParserException, match='expected integer, not .*'):
integer.parse(parts)
def test_item():
item = parser_types.Item()
parts = iter((
'test:item',
'test:item{value:69}',
'#test:items',
'not valid'
))
node = item.parse(parts)
assert not node.is_tag
assert node.namespace == 'test'
assert node.name == 'item'
assert node.datatags is None
node = item.parse(parts)
assert not node.is_tag
assert node.namespace == 'test'
assert node.name == 'item'
assert node.datatags == '{value:69}'
node = item.parse(parts)
assert node.is_tag
assert node.namespace == 'test'
assert node.name == 'items'
assert node.datatags is None
with pytest.raises(ParserException, match='expected valid item, not .*'):
item.parse(parts)
def test_json():
json = parser_types.JSON()
parts = iter((
'"test successful"',
'{"text":"test successful"}',
'test fails'
))
node = json.parse(parts)
assert node.object == 'test successful'
node = json.parse(parts)
assert node.object == {'text': 'test successful'}
with pytest.raises(ParserException, match='expected valid json, not .*'):
json.parse(parts)
def test_literal():
literal = parser_types.Literal('valid')
parts = iter((
'valid',
'not valid'
))
node = literal.parse(parts)
assert node.value == 'valid'
with pytest.raises(ParserException, match='expected .*, not .*'):
literal.parse(parts)
def test_namespaceid():
namespaceid = parser_types.NamespaceID()
parts = iter((
'namespace_is_optional',
'namespace:name',
'test:a/b/c',
'INVALID'
))
node = namespaceid.parse(parts)
assert node.namespace is None
assert node.name == 'namespace_is_optional'
node = namespaceid.parse(parts)
assert node.namespace == 'namespace'
assert node.name == 'name'
node = namespaceid.parse(parts)
assert node.namespace == 'test'
assert node.name == 'a/b/c'
with pytest.raises(ParserException,
match='expected valid namespace identifier, not .*'):
namespaceid.parse(parts)
def test_objective():
objective = parser_types.Objective()
parts = iter((
'testobjective',
'waaaaay-tooo-looooooong',
'invalid-character:'
))
node = objective.parse(parts)
assert node.value == 'testobjective'
with pytest.raises(ParserException,
match='expected valid objective, not .*'):
objective.parse(parts)
with pytest.raises(ParserException,
match='expected valid objective, not .*'):
objective.parse(parts)
def test_particle():
particle = parser_types.Particle()
parts = iter((
'test:particle',
'test:dust',
'minecraft:dust', '0', '1', '2', '3',
'minecraft:block', 'test:block',
'minecraft:falling_dust', 'test:block',
'minecraft:item', 'test:item',
'minecraft:vibration', '0', '0', '0', '1', '1', '1', '2',
'minecraft:dust_color_transition', '0', '1', '2', '3', '4', '5', '6',
'invalid particle',
'minecraft:dust'
))
node = particle.parse(parts)
assert node.namespace == 'test'
assert node.name == 'particle'
assert not node.arguments
node = particle.parse(parts)
assert node.namespace == 'test'
assert node.name == 'dust'
assert not node.arguments
node = particle.parse(parts)
assert node.namespace == 'minecraft'
assert node.name == 'dust'
assert [x.value for x in node.arguments] == [0, 1, 2, 3]
node = particle.parse(parts)
assert node.namespace == 'minecraft'
assert node.name == 'block'
assert isinstance(node.arguments[0], nodes.BlockNode)
node = particle.parse(parts)
assert node.namespace == 'minecraft'
assert node.name == 'falling_dust'
assert isinstance(node.arguments[0], nodes.BlockNode)
node = particle.parse(parts)
assert node.namespace == 'minecraft'
assert node.name == 'item'
assert isinstance(node.arguments[0], nodes.NamespaceIDNode)
node = particle.parse(parts)
assert node.namespace == 'minecraft'
assert node.name == 'vibration'
assert isinstance(node.arguments[0], nodes.PositionNode)
assert isinstance(node.arguments[1], nodes.PositionNode)
assert isinstance(node.arguments[2], nodes.IntegerNode)
node = particle.parse(parts)
assert node.namespace == 'minecraft'
assert node.name == 'dust_color_transition'
assert [x.value for x in node.arguments] == [0, 1, 2, 3, 4, 5, 6]
with pytest.raises(ParserException,
match='expected valid particle, not .*'):
particle.parse(parts)
with pytest.raises(StopIteration):
particle.parse(parts)
def test_position():
position = parser_types.Position()
parts = iter((
'0', '0', '0',
'~', '~', '~',
'^.1', '^0.1', '^0.00000000001',
'a',
'1'
))
node = position.parse(parts)
assert node.x.value == 0
assert not node.x.relative and not node.x.local
assert node.y.value == 0
assert not node.y.relative and not node.y.local
assert node.z.value == 0
assert not node.z.relative and not node.z.local
node = position.parse(parts)
assert node.x.value == 0
assert node.x.relative and not node.x.local
assert node.y.value == 0
assert node.y.relative and not node.y.local
assert node.z.value == 0
assert node.z.relative and not node.z.local
node = position.parse(parts)
assert node.x.value == 0.1
assert not node.x.relative and node.x.local
assert node.y.value == 0.1
assert not node.y.relative and node.y.local
assert node.z.value == 0.00000000001
assert not node.z.relative and node.z.local
with pytest.raises(ParserException, match='invalid coordinate: .*'):
position.parse(parts)
with pytest.raises(StopIteration):
position.parse(parts)
def test_position2d():
position2d = parser_types.Position2d()
parts = iter((
'0', '0',
'~', '~',
'^.1', '^0.1',
'a',
'1'
))
node = position2d.parse(parts)
assert node.x.value == 0
assert not node.x.relative and not node.x.local
assert node.z.value == 0
assert not node.z.relative and not node.z.local
node = position2d.parse(parts)
assert node.x.value == 0
assert node.x.relative and not node.x.local
assert node.z.value == 0
assert node.z.relative and not node.z.local
node = position2d.parse(parts)
assert node.x.value == 0.1
assert not node.x.relative and node.x.local
assert node.z.value == 0.1
assert not node.z.relative and node.z.local
with pytest.raises(ParserException, match='invalid coordinate: .*'):
position2d.parse(parts)
with pytest.raises(StopIteration):
position2d.parse(parts)
def test_scoreboardentity():
scoreboardentity = parser_types.ScoreboardEntity()
parts = iter((
'Player',
'*',
'A' * 256,
'⨁',
''
))
node = scoreboardentity.parse(parts)
assert node.selector == 'Player'
node = scoreboardentity.parse(parts)
assert node.selector == '*'
node = scoreboardentity.parse(parts)
assert node.selector == 'A' * 256
node = scoreboardentity.parse(parts)
assert node.selector == '⨁'
with pytest.raises(ParserException, match='invalid username: .*'):
scoreboardentity.parse(parts)
def test_rotation():
rotation = parser_types.Rotation()
parts = iter((
'0', '0',
'~', '~',
'^.1', '^0.1',
'a',
'1'
))
node = rotation.parse(parts)
assert node.yaw.value == 0
assert not node.yaw.relative and not node.yaw.local
assert node.pitch.value == 0
assert not node.pitch.relative and not node.pitch.local
node = rotation.parse(parts)
assert node.pitch.value == 0
assert node.pitch.relative and not node.pitch.local
assert node.pitch.value == 0
assert node.pitch.relative and not node.pitch.local
node = rotation.parse(parts)
assert node.pitch.value == 0.1
assert not node.pitch.relative and node.pitch.local
assert node.pitch.value == 0.1
assert not node.pitch.relative and node.pitch.local
with pytest.raises(ParserException, match='invalid coordinate: .*'):
rotation.parse(parts)
with pytest.raises(StopIteration):
rotation.parse(parts)
def test_union():
union = parser_types.Union('valid', 'valid_too', 'valid_three')
parts = iter((
'valid',
'valid_too',
'valid_three',
'valid_four'
))
node = union.parse(parts)
assert node.value == 'valid'
node = union.parse(parts)
assert node.value == 'valid_too'
node = union.parse(parts)
assert node.value == 'valid_three'
with pytest.raises(ParserException, match='expected any of .*, not .*'):
union.parse(parts)
def test_uuid():
uuid = parser_types.UUID()
parts = iter((
'2eb4b815-7a3c-4cf2-8845-bbc03b10ee35',
'1-2-3-4-5',
'a-b-c-d-e',
'A-B-C-D-E',
'invalid'
))
node = uuid.parse(parts)
assert node.value == '2eb4b815-7a3c-4cf2-8845-bbc03b10ee35'
node = uuid.parse(parts)
assert node.value == '1-2-3-4-5'
node = uuid.parse(parts)
assert node.value == 'a-b-c-d-e'
node = uuid.parse(parts)
assert node.value == 'A-B-C-D-E'
with pytest.raises(ParserException, match='expected valid uuid, not .*'):
uuid.parse(parts)