Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UnboundMethod#original_name and UnboundMethod#bind_call types #555

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions core/unbound_method.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,20 @@ class UnboundMethod
# if there is no method on superclass.
#
def super_method: () -> UnboundMethod?

# Returns the original name of the method.
#
# class C
# def foo; end
# alias bar foo
# end
# C.instance_method(:bar).original_name # => :foo
#
def original_name: () -> Symbol

# Bind *umeth* to *recv* and then invokes the method with the specified
# arguments. This is semantically equivalent to `umeth.bind(recv).call(args,
# ...)`.
#
def bind_call: (untyped recv, *untyped args) ?{ (*untyped) -> untyped } -> untyped
end
73 changes: 69 additions & 4 deletions test/stdlib/UnboundMethod_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,75 @@
require_relative "test_helper"

class UnboundMethodTest < StdlibTest
target UnboundMethod
class UnboundMethodTest < Test::Unit::TestCase
include TypeAssertions
testing "::UnboundMethod"

class TestClass
def m(a, m = 1, *rest, x, k: 1, **kwrest, &blk)
end

# to_s has super method
def to_s
''
end
end

def test_arity
assert_send_type "() -> Integer",
unbound_method, :arity
end

def test_bind
assert_send_type "(Object) -> Method",
unbound_method, :bind, 42
end

def test_name
assert_send_type "() -> Symbol",
unbound_method, :name
end

def test_owner
assert_send_type "() -> Module",
unbound_method, :owner
end

def test_parameters
42.method(:to_s).unbind.parameters
method(:test_parameters).unbind.parameters
assert_send_type "() -> Array[[ Symbol ]]",
unbound_method, :parameters
assert_send_type "() -> Array[[ Symbol, Symbol ]]",
TestClass.instance_method(:m), :parameters
end

def test_source_location
assert_send_type "() -> nil",
unbound_method, :source_location
assert_send_type "() -> [ String, Integer ]",
TestClass.instance_method(:m), :source_location
end

def test_super_method
assert_send_type "() -> nil",
TestClass.instance_method(:m), :super_method
assert_send_type "() -> UnboundMethod",
TestClass.instance_method(:to_s), :super_method
end

def test_original_name
assert_send_type "() -> Symbol",
unbound_method, :original_name
end

def test_bind_call
assert_send_type "(Integer) -> String",
unbound_method, :bind_call, 42
assert_send_type "(Integer, Integer) -> String",
unbound_method, :bind_call, 42, 16
assert_send_type "(UnboundMethodTest::TestClass, Integer, Integer, foo: String) { () -> void } -> nil",
TestClass.instance_method(:m), :bind_call, TestClass.new, 42, 43, foo: 'bar' do end
end

def unbound_method
Integer.instance_method(:to_s)
end
end