diff --git a/test/rbi/formatter_test.rb b/test/rbi/formatter_test.rb index 96f23f20..f0b311c8 100644 --- a/test/rbi/formatter_test.rb +++ b/test/rbi/formatter_test.rb @@ -5,6 +5,8 @@ module RBI class FormatterTest < Minitest::Test + include TestHelper + def test_format_add_sig_templates rbi = <<~RBI module Foo @@ -13,7 +15,7 @@ def foo(a, b); end RBI file = File.new - file.root = Parser.parse_string(rbi) + file.root = parse_rbi(rbi) out = Formatter.new(add_sig_templates: false).print_file(file) assert_equal(rbi, out) @@ -38,7 +40,7 @@ def bar(a, b); end RBI file = File.new - file.root = Parser.parse_string(rbi) + file.root = parse_rbi(rbi) out = Formatter.new(group_nodes: false).print_file(file) assert_equal(rbi, out) @@ -63,7 +65,7 @@ def foo(a, b); end RBI file = File.new - file.root = Parser.parse_string(rbi) + file.root = parse_rbi(rbi) out = Formatter.new(max_line_length: nil).print_file(file) assert_equal(rbi, out) @@ -90,7 +92,7 @@ def self.foo; end RBI file = File.new - file.root = Parser.parse_string(rbi) + file.root = parse_rbi(rbi) out = Formatter.new(nest_singleton_methods: false).print_file(file) assert_equal(rbi, out) @@ -113,7 +115,7 @@ module Foo RBI file = File.new - file.root = Parser.parse_string(rbi) + file.root = parse_rbi(rbi) out = Formatter.new(nest_non_public_methods: false).print_file(file) assert_equal(rbi, out) @@ -137,7 +139,7 @@ def bar(a, b); end RBI file = File.new - file.root = Parser.parse_string(rbi) + file.root = parse_rbi(rbi) out = Formatter.new(sort_nodes: false).print_file(file) assert_equal(rbi, out) diff --git a/test/rbi/index_test.rb b/test/rbi/index_test.rb index ed05f3c0..47cc1ba5 100644 --- a/test/rbi/index_test.rb +++ b/test/rbi/index_test.rb @@ -5,6 +5,7 @@ module RBI class IndexTest < Minitest::Test + include TestHelper extend T::Sig def test_index_empty_trees @@ -14,7 +15,7 @@ def test_index_empty_trees end def test_index_scopes_and_consts - rbi = Parser.parse_string(<<~RBI) + rbi = parse_rbi(<<~RBI) class A module B module ::C; end @@ -38,7 +39,7 @@ class << self; end end def test_index_properties - rbi = Parser.parse_string(<<~RBI) + rbi = parse_rbi(<<~RBI) class A attr_reader :a, :b attr_writer :a, :b @@ -70,7 +71,7 @@ def foo(a); end end def test_index_sorbet_constructs - rbi = Parser.parse_string(<<~RBI) + rbi = parse_rbi(<<~RBI) class A < T::Struct const :a, Integer prop :b, String @@ -105,7 +106,7 @@ class B < T::Enum end def test_index_multiple_trees - tree1 = Parser.parse_string(<<~RBI) + tree1 = parse_rbi(<<~RBI) class A module B module ::C; end @@ -113,7 +114,7 @@ module ::C; end end RBI - tree2 = Parser.parse_string(<<~RBI) + tree2 = parse_rbi(<<~RBI) class A module B module ::C; end diff --git a/test/rbi/parser_test.rb b/test/rbi/parser_test.rb index f8368a20..42125237 100644 --- a/test/rbi/parser_test.rb +++ b/test/rbi/parser_test.rb @@ -5,6 +5,8 @@ module RBI class ParserTest < Minitest::Test + include TestHelper + def test_parse_scopes rbi = <<~RBI module Foo; end @@ -14,7 +16,7 @@ class ::Foo::Bar::Baz < ::Foo::Bar; end class << self; end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -29,7 +31,7 @@ class << self; end end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -49,7 +51,7 @@ def m2; end end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string) A = ::Struct.new B = ::Struct.new(:a, :b) @@ -82,7 +84,7 @@ def test_parse_constants A::B::C = Foo RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -99,7 +101,7 @@ def test_parse_attributes attr_accessor :a, :b, :c RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -110,7 +112,7 @@ def self.m2; end def m3(a, b = 42, *c, d:, e: "bar", **f, &g); end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -133,7 +135,7 @@ def test_parse_sigs def foo; end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string) sig { void } sig(:final) { void } @@ -160,7 +162,7 @@ def test_parse_methods_with_visibility private attr_reader :a RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string) private def m1; end protected def self.m2; end @@ -180,7 +182,7 @@ class Foo end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -194,7 +196,7 @@ def m2; end def m3; end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -209,7 +211,7 @@ def foo; end end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string) class Foo < ::T::Struct const :a, A @@ -233,7 +235,7 @@ def baz; end end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -247,7 +249,7 @@ def baz; end end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -262,7 +264,7 @@ class Foo end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -281,7 +283,7 @@ class Foo end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -291,7 +293,7 @@ module Foo; end class Bar; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) assert_equal("-:1:0-2:14", tree.loc.to_s) end @@ -303,7 +305,7 @@ class ActiveRecord::Base end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -315,7 +317,7 @@ class Baz < Bar; end class << self; end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string(print_locs: true)) # -:1:0-1:15 module Foo; end @@ -339,7 +341,7 @@ class << self; end end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string(print_locs: true)) # -:1:0-7:3 module Foo @@ -363,7 +365,7 @@ class Bar; end end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string(print_locs: true)) # -:1:0-4:3 Foo = ::Struct.new(:a) do @@ -383,7 +385,7 @@ def test_parse_constants_locations A::B::C = Foo RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string(print_locs: true)) # -:1:0-1:8 Foo = 42 @@ -409,7 +411,7 @@ def test_parse_attributes_locations attr_accessor :a, :b, :c RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string(print_locs: true)) # -:1:0-1:14 attr_reader :a @@ -443,7 +445,7 @@ def m3(a, b = 42, *c, d:, e: "bar", **f, &g); end def m4; end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string(print_locs: true)) # -:1:0-1:11 def m1; end @@ -473,7 +475,7 @@ class Foo end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string(print_locs: true)) # -:1:0-4:3 class Foo @@ -495,7 +497,7 @@ def m2; end def m3; end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string(print_locs: true)) # -:1:0-1:6 public @@ -523,7 +525,7 @@ def foo; end end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string(print_locs: true)) # -:1:0-7:3 class Foo < ::T::Struct @@ -553,7 +555,7 @@ def baz; end end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string(print_locs: true)) # -:1:0-8:3 class Foo < ::T::Enum @@ -580,7 +582,7 @@ class Foo end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string(print_locs: true)) # -:1:0-7:3 class Foo @@ -606,7 +608,7 @@ class Foo end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string(print_locs: true)) # -:1:0-4:3 class Foo @@ -630,7 +632,7 @@ def test_comments_in_empty_files # Preserving empty lines RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string) # typed: false # frozen_string_literal: true @@ -649,7 +651,7 @@ def test_parse_file_header module A; end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string) # typed: true @@ -664,7 +666,7 @@ def test_parse_file_headers module A; end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string) # typed: true # frozen_string_literal: true @@ -680,7 +682,7 @@ def test_parse_file_header_and_node_comment # A comment module A; end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string) # typed: true @@ -700,7 +702,7 @@ def test_parse_file_header_and_node_comments # for the module module A; end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string) # typed: true # frozen_string_literal: true @@ -731,7 +733,7 @@ def c(a); end end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -752,7 +754,7 @@ def baz; end end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string) module A # foo comment @@ -783,7 +785,7 @@ class Bar; end end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -796,7 +798,7 @@ def bar; end end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(rbi, out.string) end @@ -810,7 +812,7 @@ class B; end # A comment 3 end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string) # A comment 1 # A comment 2 @@ -827,7 +829,7 @@ def test_parse_collect_dangling_file_comments module A; end # Orphan comment RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string) module A; end # Orphan comment @@ -847,7 +849,7 @@ def foo( e: _ ); end RBI - out = Parser.parse_string(rbi) + out = parse_rbi(rbi) assert_equal(<<~RBI, out.string) def bar; end diff --git a/test/rbi/rewriters/annotate_test.rb b/test/rbi/rewriters/annotate_test.rb index 2fc6b866..5d3e210b 100644 --- a/test/rbi/rewriters/annotate_test.rb +++ b/test/rbi/rewriters/annotate_test.rb @@ -5,8 +5,10 @@ module RBI class AnnotateTest < Minitest::Test + include TestHelper + def test_add_annotation_to_root_nodes - rbi = Parser.parse_string(<<~RBI) + rbi = parse_rbi(<<~RBI) module A class B def m; end @@ -36,7 +38,7 @@ class C < T::Struct end def test_add_annotation_to_all_scopes - rbi = Parser.parse_string(<<~RBI) + rbi = parse_rbi(<<~RBI) module A FOO = type_member @@ -78,7 +80,7 @@ class C < T::Struct end def test_add_annotation_to_all_properties - rbi = Parser.parse_string(<<~RBI) + rbi = parse_rbi(<<~RBI) # Root scope are always annotated module A FOO = type_member @@ -132,7 +134,7 @@ class C < T::Struct end def test_add_annotation_to_all_nodes - rbi = Parser.parse_string(<<~RBI) + rbi = parse_rbi(<<~RBI) module A FOO = type_member @@ -183,7 +185,7 @@ class C < T::Struct end def test_does_not_reannotate_already_annotated_nodes - rbi = Parser.parse_string(<<~RBI) + rbi = parse_rbi(<<~RBI) # @test module A # @test @@ -209,7 +211,7 @@ def m1; end end def test_add_different_annotation_to_nodes - rbi = Parser.parse_string(<<~RBI) + rbi = parse_rbi(<<~RBI) # @test module A # @test diff --git a/test/rbi/rewriters/attr_to_methods_test.rb b/test/rbi/rewriters/attr_to_methods_test.rb index 7828d51b..c6c5d74e 100644 --- a/test/rbi/rewriters/attr_to_methods_test.rb +++ b/test/rbi/rewriters/attr_to_methods_test.rb @@ -5,6 +5,8 @@ module RBI class AttrToMethodsTest < Minitest::Test + include TestHelper + def test_replaces_attr_reader_with_method rbi = Parser.parse_string(<<~RBI) # Lorum ipsum... diff --git a/test/rbi/rewriters/deannotate_test.rb b/test/rbi/rewriters/deannotate_test.rb index ec27b111..2909baa1 100644 --- a/test/rbi/rewriters/deannotate_test.rb +++ b/test/rbi/rewriters/deannotate_test.rb @@ -5,8 +5,10 @@ module RBI class AnnotateTest < Minitest::Test + include TestHelper + def test_deannotate_nodes - rbi = Parser.parse_string(<<~RBI) + rbi = parse_rbi(<<~RBI) # @test module A # @test @@ -56,7 +58,7 @@ class C < T::Struct end def test_deannotate_only_removes_the_matching_annotation - rbi = Parser.parse_string(<<~RBI) + rbi = parse_rbi(<<~RBI) # @test # @other module A diff --git a/test/rbi/rewriters/filter_versions_test.rb b/test/rbi/rewriters/filter_versions_test.rb index 5c06e8ae..ab92796c 100644 --- a/test/rbi/rewriters/filter_versions_test.rb +++ b/test/rbi/rewriters/filter_versions_test.rb @@ -5,6 +5,8 @@ module RBI class FilterVersions < Minitest::Test + include TestHelper + def test_filter_versions_do_nothing rbi = <<~RBI class Foo; end @@ -13,7 +15,7 @@ class Baz; end class Buzz; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) Rewriters::FilterVersions.filter(tree, Gem::Version.new("0.4.0")) assert_equal(rbi, tree.string) @@ -34,7 +36,7 @@ class Baz; end class Buzz; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) tree.filter_versions!(Gem::Version.new("0.4.0")) assert_equal(<<~RBI, tree.string) @@ -58,7 +60,7 @@ class Baz; end class Buzz; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) tree.filter_versions!(Gem::Version.new("0.4.0")) assert_equal(<<~RBI, tree.string) @@ -85,7 +87,7 @@ class Baz; end class Buzz; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) tree.filter_versions!(Gem::Version.new("0.4.0")) assert_equal(<<~RBI, tree.string) @@ -109,7 +111,7 @@ class Baz; end class Buzz; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) tree.filter_versions!(Gem::Version.new("0.4.0")) assert_equal(<<~RBI, tree.string) @@ -136,7 +138,7 @@ class Bar; end class Baz; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) tree.filter_versions!(Gem::Version.new("1.1.5")) assert_equal(<<~RBI, tree.string) @@ -160,7 +162,7 @@ class Baz; end class Buzz; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) tree.filter_versions!(Gem::Version.new("0.4.0")) assert_equal(<<~RBI, tree.string) @@ -190,7 +192,7 @@ class Baz; end class Buzz; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) tree.filter_versions!(Gem::Version.new("0.4.0")) assert_equal(<<~RBI, tree.string) @@ -217,7 +219,7 @@ class Baz; end class Buzz; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) tree.filter_versions!(Gem::Version.new("0.4.0-prerelease")) assert_equal(<<~RBI, tree.string) @@ -244,7 +246,7 @@ class Bar; end class Baz; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) tree.filter_versions!(Gem::Version.new("0.4.0")) assert_equal(<<~RBI, tree.string) @@ -272,7 +274,7 @@ class Bar; end class Baz; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) tree.filter_versions!(Gem::Version.new("0.4.0")) assert_equal(<<~RBI, tree.string) @@ -297,7 +299,7 @@ class Bar; end class Baz; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) tree.filter_versions!(Gem::Version.new("0.4.0")) assert_equal(<<~RBI, tree.string) @@ -317,7 +319,7 @@ def test_filter_versions_parse_errors class Foo; end RBI - tree = Parser.parse_string(rbi) + tree = parse_rbi(rbi) assert_raises(Gem::Requirement::BadRequirementError) do tree.filter_versions!(Gem::Version.new("0.4.0")) diff --git a/test/rbi/rewriters/merge_trees_test.rb b/test/rbi/rewriters/merge_trees_test.rb index 3337daad..4164593c 100644 --- a/test/rbi/rewriters/merge_trees_test.rb +++ b/test/rbi/rewriters/merge_trees_test.rb @@ -5,6 +5,8 @@ module RBI class MergeTest < Minitest::Test + include TestHelper + def test_merge_empty_trees rbi1 = Tree.new rbi2 = Tree.new @@ -13,7 +15,7 @@ def test_merge_empty_trees end def test_merge_empty_tree_into_tree - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class Foo; end RBI @@ -28,7 +30,7 @@ class Foo; end def test_merge_tree_into_empty_tree rbi1 = Tree.new - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class Foo; end RBI @@ -39,12 +41,12 @@ class Foo; end end def test_merge_scopes_together - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A; end class B; end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class C; end class D; end RBI @@ -59,13 +61,13 @@ class D; end end def test_merge_nested_scopes_together - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A class B; end end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class C class D; end end @@ -84,13 +86,13 @@ class D; end end def test_merge_same_scopes_together - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A class B; end end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A class B; end class C; end @@ -107,14 +109,14 @@ class C; end end def test_merge_constants_together - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A A = 42 end B = 42 RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A A = 42 B = 42 @@ -134,7 +136,7 @@ class A end def test_merge_attributes_together - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A attr_reader :a attr_writer :a @@ -144,7 +146,7 @@ class A end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A attr_reader :a attr_writer :a @@ -168,7 +170,7 @@ class A end def test_merge_methods_together - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A def a; end def b; end @@ -178,7 +180,7 @@ def x(a = T.unsafe(nil), b: T.unsafe(nil)); end end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A def a; end def b; end @@ -202,7 +204,7 @@ def e(a); end end def test_merge_mixins_together - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A include A extend B @@ -212,7 +214,7 @@ class A end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A include A extend B @@ -236,14 +238,14 @@ class A end def test_merge_helpers_together - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A abstract! interface! end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A interface! sealed! @@ -261,14 +263,14 @@ class A end def test_merge_sends_together - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A foo :bar, :baz bar end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A foo :bar, :baz baz @@ -286,14 +288,14 @@ class A end def test_merge_type_members_together - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A Foo = type_member {{ fixed: Integer }} Bar = type_template {{ upper: String }} end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A Foo = type_member { { fixed: Integer } @@ -329,14 +331,14 @@ class A end def test_merge_structs_together - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A < T::Struct prop :a, Integer const :b, Integer end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A < T::Struct const :b, Integer prop :c, Integer @@ -354,7 +356,7 @@ class A < T::Struct end def test_merge_enums_together - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A < T::Enum enums do A = new @@ -363,7 +365,7 @@ class A < T::Enum end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A < T::Enum enums do B = new @@ -385,7 +387,7 @@ class A < T::Enum end def test_merge_signatures - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A def m1; end @@ -405,7 +407,7 @@ def m3; end end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A sig { returns(Integer) } def m1; end @@ -450,7 +452,7 @@ def m3; end end def test_merge_comments - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) # Comment A1 class A # Comment a1 @@ -460,7 +462,7 @@ def m; end end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) # Comment A1 # Comment A2 # Comment A3 @@ -491,13 +493,13 @@ def m; end end def test_merge_tree_comments_together - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) # typed: true # Some comments RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) # typed: true # Other comments @@ -513,7 +515,7 @@ def test_merge_tree_comments_together end def test_merge_create_conflict_tree_for_scopes - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class Foo A = 10 end @@ -527,7 +529,7 @@ class Baz < Foo end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) module Foo A = 10 end @@ -570,7 +572,7 @@ class Baz < Bar end def test_merge_create_conflict_tree_for_structs - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) A = Struct.new(:a) do def m; end end @@ -582,7 +584,7 @@ def m; end end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) A = Struct.new(:a) do def m1; end end @@ -622,14 +624,14 @@ def m; end end def test_merge_create_conflict_tree_for_constants - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class Foo A = 10 end B = 10 RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class Foo A = 42 end @@ -654,7 +656,7 @@ class Foo end def test_merge_create_conflict_tree_constants_and_scopes - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A; end module B; end module C; end @@ -664,7 +666,7 @@ module F; end end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) module A; end class B; end C = 42 @@ -702,7 +704,7 @@ module F; end end def test_merge_create_conflict_tree_for_attributes - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class Foo attr_accessor :a attr_accessor :b @@ -710,7 +712,7 @@ class Foo end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class Foo attr_reader :a attr_writer :b @@ -737,7 +739,7 @@ class Foo end def test_merge_create_conflict_tree_for_methods - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class Foo def m1; end def m2(a); end @@ -750,7 +752,7 @@ def m8(a: nil); end end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class Foo def m1(a); end def m2; end @@ -813,7 +815,7 @@ def m8(a: 10); end end def test_merge_create_conflict_tree_for_mixins - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A include A, B extend A, B @@ -821,7 +823,7 @@ class A end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A include B extend B @@ -846,7 +848,7 @@ class A end def test_merge_create_conflict_tree_for_sends - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A foo A bar :bar @@ -854,7 +856,7 @@ class A end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A foo B bar "bar" @@ -879,7 +881,7 @@ class A end def test_merge_create_conflict_tree_for_tstructs - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class A < T::Struct prop :a, Integer const :b, Integer @@ -888,7 +890,7 @@ class A < T::Struct end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class A < T::Struct const :a, Integer prop :b, Integer @@ -916,7 +918,7 @@ class A < T::Struct end def test_merge_create_conflict_tree_for_signatures - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class Foo sig { returns(Integer) } attr_reader :a @@ -932,7 +934,7 @@ def m3; end end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) class Foo sig { returns(String) } attr_reader :a @@ -981,14 +983,14 @@ def m3; end end def test_merge_return_the_list_of_conflicts - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) class Foo A = 10 end B = 10 RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) module Foo A = 42 end @@ -1005,7 +1007,7 @@ module Foo end def test_merge_keep_left - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) module Foo A = 10 @@ -1020,7 +1022,7 @@ def m3; end end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) module Foo A = 42 @@ -1055,7 +1057,7 @@ def m4; end end def test_merge_keep_right - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) module Foo A = 10 @@ -1070,7 +1072,7 @@ def m3; end end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) module Foo A = 42 @@ -1105,7 +1107,7 @@ def m4; end end def test_merge_trees_with_singleton_classes - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) module Foo class << self def m1; end @@ -1116,7 +1118,7 @@ def m2; end end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) module Foo def self.m1(x); end @@ -1140,7 +1142,7 @@ def m2; end end def test_merge_trees_with_singleton_classes_with_scope - rbi1 = Parser.parse_string(<<~RBI) + rbi1 = parse_rbi(<<~RBI) module Foo def self.m1(x); end @@ -1149,7 +1151,7 @@ def self.m2; end end RBI - rbi2 = Parser.parse_string(<<~RBI) + rbi2 = parse_rbi(<<~RBI) module Foo class << self def m1; end diff --git a/test/rbi/rewriters/remove_known_definitions_test.rb b/test/rbi/rewriters/remove_known_definitions_test.rb index cd0f8192..64f020a8 100644 --- a/test/rbi/rewriters/remove_known_definitions_test.rb +++ b/test/rbi/rewriters/remove_known_definitions_test.rb @@ -5,6 +5,8 @@ module RBI class RemoveKnownDefinitionsTest < Minitest::Test + include TestHelper + def test_remove_known_definitions_does_nothing_with_an_empty_index index = Index.new @@ -20,7 +22,7 @@ def bar; end BAZ = 42 RBI - original = Parser.parse_string(shim) + original = parse_rbi(shim) cleaned, operations = Rewriters::RemoveKnownDefinitions.remove(original, index) assert_equal(shim, cleaned.string) @@ -28,25 +30,25 @@ def bar; end end def test_remove_known_definitions_removes_all_definitions_found_in_index - tree1 = Parser.parse_string(<<~RBI) + tree1 = parse_rbi(<<~RBI) class Foo attr_reader :foo end RBI - tree2 = Parser.parse_string(<<~RBI) + tree2 = parse_rbi(<<~RBI) module Bar def bar; end end RBI - tree3 = Parser.parse_string(<<~RBI) + tree3 = parse_rbi(<<~RBI) BAZ = 42 RBI index = Index.index(tree1, tree2, tree3) - shim = Parser.parse_string(<<~RBI) + shim = parse_rbi(<<~RBI) class Foo attr_reader :foo end @@ -72,7 +74,7 @@ def bar; end end def test_remove_known_definitions_keeps_definitions_not_found_in_index - tree = Parser.parse_string(<<~RBI) + tree = parse_rbi(<<~RBI) class Foo def foo; end FOO = 42 @@ -81,7 +83,7 @@ def foo; end index = Index.index(tree) - shim = Parser.parse_string(<<~RBI) + shim = parse_rbi(<<~RBI) class Foo def foo; end def bar; end @@ -106,7 +108,7 @@ def bar; end end def test_remove_known_definitions_keeps_mismatching_definitions - tree = Parser.parse_string(<<~RBI) + tree = parse_rbi(<<~RBI) class Foo def foo; end end @@ -121,7 +123,7 @@ def foo; end index = Index.index(tree) - shim = Parser.parse_string(<<~RBI) + shim = parse_rbi(<<~RBI) class Foo def foo(x); end end @@ -139,25 +141,25 @@ module Bar; end end def test_remove_known_definitions_removes_empty_scopes - tree1 = Parser.parse_string(<<~RBI) + tree1 = parse_rbi(<<~RBI) class Foo attr_reader :foo end RBI - tree2 = Parser.parse_string(<<~RBI) + tree2 = parse_rbi(<<~RBI) class Foo def bar; end end RBI - tree3 = Parser.parse_string(<<~RBI) + tree3 = parse_rbi(<<~RBI) Foo::BAZ = 42 RBI index = Index.index(tree1, tree2, tree3) - shim = Parser.parse_string(<<~RBI) + shim = parse_rbi(<<~RBI) class Foo attr_reader :foo def bar; end @@ -178,11 +180,11 @@ def bar; end end def test_remove_known_definitions_keeps_empty_scopes_not_found_in_index - tree1 = Parser.parse_string(<<~RBI) + tree1 = parse_rbi(<<~RBI) class Foo; end RBI - tree2 = Parser.parse_string(<<~RBI) + tree2 = parse_rbi(<<~RBI) class Bar def bar; end end @@ -190,7 +192,7 @@ def bar; end index = Index.index(tree1, tree2) - shim = Parser.parse_string(<<~RBI) + shim = parse_rbi(<<~RBI) class Foo; end class Bar def bar; end @@ -212,7 +214,7 @@ class Baz; end end def test_remove_known_definitions_keeps_nodes_defined_with_a_signature - tree = Parser.parse_string(<<~RBI) + tree = parse_rbi(<<~RBI) class Foo def foo; end def self.bar; end @@ -222,7 +224,7 @@ def self.bar; end index = Index.index(tree) - shim = Parser.parse_string(<<~RBI) + shim = parse_rbi(<<~RBI) class Foo sig { void } def foo; end @@ -242,7 +244,7 @@ def self.bar; end end def test_remove_known_definitions_keeps_multiple_attributes - tree = Parser.parse_string(<<~RBI) + tree = parse_rbi(<<~RBI) class Foo attr_reader :foo end @@ -250,7 +252,7 @@ class Foo index = Index.index(tree) - shim = Parser.parse_string(<<~RBI) + shim = parse_rbi(<<~RBI) class Foo attr_reader :foo, :bar end @@ -263,7 +265,7 @@ class Foo end def test_remove_known_definitions_even_if_the_comments_differ - tree = Parser.parse_string(<<~RBI) + tree = parse_rbi(<<~RBI) class Foo def foo; end end @@ -271,7 +273,7 @@ def foo; end index = Index.index(tree) - shim = Parser.parse_string(<<~RBI) + shim = parse_rbi(<<~RBI) class Foo # Some comments def foo; end @@ -289,7 +291,7 @@ def foo; end end def test_remove_known_definitions_even_if_the_value_differ - tree = Parser.parse_string(<<~RBI) + tree = parse_rbi(<<~RBI) class Foo FOO = 42 end @@ -297,7 +299,7 @@ class Foo index = Index.index(tree) - shim = Parser.parse_string(<<~RBI) + shim = parse_rbi(<<~RBI) class Foo FOO = 24 end diff --git a/test/test_helper.rb b/test/test_helper.rb index 58cfaccb..c8ce668d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,3 +11,14 @@ unless ENV["RM_INFO"] Minitest::Reporters.use!(Minitest::Reporters::SpecReporter.new(color: true)) end + +module TestHelper + extend T::Sig + + private + + sig { params(rbs_string: String).returns(RBI::Tree) } + def parse_rbi(rbs_string) + RBI::Parser.parse_string(rbs_string) + end +end