Skip to content

Commit

Permalink
Make AMC method name random
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Nov 23, 2020
1 parent 7a4ae2b commit 85e495e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
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

0 comments on commit 85e495e

Please sign in to comment.