Skip to content

Commit d9e2ef6

Browse files
committed
Extract assert_error_tolerant
1 parent f178ff3 commit d9e2ef6

File tree

1 file changed

+27
-100
lines changed

1 file changed

+27
-100
lines changed

test/ruby/test_ast.rb

Lines changed: 27 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -581,14 +581,10 @@ def m
581581
end
582582

583583
def test_error_tolerant_end_is_short_for_method_define
584-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
584+
assert_error_tolerant(<<~STR, <<~EXP)
585585
def m
586586
m2
587587
STR
588-
589-
str = ""
590-
PP.pp(node, str, 80)
591-
assert_equal(<<~EXP, str)
592588
(SCOPE@1:0-2:4
593589
tbl: []
594590
args: nil
@@ -615,14 +611,10 @@ def m
615611
end
616612

617613
def test_error_tolerant_end_is_short_for_singleton_method_define
618-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
614+
assert_error_tolerant(<<~STR, <<~EXP)
619615
def obj.m
620616
m2
621617
STR
622-
623-
str = ""
624-
PP.pp(node, str, 80)
625-
assert_equal(<<~EXP, str)
626618
(SCOPE@1:0-2:4
627619
tbl: []
628620
args: nil
@@ -647,43 +639,31 @@ def obj.m
647639
end
648640

649641
def test_error_tolerant_end_is_short_for_begin
650-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
642+
assert_error_tolerant(<<~STR, <<~EXP)
651643
begin
652644
a = 1
653645
STR
654-
655-
str = ""
656-
PP.pp(node, str, 80)
657-
assert_equal(<<~EXP, str)
658646
(SCOPE@1:0-2:7 tbl: [:a] args: nil body: (LASGN@2:2-2:7 :a (LIT@2:6-2:7 1)))
659647
EXP
660648
end
661649

662650
def test_error_tolerant_end_is_short_for_if
663-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
651+
assert_error_tolerant(<<~STR, <<~EXP)
664652
if cond
665653
a = 1
666654
STR
667-
668-
str = ""
669-
PP.pp(node, str, 80)
670-
assert_equal(<<~EXP, str)
671655
(SCOPE@1:0-2:7
672656
tbl: [:a]
673657
args: nil
674658
body:
675659
(IF@1:0-2:7 (VCALL@1:3-1:7 :cond) (LASGN@2:2-2:7 :a (LIT@2:6-2:7 1)) nil))
676660
EXP
677661

678-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
662+
assert_error_tolerant(<<~STR, <<~EXP)
679663
if cond
680664
a = 1
681665
else
682666
STR
683-
684-
str = ""
685-
PP.pp(node, str, 80)
686-
assert_equal(<<~EXP, str)
687667
(SCOPE@1:0-3:5
688668
tbl: [:a]
689669
args: nil
@@ -694,14 +674,10 @@ def test_error_tolerant_end_is_short_for_if
694674
end
695675

696676
def test_error_tolerant_end_is_short_for_unless
697-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
677+
assert_error_tolerant(<<~STR, <<~EXP)
698678
unless cond
699679
a = 1
700680
STR
701-
702-
str = ""
703-
PP.pp(node, str, 80)
704-
assert_equal(<<~EXP, str)
705681
(SCOPE@1:0-2:7
706682
tbl: [:a]
707683
args: nil
@@ -710,15 +686,11 @@ def test_error_tolerant_end_is_short_for_unless
710686
nil))
711687
EXP
712688

713-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
689+
assert_error_tolerant(<<~STR, <<~EXP)
714690
unless cond
715691
a = 1
716692
else
717693
STR
718-
719-
str = ""
720-
PP.pp(node, str, 80)
721-
assert_equal(<<~EXP, str)
722694
(SCOPE@1:0-3:5
723695
tbl: [:a]
724696
args: nil
@@ -729,14 +701,10 @@ def test_error_tolerant_end_is_short_for_unless
729701
end
730702

731703
def test_error_tolerant_end_is_short_for_while
732-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
704+
assert_error_tolerant(<<~STR, <<~EXP)
733705
while true
734706
m
735707
STR
736-
737-
str = ""
738-
PP.pp(node, str, 80)
739-
assert_equal(<<~EXP, str)
740708
(SCOPE@1:0-2:3
741709
tbl: []
742710
args: nil
@@ -745,14 +713,10 @@ def test_error_tolerant_end_is_short_for_while
745713
end
746714

747715
def test_error_tolerant_end_is_short_for_until
748-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
716+
assert_error_tolerant(<<~STR, <<~EXP)
749717
until true
750718
m
751719
STR
752-
753-
str = ""
754-
PP.pp(node, str, 80)
755-
assert_equal(<<~EXP, str)
756720
(SCOPE@1:0-2:3
757721
tbl: []
758722
args: nil
@@ -761,14 +725,10 @@ def test_error_tolerant_end_is_short_for_until
761725
end
762726

763727
def test_error_tolerant_end_is_short_for_case
764-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
728+
assert_error_tolerant(<<~STR, <<~EXP)
765729
case a
766730
when 1
767731
STR
768-
769-
str = ""
770-
PP.pp(node, str, 80)
771-
assert_equal(<<~EXP, str)
772732
(SCOPE@1:0-2:6
773733
tbl: []
774734
args: nil
@@ -779,14 +739,10 @@ def test_error_tolerant_end_is_short_for_case
779739
EXP
780740

781741

782-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
742+
assert_error_tolerant(<<~STR, <<~EXP)
783743
case
784744
when a == 1
785745
STR
786-
787-
str = ""
788-
PP.pp(node, str, 80)
789-
assert_equal(<<~EXP, str)
790746
(SCOPE@1:0-2:11
791747
tbl: []
792748
args: nil
@@ -800,14 +756,10 @@ def test_error_tolerant_end_is_short_for_case
800756
EXP
801757

802758

803-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
759+
assert_error_tolerant(<<~STR, <<~EXP)
804760
case a
805761
in {a: String}
806762
STR
807-
808-
str = ""
809-
PP.pp(node, str, 80)
810-
assert_equal(<<~EXP, str)
811763
(SCOPE@1:0-2:14
812764
tbl: []
813765
args: nil
@@ -824,14 +776,10 @@ def test_error_tolerant_end_is_short_for_case
824776
end
825777

826778
def test_error_tolerant_end_is_short_for_for
827-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
779+
assert_error_tolerant(<<~STR, <<~EXP)
828780
for i in ary
829781
m
830782
STR
831-
832-
str = ""
833-
PP.pp(node, str, 80)
834-
assert_equal(<<~EXP, str)
835783
(SCOPE@1:0-2:3
836784
tbl: [:i]
837785
args: nil
@@ -856,13 +804,9 @@ def test_error_tolerant_end_is_short_for_for
856804
end
857805

858806
def test_error_tolerant_end_is_short_for_class
859-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
807+
assert_error_tolerant(<<~STR, <<~EXP)
860808
class C
861809
STR
862-
863-
str = ""
864-
PP.pp(node, str, 80)
865-
assert_equal(<<~EXP, str)
866810
(SCOPE@1:0-1:7
867811
tbl: []
868812
args: nil
@@ -873,13 +817,9 @@ class C
873817
end
874818

875819
def test_error_tolerant_end_is_short_for_module
876-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
820+
assert_error_tolerant(<<~STR, <<~EXP)
877821
module M
878822
STR
879-
880-
str = ""
881-
PP.pp(node, str, 80)
882-
assert_equal(<<~EXP, str)
883823
(SCOPE@1:0-1:8
884824
tbl: []
885825
args: nil
@@ -890,14 +830,10 @@ module M
890830
end
891831

892832
def test_error_tolerant_end_is_short_for_do
893-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
833+
assert_error_tolerant(<<~STR, <<~EXP)
894834
m do
895835
a
896836
STR
897-
898-
str = ""
899-
PP.pp(node, str, 80)
900-
assert_equal(<<~EXP, str)
901837
(SCOPE@1:0-2:3
902838
tbl: []
903839
args: nil
@@ -908,14 +844,10 @@ def test_error_tolerant_end_is_short_for_do
908844
end
909845

910846
def test_error_tolerant_end_is_short_for_do_block
911-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
847+
assert_error_tolerant(<<~STR, <<~EXP)
912848
m 1 do
913849
a
914850
STR
915-
916-
str = ""
917-
PP.pp(node, str, 80)
918-
assert_equal(<<~EXP, str)
919851
(SCOPE@1:0-2:3
920852
tbl: []
921853
args: nil
@@ -926,14 +858,10 @@ def test_error_tolerant_end_is_short_for_do_block
926858
end
927859

928860
def test_error_tolerant_end_is_short_for_do_LAMBDA
929-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
861+
assert_error_tolerant(<<~STR, <<~EXP)
930862
-> do
931863
a
932864
STR
933-
934-
str = ""
935-
PP.pp(node, str, 80)
936-
assert_equal(<<~EXP, str)
937865
(SCOPE@1:0-2:3
938866
tbl: []
939867
args: nil
@@ -958,7 +886,7 @@ def test_error_tolerant_end_is_short_for_do_LAMBDA
958886
end
959887

960888
def test_error_tolerant_treat_end_as_keyword_based_on_indent
961-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
889+
assert_error_tolerant(<<~STR, <<~EXP)
962890
module Z
963891
class Foo
964892
foo.
@@ -968,10 +896,6 @@ def bar
968896
end
969897
end
970898
STR
971-
972-
str = ""
973-
PP.pp(node, str, 80)
974-
assert_equal(<<~EXP, str)
975899
(SCOPE@1:0-8:3
976900
tbl: []
977901
args: nil
@@ -1009,15 +933,11 @@ def bar
1009933
end
1010934

1011935
def test_error_tolerant_expr_value_can_be_error
1012-
node = RubyVM::AbstractSyntaxTree.parse(<<~STR, error_tolerant: true)
936+
assert_error_tolerant(<<~STR, <<~EXP)
1013937
def m
1014938
if
1015939
end
1016940
STR
1017-
1018-
str = ""
1019-
PP.pp(node, str, 80)
1020-
assert_equal(<<~EXP, str)
1021941
(SCOPE@1:0-3:3
1022942
tbl: []
1023943
args: nil
@@ -1042,4 +962,11 @@ def m
1042962
body: (IF@2:2-3:3 (ERROR@3:0-3:3) nil nil))))
1043963
EXP
1044964
end
965+
966+
def assert_error_tolerant(src, expected)
967+
node = RubyVM::AbstractSyntaxTree.parse(src, error_tolerant: true)
968+
str = ""
969+
PP.pp(node, str, 80)
970+
assert_equal(expected, str)
971+
end
1045972
end

0 commit comments

Comments
 (0)