1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
require 'abstract_unit'
require 'active_support/core_ext/object/public_send'
module PublicSendReceiver
def receive_public_method(*args)
return args + [yield]
end
protected
def receive_protected_method(*args)
return args + [yield]
end
private
def receive_private_method(*args)
return args + [yield]
end
end
# Note, running this on 1.9 will be testing the Ruby core implementation, but it is good to
# do this to ensure that our backport functions the same as Ruby core in 1.9
class PublicSendTest < Test::Unit::TestCase
def instance
@instance ||= begin
klass = Class.new do
include PublicSendReceiver
end
klass.new
end
end
def singleton_instance
@singleton_instance ||= begin
object = Object.new
object.singleton_class.send(:include, PublicSendReceiver)
object
end
end
def test_should_receive_public_method
assert_equal(
[:foo, :bar, :baz],
instance.public_send(:receive_public_method, :foo, :bar) { :baz }
)
end
def test_should_receive_public_singleton_method
assert_equal(
[:foo, :bar, :baz],
singleton_instance.public_send(:receive_public_method, :foo, :bar) { :baz }
)
end
def test_should_raise_on_protected_method
assert_raises(NoMethodError) do
instance.public_send(:receive_protected_method, :foo, :bar) { :baz }
end
end
def test_should_raise_on_protected_singleton_method
assert_raises(NoMethodError) do
singleton_instance.public_send(:receive_protected_method, :foo, :bar) { :baz }
end
end
def test_should_raise_on_private_method
assert_raises(NoMethodError) do
instance.public_send(:receive_private_method, :foo, :bar) { :baz }
end
end
def test_should_raise_on_singleton_private_method
assert_raises(NoMethodError) do
singleton_instance.public_send(:receive_private_method, :foo, :bar) { :baz }
end
end
def test_should_raise_on_undefined_method
assert_raises(NoMethodError) do
instance.public_send(:receive_undefined_method, :foo, :bar) { :baz }
end
end
def test_protected_method_message
instance.public_send(:receive_protected_method, :foo, :bar) { :baz }
rescue NoMethodError => exception
assert_equal(
"protected method `receive_protected_method' called for #{instance.inspect}",
exception.message
)
end
def test_private_method_message
instance.public_send(:receive_private_method, :foo, :bar) { :baz }
rescue NoMethodError => exception
assert_equal(
"private method `receive_private_method' called for #{instance.inspect}",
exception.message
)
end
def test_undefined_method_message
instance.public_send(:receive_undefined_method, :foo, :bar) { :baz }
rescue NoMethodError => exception
assert_equal(
"undefined method `receive_undefined_method' for #{instance.inspect}",
exception.message
)
end
def test_receiver_with_no_singleton
assert_equal "5", 5.public_send(:to_s)
assert_equal "foo", :foo.public_send(:to_s)
end
end
|