Skip to content
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
13 changes: 13 additions & 0 deletions lib/rbs/definition_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,19 @@ def build_singleton(type_name)
.update(type_params: class_params + method_type.type_params)
end

method_type = method_type.map_type do |type|
case type
when Types::Bases::Self
Types::ClassInstance.new(
name: type_name,
args: entry.type_params.map {|param| Types::Variable.new(name: param.name, location: param.location) },
location: nil
)
else
type
end
end

method_type = method_type.update(
type: method_type.type.with_return_type(
Types::ClassInstance.new(
Expand Down
51 changes: 39 additions & 12 deletions test/rbs/definition_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,18 @@ class DirectPublic
public def initialize_dup: (self) -> self
public def respond_to_missing?: () -> bool
end

class SelfInInitialize
def initialize: () { (self, class, instance) -> void } -> void
end

class SelfInInitializeWithTypeParams[T]
def initialize: () { (self, class, instance) -> T } -> void
end

class InheritedSelfInInitialize < SelfInInitialize
def initialize: () { (self, class, instance) -> void } -> void
end
EOF

manager.build do |env|
Expand Down Expand Up @@ -1196,6 +1208,21 @@ class DirectPublic
assert_method_definition definition.methods[:initialize_dup], ["(self) -> self"], accessibility: :public
assert_method_definition definition.methods[:respond_to_missing?], ["() -> bool"], accessibility: :public
end

builder.build_singleton(type_name("::SelfInInitialize")).tap do |definition|
assert_instance_of Definition, definition
assert_method_definition definition.methods[:new], ["() { (::SelfInInitialize, class, instance) -> void } -> ::SelfInInitialize"], accessibility: :public
end

builder.build_singleton(type_name("::SelfInInitializeWithTypeParams")).tap do |definition|
assert_instance_of Definition, definition
assert_method_definition definition.methods[:new], ["[T] () { (::SelfInInitializeWithTypeParams[T], class, instance) -> T } -> ::SelfInInitializeWithTypeParams[T]"], accessibility: :public
end

builder.build_singleton(type_name("::InheritedSelfInInitialize")).tap do |definition|
assert_instance_of Definition, definition
assert_method_definition definition.methods[:new], ["() { (::InheritedSelfInInitialize, class, instance) -> void } -> ::InheritedSelfInInitialize"], accessibility: :public
end
end
end
end
Expand Down Expand Up @@ -3641,30 +3668,30 @@ def test_inline_instance_variable_declarations
class Person
# @rbs @name: String
# @rbs @age: Integer?

def initialize(name, age)
@name = name
@age = age
end
end
RUBY

manager.build do |env|
builder = DefinitionBuilder.new(env: env)

builder.build_instance(type_name("::Person")).tap do |definition|
assert_instance_of Definition, definition

# Verify instance variables are present
assert_equal [:@name, :@age].sort, definition.instance_variables.keys.sort

# Check @name type
definition.instance_variables[:@name].tap do |variable|
assert_instance_of Definition::Variable, variable
assert_equal parse_type("::String"), variable.type
assert_equal type_name("::Person"), variable.declared_in
end

# Check @age type
definition.instance_variables[:@age].tap do |variable|
assert_instance_of Definition::Variable, variable
Expand All @@ -3682,30 +3709,30 @@ def test_inline_instance_variable_with_complex_types
class Container
# @rbs @items: Array[String]
# @rbs @metadata: Integer

def initialize
@items = ""
@metadata = 42
end
end
RUBY

manager.build do |env|
builder = DefinitionBuilder.new(env: env)

builder.build_instance(type_name("::Container")).tap do |definition|
assert_instance_of Definition, definition

# Verify instance variables are present
assert_equal [:@items, :@metadata].sort, definition.instance_variables.keys.sort

# Check @items type
definition.instance_variables[:@items].tap do |variable|
assert_instance_of Definition::Variable, variable
assert_equal "Array[::String]", variable.type.to_s
assert_equal type_name("::Container"), variable.declared_in
end

# Check @metadata type
definition.instance_variables[:@metadata].tap do |variable|
assert_instance_of Definition::Variable, variable
Expand Down
5 changes: 4 additions & 1 deletion test/stdlib/OpenSSL_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ def test_generate_prime
OpenSSL::BN, :generate_prime, 3
end


def test_new
assert_send_type "(::OpenSSL::BN) -> OpenSSL::BN",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the changes

Failure: test_new(OpenSSLBNSingletonTest):
  Call trace does not match one of method definitions:
    #<struct RBS::Test::CallTrace method_name=:new, method_call=<RBS::Test::ArgumentsReturn:@arguments: [#<OpenSSL::BN:0x00000001279b8b38>], @exit_value: #<OpenSSL::BN:0x0000000127951e10>, @exit_type: :return>, block_calls=[], block_given=false>
    (self) -> ::OpenSSL::BN | (::Integer) -> ::OpenSSL::BN | (::string) -> ::OpenSSL::BN | (::string, 0 | 2 | 10 | 16) -> ::OpenSSL::BN.
  <false> is not true.
/Users/ksss/src/github.com/ksss/rbs/lib/rbs/unit_test/type_assertions.rb:187:in 'block in RBS::UnitTest::TypeAssertions#assert_send_type'
/Users/ksss/src/github.com/ksss/rbs/lib/rbs/unit_test/type_assertions.rb:168:in 'RBS::UnitTest::TypeAssertions#send_setup'
/Users/ksss/src/github.com/ksss/rbs/lib/rbs/unit_test/type_assertions.rb:172:in 'RBS::UnitTest::TypeAssertions#assert_send_type'
test/stdlib/OpenSSL_test.rb:237:in 'OpenSSLBNSingletonTest#test_new'
     234:   end
     235:
     236:   def test_new
  => 237:     assert_send_type "(::OpenSSL::BN) -> OpenSSL::BN",
     238:       OpenSSL::BN, :new, OpenSSL::BN.new(3)
     239:   end
     240: end

OpenSSL::BN, :new, OpenSSL::BN.new(3)
end
end

class OpenSSLBNTest < Test::Unit::TestCase
Expand Down