| | 102 | |
|---|
| | 103 | module BarMethodAliaser |
|---|
| | 104 | def self.included(foo_class) |
|---|
| | 105 | foo_class.alias_method_chain :bar, :baz |
|---|
| | 106 | end |
|---|
| | 107 | |
|---|
| | 108 | def bar_with_baz |
|---|
| | 109 | bar_without_baz << '_with_baz' |
|---|
| | 110 | end |
|---|
| | 111 | |
|---|
| | 112 | def quux_with_baz |
|---|
| | 113 | quux_without_baz << '_with_baz' |
|---|
| | 114 | end |
|---|
| | 115 | end |
|---|
| | 116 | |
|---|
| | 117 | class MethodAliasingTest < Test::Unit::TestCase |
|---|
| | 118 | |
|---|
| | 119 | def setup |
|---|
| | 120 | Object.const_set(:FooClassWithBarMethod, Class.new) |
|---|
| | 121 | FooClassWithBarMethod.send(:define_method, 'bar', Proc.new { 'bar' }) |
|---|
| | 122 | @instance = FooClassWithBarMethod.new |
|---|
| | 123 | end |
|---|
| | 124 | |
|---|
| | 125 | def teardown |
|---|
| | 126 | Object.send(:remove_const, :FooClassWithBarMethod) |
|---|
| | 127 | end |
|---|
| | 128 | |
|---|
| | 129 | def test_alias_method_chain |
|---|
| | 130 | assert @instance.respond_to? :bar |
|---|
| | 131 | feature_aliases = [:bar_with_baz, :bar_without_baz] |
|---|
| | 132 | |
|---|
| | 133 | feature_aliases.each do |method| |
|---|
| | 134 | assert !@instance.respond_to?(method) |
|---|
| | 135 | end |
|---|
| | 136 | |
|---|
| | 137 | assert_equal 'bar', @instance.bar |
|---|
| | 138 | |
|---|
| | 139 | FooClassWithBarMethod.send(:include, BarMethodAliaser) |
|---|
| | 140 | |
|---|
| | 141 | feature_aliases.each do |method| |
|---|
| | 142 | assert @instance.respond_to?(method) |
|---|
| | 143 | end |
|---|
| | 144 | |
|---|
| | 145 | assert_equal 'bar_with_baz', @instance.bar |
|---|
| | 146 | assert_equal 'bar', @instance.bar_without_baz |
|---|
| | 147 | end |
|---|
| | 148 | |
|---|
| | 149 | def test_alias_method_chain_with_punctuation_method |
|---|
| | 150 | FooClassWithBarMethod.send(:define_method, 'quux!', Proc.new { 'quux' }) |
|---|
| | 151 | assert !@instance.respond_to?(:quux_with_baz) |
|---|
| | 152 | FooClassWithBarMethod.send(:include, BarMethodAliaser) |
|---|
| | 153 | FooClassWithBarMethod.alias_method_chain :quux!, :baz |
|---|
| | 154 | assert @instance.respond_to?(:quux_with_baz) |
|---|
| | 155 | |
|---|
| | 156 | assert_equal 'quux_with_baz', @instance.quux! |
|---|
| | 157 | assert_equal 'quux', @instance.quux_without_baz |
|---|
| | 158 | end |
|---|
| | 159 | end |
|---|