Permalink
Browse files

Make alias_method_chain complain about duplicate aliases. Previously

repeated calls to alias_method_chain would cause infinite loops.

Signed-Off-By: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
jbarnette authored and NZKoz committed Apr 28, 2008
1 parent 850aba5 commit be48cad9a41ebf88ae19f2381a57917d532734c9
@@ -28,6 +28,10 @@ def alias_method_chain(target, feature)
with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
+ if method_defined?(without_method) || private_method_defined?(without_method)
+ raise NameError, "#{with_method} is already in #{target}'s alias chain."
+ end
+
alias_method without_method, target
alias_method target, with_method
@@ -166,6 +166,19 @@ def test_alias_method_chain
assert_equal 'bar_with_baz', @instance.bar
assert_equal 'bar', @instance.bar_without_baz
end
+
+ def test_alias_method_chain_complains_the_second_time
+ FooClassWithBarMethod.class_eval do
+ def bar_with_magic; end
+ alias_method_chain :bar, :magic
+ end
+
+ assert_raise(NameError) do
+ FooClassWithBarMethod.class_eval do
+ alias_method_chain :bar, :magic
+ end
+ end
+ end
def test_alias_method_chain_with_punctuation_method
FooClassWithBarMethod.class_eval do
@@ -289,5 +302,5 @@ def duck; 'duck' end
assert_equal 'duck_with_orange', @instance.duck
assert FooClassWithBarMethod.public_method_defined?(:duck)
- end
+ end
end

0 comments on commit be48cad

Please sign in to comment.