/
file_test.rb
822 lines (687 loc) · 20.8 KB
/
file_test.rb
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
require File.expand_path('../helper', __FILE__)
class CitrusFileTest < Test::Unit::TestCase
## File tests
def run_file_test(file, root)
match = File.parse(::File.read(file), :root => root)
assert(match)
end
%w<file grammar rule>.each do |type|
Dir[::File.dirname(__FILE__) + "/_files/#{type}*.citrus"].each do |path|
module_eval(<<-CODE.gsub(/^ /, ''), __FILE__, __LINE__ + 1)
def test_#{::File.basename(path, '.citrus')}
run_file_test("#{path}", :#{type})
end
CODE
end
end
## Hierarchical syntax
def test_expression_empty
assert_raise SyntaxError do
File.parse('', :root => :expression)
end
end
def test_expression_alias
match = File.parse('rule_name', :root => :expression)
assert(match)
assert_instance_of(Alias, match.value)
end
def test_expression_dot
match = File.parse('.', :root => :expression)
assert(match)
assert_instance_of(Terminal, match.value)
end
def test_expression_character_range
match = File.parse('[a-z]', :root => :expression)
assert(match)
assert_instance_of(Terminal, match.value)
end
def test_expression_terminal
match = File.parse('/./', :root => :expression)
assert(match)
assert_instance_of(Terminal, match.value)
end
def test_expression_string_terminal_empty
match = File.parse('""', :root => :expression)
assert(match)
assert_instance_of(StringTerminal, match.value)
end
def test_expression_string_terminal
match = File.parse('"a"', :root => :expression)
assert(match)
assert_instance_of(StringTerminal, match.value)
end
def test_expression_string_terminal_empty_block
match = File.parse('"" {}', :root => :expression)
assert(match)
assert_instance_of(StringTerminal, match.value)
end
def test_expression_repeat_string_terminal
match = File.parse('"a"*', :root => :expression)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_expression_repeat_empty_string_terminal_block
match = File.parse('""* {}', :root => :expression)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_expression_repeat_sequence
match = File.parse('("a" "b")*', :root => :expression)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_expression_repeat_choice
match = File.parse('("a" | "b")*', :root => :expression)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_expression_repeat_sequence_block
match = File.parse('("a" "b")* {}', :root => :expression)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_expression_repeat_choice_block
match = File.parse('("a" | "b")* {}', :root => :expression)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_expression_repeat_sequence_extension
match = File.parse('("a" "b")* <Module>', :root => :expression)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_expression_repeat_sequence_extension_spaced
match = File.parse('( "a" "b" )* <Module>', :root => :expression)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_expression_repeat_choice_extension
match = File.parse('("a" | "b")* <Module>', :root => :expression)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_expression_choice_terminal
match = File.parse('/./ | /./', :root => :expression)
assert(match)
assert_instance_of(Choice, match.value)
end
def test_expression_choice_string_terminal
match = File.parse('"a" | "b"', :root => :expression)
assert(match)
assert_instance_of(Choice, match.value)
end
def test_expression_choice_embedded_sequence
match = File.parse('"a" | ("b" "c")', :root => :expression)
assert(match)
assert_instance_of(Choice, match.value)
end
def test_expression_choice_mixed
match = File.parse('("a" | /./)', :root => :expression)
assert(match)
assert_instance_of(Choice, match.value)
end
def test_expression_choice_extended
match = File.parse('("a" | "b") <Module>', :root => :expression)
assert(match)
assert_instance_of(Choice, match.value)
end
def test_expression_sequence_terminal
match = File.parse('/./ /./', :root => :expression)
assert(match)
assert_instance_of(Sequence, match.value)
end
def test_expression_sequence_string_terminal
match = File.parse('"a" "b"', :root => :expression)
assert(match)
assert_instance_of(Sequence, match.value)
end
def test_expression_sequence_extension
match = File.parse('( "a" "b" ) <Module>', :root => :expression)
assert(match)
assert_instance_of(Sequence, match.value)
end
def test_expression_sequence_mixed
match = File.parse('"a" ("b" | /./)* <Module>', :root => :expression)
assert(match)
assert_instance_of(Sequence, match.value)
end
def test_expression_sequence_block
match = File.parse('"a" ("b" | /./)* {}', :root => :expression)
assert(match)
assert_instance_of(Sequence, match.value)
end
def test_precedence_sequence_before_choice
# Sequence should bind more tightly than Choice.
match = File.parse('"a" "b" | "c"', :root => :expression)
assert(match)
assert_instance_of(Choice, match.value)
end
def test_precedence_parentheses
# Parentheses should change binding precedence.
match = File.parse('"a" ("b" | "c")', :root => :expression)
assert(match)
assert_instance_of(Sequence, match.value)
end
def test_precedence_repeat_before_predicate
# Repeat should bind more tightly than AndPredicate.
match = File.parse("&'a'+", :root => :expression)
assert(match)
assert_instance_of(AndPredicate, match.value)
end
def test_sequence
match = File.parse('"" ""', :root => :sequence)
assert(match)
assert_instance_of(Sequence, match.value)
end
def test_sequence_embedded_choice
match = File.parse('"a" ("b" | "c")', :root => :sequence)
assert(match)
assert_instance_of(Sequence, match.value)
end
def test_labelled
match = File.parse('label:""', :root => :labelled)
assert(match)
assert_instance_of(StringTerminal, match.value)
end
def test_extended_tag
match = File.parse('"" <Module>', :root => :extended)
assert(match)
assert_kind_of(Rule, match.value)
assert_kind_of(Module, match.value.extension)
end
def test_extended_block
match = File.parse('"" {}', :root => :extended)
assert(match)
assert_kind_of(Rule, match.value)
assert_kind_of(Module, match.value.extension)
end
def test_prefix_and
match = File.parse('&""', :root => :prefix)
assert(match)
assert_instance_of(AndPredicate, match.value)
end
def test_prefix_not
match = File.parse('!""', :root => :prefix)
assert(match)
assert_instance_of(NotPredicate, match.value)
end
def test_prefix_but
match = File.parse('~""', :root => :prefix)
assert(match)
assert_instance_of(ButPredicate, match.value)
end
def test_suffix_plus
match = File.parse('""+', :root => :suffix)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_suffix_question
match = File.parse('""?', :root => :suffix)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_suffix_star
match = File.parse('""*', :root => :suffix)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_suffix_n_star
match = File.parse('""1*', :root => :suffix)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_suffix_star_n
match = File.parse('""*2', :root => :suffix)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_suffix_n_star_n
match = File.parse('""1*2', :root => :suffix)
assert(match)
assert_instance_of(Repeat, match.value)
end
def test_primary_alias
match = File.parse('rule_name', :root => :primary)
assert(match)
assert_instance_of(Alias, match.value)
end
def test_primary_string_terminal
match = File.parse('"a"', :root => :primary)
assert(match)
assert_instance_of(StringTerminal, match.value)
end
## Lexical syntax
def test_require
match = File.parse('require "some/file"', :root => :require)
assert(match)
assert_equal('some/file', match.value)
end
def test_require_no_space
match = File.parse('require"some/file"', :root => :require)
assert(match)
assert_equal('some/file', match.value)
end
def test_require_single_quoted
match = File.parse("require 'some/file'", :root => :require)
assert(match)
assert_equal('some/file', match.value)
end
def test_include
match = File.parse('include Module', :root => :include)
assert(match)
assert_equal(Module, match.value)
end
def test_include_colon_colon
match = File.parse('include ::Module', :root => :include)
assert(match)
assert_equal(Module, match.value)
end
def test_root
match = File.parse('root some_rule', :root => :root)
assert(match)
assert_equal('some_rule', match.value)
end
def test_root_invalid
assert_raise SyntaxError do
File.parse('root :a_root', :root => :root)
end
end
def test_rule_name
match = File.parse('some_rule', :root => :rule_name)
assert(match)
assert('some_rule', match.value)
end
def test_rule_name_space
match = File.parse('some_rule ', :root => :rule_name)
assert(match)
assert('some_rule', match.value)
end
def test_terminal_single_quoted_string
match = File.parse("'a'", :root => :terminal)
assert(match)
assert_instance_of(StringTerminal, match.value)
end
def test_terminal_double_quoted_string
match = File.parse('"a"', :root => :terminal)
assert(match)
assert_instance_of(StringTerminal, match.value)
end
def test_terminal_case_insensitive_string
match = File.parse('`a`', :root => :terminal)
assert(match)
assert_instance_of(StringTerminal, match.value)
end
def test_terminal_regular_expression
match = File.parse('/./', :root => :terminal)
assert(match)
assert_instance_of(Terminal, match.value)
end
def test_terminal_character_class
match = File.parse('[a-z]', :root => :terminal)
assert(match)
assert_instance_of(Terminal, match.value)
end
def test_terminal_dot
match = File.parse('.', :root => :terminal)
assert(match)
assert_instance_of(Terminal, match.value)
end
def test_single_quoted_string
match = File.parse("'test'", :root => :quoted_string)
assert(match)
assert_equal('test', match.value)
end
def test_single_quoted_string_embedded_single_quote
match = File.parse("'te\\'st'", :root => :quoted_string)
assert(match)
assert_equal("te'st", match.value)
end
def test_single_quoted_string_embedded_double_quote
match = File.parse("'te\"st'", :root => :quoted_string)
assert(match)
assert_equal('te"st', match.value)
end
def test_double_quoted_string
match = File.parse('"test"', :root => :quoted_string)
assert(match)
assert_equal('test', match.value)
end
def test_double_quoted_string_embedded_double_quote
match = File.parse('"te\\"st"', :root => :quoted_string)
assert(match)
assert_equal('te"st', match.value)
end
def test_double_quoted_string_embedded_single_quote
match = File.parse('"te\'st"', :root => :quoted_string)
assert(match)
assert_equal("te'st", match.value)
end
def test_double_quoted_string_hex
match = File.parse('"\\x26"', :root => :quoted_string)
assert(match)
assert_equal('&', match.value)
end
def test_case_insensitive_string
match = File.parse('`test`', :root => :case_insensitive_string)
assert(match)
assert_equal('test', match.value)
end
def test_case_insensitive_string_embedded_double_quote
match = File.parse('`te\\"st`', :root => :case_insensitive_string)
assert(match)
assert_equal('te"st', match.value)
end
def test_case_insensitive_string_embedded_backtick
match = File.parse('`te\`st`', :root => :case_insensitive_string)
assert(match)
assert_equal("te`st", match.value)
end
def test_case_insensitive_string_hex
match = File.parse('`\\x26`', :root => :case_insensitive_string)
assert(match)
assert_equal('&', match.value)
end
def test_regular_expression
match = File.parse('/./', :root => :regular_expression)
assert(match)
assert_equal(/./, match.value)
end
def test_regular_expression_escaped_forward_slash
match = File.parse('/\\//', :root => :regular_expression)
assert(match)
assert_equal(/\//, match.value)
end
def test_regular_expression_escaped_backslash
match = File.parse('/\\\\/', :root => :regular_expression)
assert(match)
assert_equal(/\\/, match.value)
end
def test_regular_expression_hex
match = File.parse('/\\x26/', :root => :regular_expression)
assert(match)
assert_equal(/\x26/, match.value)
end
def test_regular_expression_with_flag
match = File.parse('/a/i', :root => :regular_expression)
assert(match)
assert_equal(/a/i, match.value)
end
def test_character_class
match = File.parse('[_]', :root => :character_class)
assert(match)
assert_equal(/[_]/n, match.value)
end
def test_character_class_a_z
match = File.parse('[a-z]', :root => :character_class)
assert(match)
assert_equal(/[a-z]/n, match.value)
end
def test_character_class_nested_square_brackets
match = File.parse('[\[-\]]', :root => :character_class)
assert(match)
assert_equal(/[\[-\]]/n, match.value)
end
def test_character_class_hex_range
match = File.parse('[\\x26-\\x29]', :root => :character_class)
assert(match)
assert_equal(/[\x26-\x29]/, match.value)
end
def test_dot
match = File.parse('.', :root => :dot)
assert(match)
assert_equal(DOT, match.value)
end
def test_label
match = File.parse('label:', :root => :label)
assert(match)
assert_equal(:label, match.value)
end
def test_label_spaced
match = File.parse('a_label : ', :root => :label)
assert(match)
assert_equal(:a_label, match.value)
end
def test_tag
match = File.parse('<Module>', :root => :tag)
assert(match)
assert_equal(Module, match.value)
end
def test_tag_inner_space
match = File.parse('< Module >', :root => :tag)
assert(match)
assert_equal(Module, match.value)
end
def test_tag_space
match = File.parse('<Module> ', :root => :tag)
assert(match)
assert_equal(Module, match.value)
end
def test_block
match = File.parse('{}', :root => :block)
assert(match)
assert(match.value)
end
def test_block_space
match = File.parse("{} \n", :root => :block)
assert(match)
assert(match.value)
end
def test_block_n
match = File.parse('{ 2 }', :root => :block)
assert(match)
assert(match.value)
assert_equal(2, match.value.call)
end
def test_block_with_hash
match = File.parse("{ {:a => :b}\n}", :root => :block)
assert(match)
assert(match.value)
assert_equal({:a => :b}, match.value.call)
end
def test_block_proc
match = File.parse("{|b|\n Proc.new(&b)\n}", :root => :block)
assert(match)
assert(match.value)
b = match.value.call(Proc.new { :hi })
assert(b)
assert_equal(:hi, b.call)
end
def test_block_def
match = File.parse("{def value; 'a' end}", :root => :block)
assert(match)
assert(match.value)
assert_instance_of(Module, match.value)
method_names = match.value.instance_methods.map {|m| m.to_sym }
assert_equal([:value], method_names)
end
def test_block_def_multiline
match = File.parse("{\n def value\n 'a'\n end\n} ", :root => :block)
assert(match)
assert(match.value)
assert_instance_of(Module, match.value)
method_names = match.value.instance_methods.map {|m| m.to_sym }
assert_equal([:value], method_names)
end
def test_block_with_interpolation
match = File.parse('{ "#{number}" }', :root => :block)
assert(match)
assert(match.value)
end
def test_predicate_and
match = File.parse('&', :root => :predicate)
assert(match)
assert_instance_of(AndPredicate, match.value(''))
end
def test_predicate_not
match = File.parse('!', :root => :predicate)
assert(match)
assert_instance_of(NotPredicate, match.value(''))
end
def test_predicate_but
match = File.parse('~', :root => :predicate)
assert(match)
assert_instance_of(ButPredicate, match.value(''))
end
def test_and
match = File.parse('&', :root => :and)
assert(match)
assert_instance_of(AndPredicate, match.value(''))
end
def test_and_space
match = File.parse('& ', :root => :and)
assert(match)
assert_instance_of(AndPredicate, match.value(''))
end
def test_not
match = File.parse('!', :root => :not)
assert(match)
assert_instance_of(NotPredicate, match.value(''))
end
def test_not_space
match = File.parse('! ', :root => :not)
assert(match)
assert_instance_of(NotPredicate, match.value(''))
end
def test_but
match = File.parse('~', :root => :but)
assert(match)
assert_instance_of(ButPredicate, match.value(''))
end
def test_but_space
match = File.parse('~ ', :root => :but)
assert(match)
assert_instance_of(ButPredicate, match.value(''))
end
def test_repeat_question
match = File.parse('?', :root => :repeat)
assert(match)
assert_instance_of(Repeat, match.value(''))
end
def test_repeat_plus
match = File.parse('+', :root => :repeat)
assert(match)
assert_instance_of(Repeat, match.value(''))
end
def test_repeat_star
match = File.parse('*', :root => :repeat)
assert(match)
assert_instance_of(Repeat, match.value(''))
end
def test_repeat_n_star
match = File.parse('1*', :root => :repeat)
assert(match)
assert_instance_of(Repeat, match.value(''))
end
def test_repeat_star_n
match = File.parse('*2', :root => :repeat)
assert(match)
assert_instance_of(Repeat, match.value(''))
end
def test_repeat_n_star_n
match = File.parse('1*2', :root => :repeat)
assert(match)
assert_instance_of(Repeat, match.value(''))
end
def test_question
match = File.parse('?', :root => :question)
assert(match)
rule = match.value('')
assert_instance_of(Repeat, rule)
assert_equal(0, rule.min)
assert_equal(1, rule.max)
end
def test_question_space
match = File.parse('? ', :root => :question)
assert(match)
rule = match.value('')
assert_instance_of(Repeat, rule)
assert_equal(0, rule.min)
assert_equal(1, rule.max)
end
def test_plus
match = File.parse('+', :root => :plus)
assert(match)
rule = match.value('')
assert_instance_of(Repeat, rule)
assert_equal(1, rule.min)
assert_equal(Infinity, rule.max)
end
def test_plus_space
match = File.parse('+ ', :root => :plus)
assert(match)
rule = match.value('')
assert_instance_of(Repeat, rule)
assert_equal(1, rule.min)
assert_equal(Infinity, rule.max)
end
def test_star
match = File.parse('*', :root => :star)
assert(match)
rule = match.value('')
assert_instance_of(Repeat, rule)
assert_equal(0, rule.min)
assert_equal(Infinity, rule.max)
end
def test_n_star
match = File.parse('1*', :root => :star)
assert(match)
rule = match.value('')
assert_instance_of(Repeat, rule)
assert_equal(1, rule.min)
assert_equal(Infinity, rule.max)
end
def test_star_n
match = File.parse('*2', :root => :star)
assert(match)
rule = match.value('')
assert_instance_of(Repeat, rule)
assert_equal(0, rule.min)
assert_equal(2, rule.max)
end
def test_n_star_n
match = File.parse('1*2', :root => :star)
assert(match)
rule = match.value('')
assert_instance_of(Repeat, rule)
assert_equal(1, rule.min)
assert_equal(2, rule.max)
end
def test_n_star_n_space
match = File.parse('1*2 ', :root => :star)
assert(match)
rule = match.value('')
assert_instance_of(Repeat, rule)
assert_equal(1, rule.min)
assert_equal(2, rule.max)
end
def test_module_name
match = File.parse('Module', :root => :module_name)
assert(match)
assert_equal('Module', match)
end
def test_module_name_space
match = File.parse('Module ', :root => :module_name)
assert(match)
assert_equal('Module', match.first)
end
def test_module_name_colon_colon
match = File.parse('::Proc', :root => :module_name)
assert(match)
assert_equal('::Proc', match)
end
def test_constant
match = File.parse('Math', :root => :constant)
assert(match)
assert_equal('Math', match)
end
def test_constant_invalid
assert_raise SyntaxError do
File.parse('math', :root => :constant)
end
end
def test_comment
match = File.parse('# A comment.', :root => :comment)
assert(match)
assert_equal('# A comment.', match)
end
end