Skip to content

Commit e5a75b3

Browse files
committed
Add new Module#prepend and Module#include invalidation specs
1 parent f96be45 commit e5a75b3

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

spec/ruby/core/module/include_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,30 @@ def foo
263263
foo.call.should == 'm'
264264
end
265265

266+
267+
it "updates the method when a module included after a call is later updated" do
268+
m_module = Module.new
269+
a_class = Class.new do
270+
def foo
271+
'a'
272+
end
273+
end
274+
b_class = Class.new(a_class)
275+
b = b_class.new
276+
foo = -> { b.foo }
277+
foo.call.should == 'a'
278+
279+
b_class.include m_module
280+
foo.call.should == 'a'
281+
282+
m_module.module_eval do
283+
def foo
284+
"m"
285+
end
286+
end
287+
foo.call.should == 'm'
288+
end
289+
266290
it "updates the method when a nested included module is updated" do
267291
a_class = Class.new do
268292
def foo

spec/ruby/core/module/prepend_spec.rb

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,124 @@ def foo
7575
foo.call.should == 'm'
7676
end
7777

78+
it "updates the method when there is a base included method and the prepended module overrides it" do
79+
base_module = Module.new do
80+
def foo
81+
'a'
82+
end
83+
end
84+
a_class = Class.new do
85+
include base_module
86+
end
87+
a = a_class.new
88+
foo = -> { a.foo }
89+
foo.call.should == 'a'
90+
91+
m_module = Module.new do
92+
def foo
93+
"m"
94+
end
95+
end
96+
a_class.prepend m_module
97+
foo.call.should == 'm'
98+
end
99+
100+
it "updates the method when there is a base included method and the prepended module is later updated" do
101+
base_module = Module.new do
102+
def foo
103+
'a'
104+
end
105+
end
106+
a_class = Class.new do
107+
include base_module
108+
end
109+
a = a_class.new
110+
foo = -> { a.foo }
111+
foo.call.should == 'a'
112+
113+
m_module = Module.new
114+
a_class.prepend m_module
115+
foo.call.should == 'a'
116+
117+
m_module.module_eval do
118+
def foo
119+
"m"
120+
end
121+
end
122+
foo.call.should == 'm'
123+
end
124+
125+
it "updates the method when a module prepended after a call is later updated" do
126+
m_module = Module.new
127+
a_class = Class.new do
128+
def foo
129+
'a'
130+
end
131+
end
132+
a = a_class.new
133+
foo = -> { a.foo }
134+
foo.call.should == 'a'
135+
136+
a_class.prepend m_module
137+
foo.call.should == 'a'
138+
139+
m_module.module_eval do
140+
def foo
141+
"m"
142+
end
143+
end
144+
foo.call.should == 'm'
145+
end
146+
147+
it "updates the method when a module is prepended after another and the method is defined later on that module" do
148+
m_module = Module.new do
149+
def foo
150+
'a'
151+
end
152+
end
153+
a_class = Class.new
154+
a_class.prepend m_module
155+
a = a_class.new
156+
foo = -> { a.foo }
157+
foo.call.should == 'a'
158+
159+
n_module = Module.new
160+
a_class.prepend n_module
161+
foo.call.should == 'a'
162+
163+
n_module.module_eval do
164+
def foo
165+
"n"
166+
end
167+
end
168+
foo.call.should == 'n'
169+
end
170+
171+
it "updates the method when a module is included in a prepended module and the method is defined later" do
172+
a_class = Class.new
173+
base_module = Module.new do
174+
def foo
175+
'a'
176+
end
177+
end
178+
a_class.prepend base_module
179+
a = a_class.new
180+
foo = -> { a.foo }
181+
foo.call.should == 'a'
182+
183+
m_module = Module.new
184+
n_module = Module.new
185+
m_module.include n_module
186+
a_class.prepend m_module
187+
188+
n_module.module_eval do
189+
def foo
190+
"n"
191+
end
192+
end
193+
foo.call.should == 'n'
194+
end
195+
78196
it "updates the method when a new module with an included module is prepended" do
79197
a_class = Class.new do
80198
def foo

0 commit comments

Comments
 (0)