Skip to content

Commit

Permalink
Make more tests using parsing for their input
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
  • Loading branch information
Morriar committed Aug 6, 2024
1 parent 5960557 commit 9c6841c
Show file tree
Hide file tree
Showing 5 changed files with 475 additions and 447 deletions.
98 changes: 48 additions & 50 deletions test/rbi/rewriters/add_sig_templates_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,44 @@

module RBI
class AddSigTemplatesTest < Minitest::Test
include TestHelper

def test_do_not_add_sig_if_already_one
rbi = Tree.new
tree = parse_rbi(<<~RBI)
sig { returns(Integer) }
attr_reader :a
rbi << AttrReader.new(:a) do |attr|
attr.sigs << Sig.new(return_type: "Integer")
end
sig { returns(Integer) }
def foo; end
rbi << Method.new("m") do |meth|
meth.sigs << Sig.new(return_type: "Integer")
end
def bar; end
RBI

rbi.add_sig_templates!(with_todo_comment: true)
tree.add_sig_templates!(with_todo_comment: true)

assert_equal(<<~RBI, rbi.string)
assert_equal(<<~RBI, tree.string)
sig { returns(Integer) }
attr_reader :a
sig { returns(Integer) }
def m; end
def foo; end
# TODO: fill in signature with appropriate type information
sig { returns(T.untyped) }
def bar; end
RBI
end

def test_add_empty_sigs_to_attributes
rbi = Tree.new
rbi << AttrReader.new(:a1)
rbi << AttrWriter.new(:a2)
rbi << AttrAccessor.new(:a3)
tree = parse_rbi(<<~RBI)
attr_reader :a1
attr_writer :a2
attr_accessor :a3
RBI

rbi.add_sig_templates!(with_todo_comment: false)
tree.add_sig_templates!(with_todo_comment: false)

assert_equal(<<~RBI, rbi.string)
assert_equal(<<~RBI, tree.string)
sig { returns(T.untyped) }
attr_reader :a1
Expand All @@ -48,14 +55,15 @@ def test_add_empty_sigs_to_attributes
end

def test_add_empty_sigs_and_todo_comment_to_attributes
rbi = Tree.new
rbi << AttrReader.new(:a1)
rbi << AttrWriter.new(:a2)
rbi << AttrAccessor.new(:a3)
tree = parse_rbi(<<~RBI)
attr_reader :a1
attr_writer :a2
attr_accessor :a3
RBI

rbi.add_sig_templates!(with_todo_comment: true)
tree.add_sig_templates!(with_todo_comment: true)

assert_equal(<<~RBI, rbi.string)
assert_equal(<<~RBI, tree.string)
# TODO: fill in signature with appropriate type information
sig { returns(T.untyped) }
attr_reader :a1
Expand All @@ -71,20 +79,15 @@ def test_add_empty_sigs_and_todo_comment_to_attributes
end

def test_add_empty_sigs_to_methods
rbi = Tree.new
rbi << Method.new("m1")
rbi << Method.new("m2") do |meth|
meth << ReqParam.new("x")
end
rbi << Method.new("m3", is_singleton: true) do |meth|
meth << ReqParam.new("x")
meth << OptParam.new("y", "42")
meth << KwRestParam.new("z")
end

rbi.add_sig_templates!(with_todo_comment: false)

assert_equal(<<~RBI, rbi.string)
tree = parse_rbi(<<~RBI)
def m1; end
def m2(x); end
def self.m3(x, y = 42, **z); end
RBI

tree.add_sig_templates!(with_todo_comment: false)

assert_equal(<<~RBI, tree.string)
sig { returns(T.untyped) }
def m1; end
Expand All @@ -97,20 +100,15 @@ def self.m3(x, y = 42, **z); end
end

def test_add_empty_sigs_and_todo_comment_to_methods
rbi = Tree.new
rbi << Method.new("m1")
rbi << Method.new("m2") do |meth|
meth << ReqParam.new("x")
end
rbi << Method.new("m3", is_singleton: true) do |meth|
meth << ReqParam.new("x")
meth << OptParam.new("y", "42")
meth << KwRestParam.new("z")
end

rbi.add_sig_templates!(with_todo_comment: true)

assert_equal(<<~RBI, rbi.string)
tree = parse_rbi(<<~RBI)
def m1; end
def m2(x); end
def self.m3(x, y = 42, **z); end
RBI

tree.add_sig_templates!(with_todo_comment: true)

assert_equal(<<~RBI, tree.string)
# TODO: fill in signature with appropriate type information
sig { returns(T.untyped) }
def m1; end
Expand Down
Loading

0 comments on commit 9c6841c

Please sign in to comment.