This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
rhook_minor_spec.rb
224 lines (180 loc) · 5.47 KB
/
rhook_minor_spec.rb
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
# ================================================================
describe "rhook (minor specifications / behavior, and bugs)" do
class Target
end
before :each do
Target._rhook.unbind_all()
end
# ================================================================
describe "invocation" do
describe "is proceed by correct order & not rerun." do
class Target
attr_reader :order_ary
def order_call
@order_ary = []
_rhook.to.order_target
end
def order_target
@order_ary << "orig"
end
end
example do
t = Target.new
# [bug] It calls the target method/procedure twice when no hooks are registered.
t.order_call
t.order_ary.should == ["orig"]
t._rhook.bind(:order_target) do |inv|
t.order_ary << "before_call"
inv.call
t.order_ary << "after_call"
end
t.order_call
t.order_ary.should == ["before_call", "orig", "after_call"]
end
end
end
# ================================================================
describe "hack" do
# ================================================================
describe "Can hack on private method" do
class Target
def call_private
test_private
end
private
def test_private
"foo"
end
end #/Target
example do
Target._rhook.hack(:test_private) do |inv|
"bar"
end
t = Target.new
t.call_private.should == "bar"
end
end
# ================================================================
# ================================================================
describe "Raises error on hack to non-existent method" do
class Target
def hack_err1
end
def self.hack_err2
end
end
example do
Target._rhook.hack(:hack_err1) do |inv|
end
Target._rhook.hack(:hack_err2) do |inv|
end
proc {
Target._rhook.hack(:hack_err_nonexistent_method) do |inv|
end
}.should raise_error(/defined in neither/)
end
end
end
# ================================================================
describe "on_method" do
# ================================================================
describe "to non-existent method" do
class Target
def on_method_test_non_existent
end
end
example "raises error" do
proc {
Target._rhook.on_method(:nonexistent_method)
}.should raise_error(/Tried to on_method/)
end
example "to ignore error, set :ifdef => true" do
result = Target._rhook.on_method(:nonexistent_method, :ifdef => true)
result.should == false
# if success, it returns true
result = Target._rhook.on_method(:on_method_test_non_existent, :ifdef => true)
result.should == true
end
end
# ========================================================================
describe "is applied to the method (== 'hack'ed), and also it is called via _rhook.to(), " do
class Target
def both_on_method_and_to
_rhook.to.both_on_method_and_to_target
end
def both_on_method_and_to_target
end
end
example "even though, the hook should be just called once." do
m = mock
m.should_receive(:called).once
Target._rhook.hack(:both_on_method_and_to_target) { |inv|
m.called
inv.call
}
Target.new.both_on_method_and_to()
end
end
end
# ========================================================================
describe "#call_method(#to): " do
before :each do
end
example "[bugfix] super: no superclass method" do
class CallMethodSuperTest_Super
def the_method
end
end
module CallMethodSuperTest_Module
def the_method
super
end
end
class CallMethodSuperTest_Inherited < CallMethodSuperTest_Super
include CallMethodSuperTest_Module
end
# This bug does not appear without hooks.
CallMethodSuperTest_Inherited._rhook.bind(:the_method) do |inv|
inv.call
end
# OK.
CallMethodSuperTest_Inherited.new.the_method
# We fixed the bug this cause error:
lambda {
CallMethodSuperTest_Inherited.new._rhook.to.the_method
}.should_not raise_error
# - super: no superclass method `the_method'
end
end
# ========================================================================
# ========================================================================
describe ":once" do
class Target
def once_test
"a"
end
end
before :each do
end
example "" do
# first hook
Target._rhook.hack(:once_test, :once => :increment_hook) do |inv|
inv.call + "b"
end
# again
Target._rhook.hack(:once_test, :once => :increment_hook) do |inv|
inv.call + "c"
end
# so, the hook is not called twice.
Target.new.once_test.should == "ab"
# add another hook
Target._rhook.hack(:once_test, :once => :another) do |inv|
inv.call + "x"
end
# so, it is called.
Target.new.once_test.should == "abx"
end
end
# ========================================================================
end