Skip to content

Commit ff4b2c7

Browse files
authored
Merge pull request #489 from ruby/random-alias
Make AMC method name random
2 parents 7a4ae2b + 85e495e commit ff4b2c7

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

lib/rbs/test/hook.rb

+17-13
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,28 @@ module Hook
2727
:>> => "rshift",
2828
:~ => "tilda"
2929
}
30-
def self.alias_names(target)
30+
def self.alias_names(target, random)
31+
suffix = "#{RBS::Test.suffix}_#{random}"
32+
3133
case target
3234
when *OPERATORS.keys
3335
name = OPERATORS[target]
3436
[
35-
"#{name}____with__#{Test.suffix}",
36-
"#{name}____without__#{Test.suffix}"
37+
"#{name}____with__#{suffix}",
38+
"#{name}____without__#{suffix}"
3739
]
3840
else
3941
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
4042

4143
[
42-
"#{aliased_target}__with__#{Test.suffix}#{punctuation}",
43-
"#{aliased_target}__without__#{Test.suffix}#{punctuation}"
44+
"#{aliased_target}__with__#{suffix}#{punctuation}",
45+
"#{aliased_target}__without__#{suffix}#{punctuation}"
4446
]
4547
end
4648
end
4749

48-
def self.setup_alias_method_chain(klass, target)
49-
with_method, without_method = alias_names(target)
50+
def self.setup_alias_method_chain(klass, target, random:)
51+
with_method, without_method = alias_names(target, random)
5052

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

@@ -65,8 +67,8 @@ def self.setup_alias_method_chain(klass, target)
6567
end
6668
end
6769

68-
def self.hook_method_source(prefix, method_name, key)
69-
with_name, without_name = alias_names(method_name)
70+
def self.hook_method_source(prefix, method_name, key, random:)
71+
with_name, without_name = alias_names(method_name, random)
7072
full_method_name = "#{prefix}#{method_name}"
7173

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

162164
def self.hook_instance_method(klass, method, key:)
163-
line, source = hook_method_source("#{klass}#", method, key)
165+
random = SecureRandom.hex(4)
166+
line, source = hook_method_source("#{klass}#", method, key, random: random)
164167

165168
klass.module_eval(source, __FILE__, line)
166-
setup_alias_method_chain klass, method
169+
setup_alias_method_chain klass, method, random: random
167170
end
168171

169172
def self.hook_singleton_method(klass, method, key:)
170-
line, source = hook_method_source("#{klass}.",method, key)
173+
random = SecureRandom.hex(4)
174+
line, source = hook_method_source("#{klass}.",method, key, random: random)
171175

172176
klass.singleton_class.module_eval(source, __FILE__, line)
173-
setup_alias_method_chain klass.singleton_class, method
177+
setup_alias_method_chain klass.singleton_class, method, random: random
174178
end
175179
end
176180
end

test/rbs/test/runtime_test_test.rb

+29
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,35 @@ def foo(integer, &block)
218218
self + 3
219219
end
220220
RUBY
221+
end
222+
223+
def test_super
224+
assert_test_success(other_env: { 'RBS_TEST_TARGET' => 'Foo,Bar' }, rbs_content: <<RBS, ruby_content: <<'RUBY')
225+
class Foo
226+
def foo: (Integer) -> void
227+
end
228+
229+
class Bar
230+
def foo: (Integer) -> void
231+
end
232+
RBS
221233
234+
class Foo
235+
def foo(x)
236+
::RBS.logger.error("Foo#foo")
237+
puts "Foo#foo: x=#{x}"
238+
end
239+
end
240+
241+
class Bar < Foo
242+
def foo(x)
243+
::RBS.logger.error("Bar#foo")
244+
puts "Bar#foo: x=#{x}"
245+
super
246+
end
247+
end
248+
249+
Bar.new.foo(30)
250+
RUBY
222251
end
223252
end

0 commit comments

Comments
 (0)