Skip to content

Commit 8473fcd

Browse files
committed
Add specs for already initialized constant warnings
1 parent 556824d commit 8473fcd

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

spec/ruby/core/module/const_set_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,53 @@
7272
lambda { ConstantSpecs.const_set name, 1 }.should raise_error(TypeError)
7373
end
7474

75+
describe "when overwriting an existing constant" do
76+
it "warns if the previous value was a normal value" do
77+
mod = Module.new
78+
mod.const_set :Foo, 42
79+
-> {
80+
mod.const_set :Foo, 1
81+
}.should complain(/already initialized constant/)
82+
mod.const_get(:Foo).should == 1
83+
end
84+
85+
it "does not warn if the previous value was an autoload" do
86+
mod = Module.new
87+
mod.autoload :Foo, "not-existing"
88+
-> {
89+
mod.const_set :Foo, 1
90+
}.should_not complain
91+
mod.const_get(:Foo).should == 1
92+
end
93+
94+
it "does not warn if the previous value was undefined" do
95+
path = fixture(__FILE__, "autoload_o.rb")
96+
ScratchPad.record []
97+
mod = Module.new
98+
99+
mod.autoload :Foo, path
100+
-> { mod::Foo }.should raise_error(NameError)
101+
102+
mod.should have_constant(:Foo)
103+
mod.const_defined?(:Foo).should == false
104+
mod.autoload?(:Foo).should == nil
105+
106+
-> {
107+
mod.const_set :Foo, 1
108+
}.should_not complain
109+
mod.const_get(:Foo).should == 1
110+
end
111+
112+
it "does not warn if the new value is an autoload" do
113+
mod = Module.new
114+
mod.const_set :Foo, 42
115+
-> {
116+
mod.autoload :Foo, "not-existing"
117+
}.should_not complain
118+
mod.const_get(:Foo).should == 42
119+
end
120+
end
121+
75122
describe "on a frozen module" do
76123
before :each do
77124
@frozen = Module.new.freeze

0 commit comments

Comments
 (0)