-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_extendedcaos.py
670 lines (615 loc) · 18.4 KB
/
test_extendedcaos.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
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
from extendedcaos import *
from caoslexer import *
import unittest
class TestExtendedCAOS(unittest.TestCase):
maxDiff = 999999999
def test_idempotent(self):
s = ' * hello\nnew: comp 3 1 21051 "elevines" 13 4 2000'
self.assertMultiLineEqual(s, extendedcaos_to_caos(s))
def test_moves_comments_to_own_line(self):
input = " stop* open cd tray\nstop"
desired_output = " * open cd tray\n stop\nstop"
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
input = " stop * open cd tray\nstop"
desired_output = " * open cd tray\n stop\nstop"
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
input = " stop * open cd tray\n* another"
desired_output = " * open cd tray\n stop\n* another"
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
input = " stop * open cd tray\n * another"
desired_output = " * open cd tray\n stop\n * another"
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
input = " * open cd tray"
desired_output = " * open cd tray"
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_parses_comment_at_eoi(self):
s = "* comment at eoi"
self.assertMultiLineEqual(s, extendedcaos_to_caos(s))
s = "* comment at eoi\n"
self.assertMultiLineEqual(s, extendedcaos_to_caos(s))
def test_named_variables(self):
input = """
sets $hello 5
setv $hello "world"
setv va01 8
setv $another "world"
scrp 1 2 3 1000
setv va00 0
setv va01 1
setv va03 3
setv $avariable 2
endm
scrp 1 2 3 1001
setv va00 0
setv $hello 1
setv va02 2
setv $another 3
setv $avariable 4
endm
scrp 1 2 3 1002
setv $start 5
gsub subroutine
subr subroutine
setv va00 6
retn
endm
rscr
enum 1 2 3
setv $posy posy
next
"""
desired_output = """
sets va00 5
setv va00 "world"
setv va01 8
setv va02 "world"
scrp 1 2 3 1000
setv va00 0
setv va01 1
setv va03 3
setv va02 2
endm
scrp 1 2 3 1001
setv va00 0
setv va01 1
setv va02 2
setv va03 3
setv va04 4
endm
scrp 1 2 3 1002
setv va01 5
gsub subroutine
subr subroutine
setv va00 6
retn
endm
rscr
enum 1 2 3
setv va00 posy
next
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_explicit_targ(self):
input = """
from.mvsf 5 6
ownr.mvsf 6 5
dbg: outv from.angl 0 0
"""
desired_output = """
seta va00 targ
targ from
mvsf 5 6
targ ownr
mvsf 6 5
targ from
setv va01 angl 0 0
targ va00
dbg: outv va01
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
input = """
dbg: outv angl va00.posx va00.posy
"""
desired_output = """
seta va01 targ
targ va00
setv va02 posx
setv va03 posy
targ va01
dbg: outv angl va02 va03
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
input = """
dbg: outv from.angl va00.posx va00.posy
"""
desired_output = """
seta va01 targ
targ va00
setv va02 posx
setv va03 posy
targ from
setv va04 angl va02 va03
targ va01
dbg: outv va04
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
input = """
doif $targetring.movs ne 0
"""
desired_output = """
seta va00 targ
targ va01
setv va02 movs
targ va00
doif va02 ne 0
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
input = """
dbg: outv $targetring.movs
dbg: outs $targetring.gall
seta va99 $targetring.carr
"""
desired_output = """
seta va00 targ
targ va01
setv va02 movs
targ va00
dbg: outv va02
seta va00 targ
targ va01
sets va03 gall
targ va00
dbg: outs va03
seta va00 targ
targ va01
seta va04 carr
targ va00
seta va99 va04
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_explicit_targ_in_doif(self):
input = """
doif movs <> 0
dbg: outv 0
doif 0 < 1
endi
elif posy > ownr.posy
dbg: outv 1
else
dbg: outv 2
endi
"""
desired_output = """
doif movs <> 0
dbg: outv 0
doif 0 < 1
endi
else
seta va00 targ
targ ownr
setv va01 posy
targ va00
doif posy > va01
dbg: outv 1
else
dbg: outv 2
endi
endi
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_explicit_targ_other_commands(self):
input = """
$targetring.tick tick
dbg: outv $targetring.tmvt posx posy
"""
desired_output = """
setv va00 tick
seta va01 targ
targ va02
tick va00
targ va01
setv va03 posx
setv va04 posy
seta va01 targ
targ va02
setv va05 tmvt va03 va04
targ va01
dbg: outv va05
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_remove_extraneous_targ_saving(self):
input = """
seta va01 targ
targ va00
setv va02 posx
targ va01
seta va01 targ
targ va00
setv va03 posy
targ va01
seta va01 targ
targ from
setv va04 angl va02 va03
targ va01
dbg: outv va04
"""
desired_output = """
seta va01 targ
targ va00
setv va02 posx
setv va03 posy
targ from
setv va04 angl va02 va03
targ va01
dbg: outv va04
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_remove_double_targ(self):
input = """
targ ownr
targ va00
targ va01
setv va02 posx
targ va01
"""
desired_output = """
targ va01
setv va02 posx
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_object_variables(self):
input = """
agent_variable $bioenergy ov63
subv targ.$bioenergy 1
subv ownr.$bioenergy 2
subv from.$bioenergy 3
"""
desired_output = """
subv ov63 1
subv mv63 2
subv avar from 63 3
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_get_indentation_at(self):
self.assertEqual(get_indentation_at(lexcaos(" "), 0), " ")
self.assertEqual(get_indentation_at(lexcaos(" bang"), 1), " ")
self.assertEqual(get_indentation_at(lexcaos(" \n bang"), 2,), " ")
self.assertEqual(get_indentation_at(lexcaos(" \n bang"), 3), " ")
self.assertEqual(
get_indentation_at(
[
(TOK_WHITESPACE, ""),
(TOK_WHITESPACE, " "),
(TOK_WHITESPACE, " "),
(TOK_WORD, "bang"),
(TOK_EOI, None),
],
3,
),
" ",
)
def test_strip_indent(self):
self.assertMultiLineEqual(
tokens_to_string(strip_indent(lexcaos(" bang\n bang\nbang"))),
"bang\n bang\nbang",
)
def test_add_indent(self):
self.assertMultiLineEqual(
tokens_to_string(add_indent(lexcaos("bang\n bang"), " ")),
" bang\n bang",
)
def test_macros_toplevel(self):
input = """
macro SimpleMacro
dbg: outs "inside macro"
endmacro
SimpleMacro
"""
desired_output = """
dbg: outs "inside macro"
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
input = """
macro SimpleMacroWithArg target
dbg: outs "inside macro"
endmacro
SimpleMacroWithArg 1
"""
desired_output = """
setv va00 1
dbg: outs "inside macro"
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
input = """
macro CreateMyAgent sprite_name
sets $sprite_name lowa $sprite_name
new: simp 1 2 1001 $sprite_name 13 4 2000
endmacro
sets $sprite_name "MY SPRITE"
* macro with variable
CreateMyAgent $sprite_name
* macro with literal
CreateMyAgent "my sprite"
* macro with command of an unknown result type
CreateMyAgent from
* make sure original variable is untouched
dbg: outs $sprite_name
"""
desired_output = """
sets va00 "MY SPRITE"
* macro with variable
doif type va00 = 0 or type va00 = 1
setv va01 va00
elif type va00 = 2
sets va01 va00
else
seta va01 va00
endi
sets va01 lowa va01
new: simp 1 2 1001 va01 13 4 2000
* macro with literal
sets va01 "my sprite"
sets va01 lowa va01
new: simp 1 2 1001 va01 13 4 2000
* macro with command of an unknown result type
doif type from = 0 or type from = 1
setv va01 from
elif type from = 2
sets va01 from
else
seta va01 from
endi
sets va01 lowa va01
new: simp 1 2 1001 va01 13 4 2000
* make sure original variable is untouched
dbg: outs va00
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_macros_toplevel2(self):
# This had gotten broken by some issues around lower-casing targ names for explicit targs
input = """
macro CreateElevine parent
dbg: outv $parent.clac
endmacro
CreateElevine null
"""
desired_output = """
seta va00 null
seta va01 targ
targ va00
setv va02 clac
targ va01
dbg: outv va02
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_macros_toplevel3(self):
# This was broken by including an erroneous TOK_EOI in the macro body and pasting it into the main script
input = """
macro CreateElevine parent
dbg: outv $parent.clac
endmacro
CreateElevine null
CreateElevine null
"""
desired_output = """
seta va00 null
seta va01 targ
targ va00
setv va02 clac
targ va01
dbg: outv va02
seta va00 null
seta va01 targ
targ va00
setv va03 clac
targ va01
dbg: outv va03
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_macros_remove_comments_when_removing_macro_definition(self):
input = """
macro MyMacro
dbg: outs "hello"
* this is a comment
dbg: outs " world"
endmacro
MyMacro
"""
desired_output = """
dbg: outs "hello"
* this is a comment
dbg: outs " world"
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_macros_exception_on_bad_argnames(self):
with self.assertRaises(Exception):
extendedcaos_to_caos("macro MyMacro $arg1\nendmacro")
def test_constants(self):
input = """
constant :my_classifier 3 21 4000
constant :my_clac_script_number 1000
new: simp :my_classifier "sprite_name" 1 1 1000
dbg: outv :my_clac_script_number
"""
desired_output = """
new: simp 3 21 4000 "sprite_name" 1 1 1000
dbg: outv 1000
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_agentvariable_on_variable(self):
input = """
agent_variable $unique_pose ov96
dbg: outs $parent.$unique_pose
"""
desired_output = """
dbg: outs avar va00 96
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_transform_elifs_into_elses(self):
input = """
doif 1 = 1
dbg: outs "first branch"
elif ownr.posy > 5
dbg: outs "second branch"
else
dbg: outs "third branch"
endi
"""
desired_output = """
doif 1 = 1
dbg: outs "first branch"
else
seta va00 targ
targ ownr
setv va01 posy
targ va00
doif va01 > 5
dbg: outs "second branch"
else
dbg: outs "third branch"
endi
endi
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_short_circuit_doifs(self):
input = """
doif $parent = null or posy > $parent.posy and 1 eq 2
dbg: outs "true"
else
dbg: outs "false"
endi
"""
desired_output = """
setv va00 0
doif va01 = null
setv va00 1
endi
doif va00 = 0
seta va02 targ
targ va01
setv va03 posy
targ va02
doif posy > va03
setv va00 1
endi
endi
doif va00 = 1
doif 1 eq 2
else
setv va00 0
endi
endi
doif va00 = 1
dbg: outs "true"
else
dbg: outs "false"
endi
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_short_circuit_within_macros(self):
input = """
macro MyMacro
doif $parent = null or posy > $parent.posy
dbg: outs "true"
endi
endmacro
MyMacro
"""
desired_output = """
setv va00 0
doif va01 = null
setv va00 1
endi
doif va00 = 0
seta va02 targ
targ va01
setv va03 posy
targ va02
doif posy > va03
setv va00 1
endi
endi
doif va00 = 1
dbg: outs "true"
endi
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_short_circuit_other_commands_taking_condition(self):
input = """
loop
dbg: outs "hello world"
untl $parent = null or posy > $parent.posy
"""
desired_output = """
loop
dbg: outs "hello world"
setv va00 0
doif va01 = null
setv va00 1
endi
doif va00 = 0
seta va02 targ
targ va01
setv va03 posy
targ va02
doif posy > va03
setv va00 1
endi
endi
untl va00 = 1
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
input = """
dbg: asrt $parent = null or posy > $parent.posy
"""
desired_output = """
setv va00 0
doif va01 = null
setv va00 1
endi
doif va00 = 0
seta va02 targ
targ va01
setv va03 posy
targ va02
doif posy > va03
setv va00 1
endi
endi
dbg: asrt va00 = 1
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_get_indentation_at_previous_line_when_previous_line_is_blank(self):
input = """
endi
doif"""
tokens = lexcaos(input)
self.assertEqual(
" ", get_indentation_at_previous_line(tokens, len(tokens) - 2)
)
def test_remove_double_targ_when_first_targ_is_expression(self):
# this failed when we were just looking at tokens, not parsed nodes
input = """
doif 1 = 1
kill targ
endi
targ ownr
"""
desired_output = """
doif 1 = 1
kill targ
endi
targ ownr
"""
self.assertMultiLineEqual(desired_output, extendedcaos_to_caos(input))
def test_parse_face(self):
# FACE is a different command depending on the expected return type
# openc2e handles this in a weird way, and thus commandinfo.json is weird
# Make sure our hack fix keeps working even if the commandinfo is changed
input = """
dbg: outs face
dbg: outv face
"""
self.assertMultiLineEqual(input, extendedcaos_to_caos(input))
if __name__ == "__main__":
unittest.main()