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

Make AMC method name random #489

Merged
merged 1 commit into from
Nov 24, 2020
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
30 changes: 17 additions & 13 deletions lib/rbs/test/hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,28 @@ module Hook
:>> => "rshift",
:~ => "tilda"
}
def self.alias_names(target)
def self.alias_names(target, random)
suffix = "#{RBS::Test.suffix}_#{random}"

case target
when *OPERATORS.keys
name = OPERATORS[target]
[
"#{name}____with__#{Test.suffix}",
"#{name}____without__#{Test.suffix}"
"#{name}____with__#{suffix}",
"#{name}____without__#{suffix}"
]
else
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1

[
"#{aliased_target}__with__#{Test.suffix}#{punctuation}",
"#{aliased_target}__without__#{Test.suffix}#{punctuation}"
"#{aliased_target}__with__#{suffix}#{punctuation}",
"#{aliased_target}__without__#{suffix}#{punctuation}"
]
end
end

def self.setup_alias_method_chain(klass, target)
with_method, without_method = alias_names(target)
def self.setup_alias_method_chain(klass, target, random:)
with_method, without_method = alias_names(target, random)

RBS.logger.debug "alias name: #{target}, #{with_method}, #{without_method}"

Expand All @@ -65,8 +67,8 @@ def self.setup_alias_method_chain(klass, target)
end
end

def self.hook_method_source(prefix, method_name, key)
with_name, without_name = alias_names(method_name)
def self.hook_method_source(prefix, method_name, key, random:)
with_name, without_name = alias_names(method_name, random)
full_method_name = "#{prefix}#{method_name}"

[__LINE__ + 1, <<RUBY]
Expand Down Expand Up @@ -160,17 +162,19 @@ def #{with_name}(*args, &block)
end

def self.hook_instance_method(klass, method, key:)
line, source = hook_method_source("#{klass}#", method, key)
random = SecureRandom.hex(4)
line, source = hook_method_source("#{klass}#", method, key, random: random)

klass.module_eval(source, __FILE__, line)
setup_alias_method_chain klass, method
setup_alias_method_chain klass, method, random: random
end

def self.hook_singleton_method(klass, method, key:)
line, source = hook_method_source("#{klass}.",method, key)
random = SecureRandom.hex(4)
line, source = hook_method_source("#{klass}.",method, key, random: random)

klass.singleton_class.module_eval(source, __FILE__, line)
setup_alias_method_chain klass.singleton_class, method
setup_alias_method_chain klass.singleton_class, method, random: random
end
end
end
Expand Down
29 changes: 29 additions & 0 deletions test/rbs/test/runtime_test_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,35 @@ def foo(integer, &block)
self + 3
end
RUBY
end

def test_super
assert_test_success(other_env: { 'RBS_TEST_TARGET' => 'Foo,Bar' }, rbs_content: <<RBS, ruby_content: <<'RUBY')
class Foo
def foo: (Integer) -> void
end

class Bar
def foo: (Integer) -> void
end
RBS

class Foo
def foo(x)
::RBS.logger.error("Foo#foo")
puts "Foo#foo: x=#{x}"
end
end

class Bar < Foo
def foo(x)
::RBS.logger.error("Bar#foo")
puts "Bar#foo: x=#{x}"
super
end
end

Bar.new.foo(30)
RUBY
end
end