Skip to content

Use remove_const after specs which set constants #1138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/basicobject/singleton_method_added_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def self.foo
end
}.should raise_error(NoMethodError, /undefined method [`']singleton_method_added' for/)
end
ensure
BasicObjectSpecs.send(:remove_const, :NoSingletonMethodAdded)
end

it "raises NoMethodError for a singleton instance" do
Expand Down
2 changes: 2 additions & 0 deletions core/class/dup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def self.message
it "stores the new name if assigned to a constant" do
CoreClassSpecs::RecordCopy = CoreClassSpecs::Record.dup
CoreClassSpecs::RecordCopy.name.should == "CoreClassSpecs::RecordCopy"
ensure
CoreClassSpecs.send(:remove_const, :RecordCopy)
end

it "raises TypeError if called on BasicObject" do
Expand Down
2 changes: 2 additions & 0 deletions core/class/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def message2; "hello"; end
a = Class.new
MyClass::NestedClass = a
MyClass::NestedClass.name.should == "MyClass::NestedClass"
ensure
Object.send(:remove_const, :MyClass)
end

it "sets the new class' superclass to the given class" do
Expand Down
2 changes: 2 additions & 0 deletions core/exception/errno_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
ExceptionSpecs::EMFILESub = Class.new(Errno::EMFILE)
exc = ExceptionSpecs::EMFILESub.new
exc.should be_an_instance_of(ExceptionSpecs::EMFILESub)
ensure
ExceptionSpecs.send(:remove_const, :EMFILESub)
end
end

Expand Down
2 changes: 2 additions & 0 deletions core/exception/system_call_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def initialize
exc = ExceptionSpecs::SCESub.new
ScratchPad.recorded.should equal(:initialize)
exc.should be_an_instance_of(ExceptionSpecs::SCESub)
ensure
ExceptionSpecs.send(:remove_const, :SCESub)
end
end

Expand Down
16 changes: 16 additions & 0 deletions core/kernel/eval_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ class EvalSpecs
eval(code)
EvalSpecs.constants(false).should include(:"Vπ")
EvalSpecs::Vπ.should == 3.14
ensure
EvalSpecs.send(:remove_const, :Vπ)
end

it "allows an emacs-style magic comment encoding" do
Expand All @@ -326,6 +328,8 @@ class EvalSpecs
eval(code)
EvalSpecs.constants(false).should include(:"Vπemacs")
EvalSpecs::Vπemacs.should == 3.14
ensure
EvalSpecs.send(:remove_const, :Vπemacs)
end

it "allows spaces before the magic encoding comment" do
Expand All @@ -339,6 +343,8 @@ class EvalSpecs
eval(code)
EvalSpecs.constants(false).should include(:"Vπspaces")
EvalSpecs::Vπspaces.should == 3.14
ensure
EvalSpecs.send(:remove_const, :Vπspaces)
end

it "allows a shebang line before the magic encoding comment" do
Expand All @@ -353,6 +359,8 @@ class EvalSpecs
eval(code)
EvalSpecs.constants(false).should include(:"Vπshebang")
EvalSpecs::Vπshebang.should == 3.14
ensure
EvalSpecs.send(:remove_const, :Vπshebang)
end

it "allows a shebang line and some spaces before the magic encoding comment" do
Expand All @@ -367,6 +375,8 @@ class EvalSpecs
eval(code)
EvalSpecs.constants(false).should include(:"Vπshebang_spaces")
EvalSpecs::Vπshebang_spaces.should == 3.14
ensure
EvalSpecs.send(:remove_const, :Vπshebang_spaces)
end

it "allows a magic encoding comment and a subsequent frozen_string_literal magic comment" do
Expand All @@ -385,6 +395,8 @@ class EvalSpecs
EvalSpecs::Vπstring.should == "frozen"
EvalSpecs::Vπstring.encoding.should == Encoding::UTF_8
EvalSpecs::Vπstring.frozen?.should == !frozen_string_default
ensure
EvalSpecs.send(:remove_const, :Vπstring)
end

it "allows a magic encoding comment and a frozen_string_literal magic comment on the same line in emacs style" do
Expand All @@ -400,6 +412,8 @@ class EvalSpecs
EvalSpecs::Vπsame_line.should == "frozen"
EvalSpecs::Vπsame_line.encoding.should == Encoding::UTF_8
EvalSpecs::Vπsame_line.frozen?.should be_true
ensure
EvalSpecs.send(:remove_const, :Vπsame_line)
end

it "ignores the magic encoding comment if it is after a frozen_string_literal magic comment" do
Expand All @@ -420,6 +434,8 @@ class EvalSpecs
value.should == "frozen"
value.encoding.should == Encoding::BINARY
value.frozen?.should == !frozen_string_default
ensure
EvalSpecs.send(:remove_const, binary_constant)
end

it "ignores the frozen_string_literal magic comment if it appears after a token and warns if $VERBOSE is true" do
Expand Down
2 changes: 2 additions & 0 deletions core/module/const_defined_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
str = "CS_CONSTλ".encode("euc-jp")
ConstantSpecs.const_set str, 1
ConstantSpecs.const_defined?(str).should be_true
ensure
ConstantSpecs.send(:remove_const, str)
end

it "returns false if the constant is not defined in the receiver, its superclass, or any included modules" do
Expand Down
22 changes: 22 additions & 0 deletions core/module/const_get_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,40 +202,60 @@

ConstantSpecs::ContainerA::ChildA::CS_CONST301 = :const301_5
ConstantSpecs::ContainerA::ChildA.const_get(:CS_CONST301).should == :const301_5
ensure
ConstantSpecs::ClassA.send(:remove_const, :CS_CONST301)
ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST301)
ConstantSpecs::ParentA.send(:remove_const, :CS_CONST301)
ConstantSpecs::ContainerA::ChildA.send(:remove_const, :CS_CONST301)
end

it "searches a module included in the immediate class before the superclass" do
ConstantSpecs::ParentB::CS_CONST302 = :const302_1
ConstantSpecs::ModuleF::CS_CONST302 = :const302_2
ConstantSpecs::ContainerB::ChildB.const_get(:CS_CONST302).should == :const302_2
ensure
ConstantSpecs::ParentB.send(:remove_const, :CS_CONST302)
ConstantSpecs::ModuleF.send(:remove_const, :CS_CONST302)
end

it "searches the superclass before a module included in the superclass" do
ConstantSpecs::ModuleE::CS_CONST303 = :const303_1
ConstantSpecs::ParentB::CS_CONST303 = :const303_2
ConstantSpecs::ContainerB::ChildB.const_get(:CS_CONST303).should == :const303_2
ensure
ConstantSpecs::ModuleE.send(:remove_const, :CS_CONST303)
ConstantSpecs::ParentB.send(:remove_const, :CS_CONST303)
end

it "searches a module included in the superclass" do
ConstantSpecs::ModuleA::CS_CONST304 = :const304_1
ConstantSpecs::ModuleE::CS_CONST304 = :const304_2
ConstantSpecs::ContainerB::ChildB.const_get(:CS_CONST304).should == :const304_2
ensure
ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST304)
ConstantSpecs::ModuleE.send(:remove_const, :CS_CONST304)
end

it "searches the superclass chain" do
ConstantSpecs::ModuleA::CS_CONST305 = :const305
ConstantSpecs::ContainerB::ChildB.const_get(:CS_CONST305).should == :const305
ensure
ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST305)
end

it "returns a toplevel constant when the receiver is a Class" do
Object::CS_CONST306 = :const306
ConstantSpecs::ContainerB::ChildB.const_get(:CS_CONST306).should == :const306
ensure
Object.send(:remove_const, :CS_CONST306)
end

it "returns a toplevel constant when the receiver is a Module" do
Object::CS_CONST308 = :const308
ConstantSpecs.const_get(:CS_CONST308).should == :const308
ConstantSpecs::ModuleA.const_get(:CS_CONST308).should == :const308
ensure
Object.send(:remove_const, :CS_CONST308)
end

it "returns the updated value of a constant" do
Expand All @@ -246,6 +266,8 @@
ConstantSpecs::ClassB::CS_CONST309 = :const309_2
}.should complain(/already initialized constant/)
ConstantSpecs::ClassB.const_get(:CS_CONST309).should == :const309_2
ensure
ConstantSpecs::ClassB.send(:remove_const, :CS_CONST309)
end
end
end
13 changes: 13 additions & 0 deletions core/module/const_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@

ConstantSpecs.const_set "CS_CONST402", :const402
ConstantSpecs.const_get(:CS_CONST402).should == :const402
ensure
ConstantSpecs.send(:remove_const, :CS_CONST401)
ConstantSpecs.send(:remove_const, :CS_CONST402)
end

it "returns the value set" do
ConstantSpecs.const_set(:CS_CONST403, :const403).should == :const403
ensure
ConstantSpecs.send(:remove_const, :CS_CONST403)
end

it "sets the name of an anonymous module" do
m = Module.new
ConstantSpecs.const_set(:CS_CONST1000, m)
m.name.should == "ConstantSpecs::CS_CONST1000"
ensure
ConstantSpecs.send(:remove_const, :CS_CONST1000)
end

it "sets the name of a module scoped by an anonymous module" do
Expand All @@ -38,6 +45,8 @@
b.name.should == "ModuleSpecs_CS3::B"
c.name.should == "ModuleSpecs_CS3::B::C"
d.name.should == "ModuleSpecs_CS3::D"
ensure
Object.send(:remove_const, :ModuleSpecs_CS3)
end

it "raises a NameError if the name does not start with a capital letter" do
Expand All @@ -55,13 +64,17 @@
ConstantSpecs.const_set("CS_CONST404", :const404).should == :const404
-> { ConstantSpecs.const_set "Name=", 1 }.should raise_error(NameError)
-> { ConstantSpecs.const_set "Name?", 1 }.should raise_error(NameError)
ensure
ConstantSpecs.send(:remove_const, :CS_CONST404)
end

it "calls #to_str to convert the given name to a String" do
name = mock("CS_CONST405")
name.should_receive(:to_str).and_return("CS_CONST405")
ConstantSpecs.const_set(name, :const405).should == :const405
ConstantSpecs::CS_CONST405.should == :const405
ensure
ConstantSpecs.send(:remove_const, :CS_CONST405)
end

it "raises a TypeError if conversion to a String by calling #to_str fails" do
Expand Down
22 changes: 22 additions & 0 deletions core/module/const_source_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,60 @@

ConstantSpecs::ContainerA::ChildA::CSL_CONST301 = :const301_5
ConstantSpecs::ContainerA::ChildA.const_source_location(:CSL_CONST301).should == [__FILE__, __LINE__ - 1]
ensure
ConstantSpecs::ClassA.send(:remove_const, :CSL_CONST301)
ConstantSpecs::ModuleA.send(:remove_const, :CSL_CONST301)
ConstantSpecs::ParentA.send(:remove_const, :CSL_CONST301)
ConstantSpecs::ContainerA::ChildA.send(:remove_const, :CSL_CONST301)
end

it "searches a path in a module included in the immediate class before the superclass" do
ConstantSpecs::ParentB::CSL_CONST302 = :const302_1
ConstantSpecs::ModuleF::CSL_CONST302 = :const302_2
ConstantSpecs::ContainerB::ChildB.const_source_location(:CSL_CONST302).should == [__FILE__, __LINE__ - 1]
ensure
ConstantSpecs::ParentB.send(:remove_const, :CSL_CONST302)
ConstantSpecs::ModuleF.send(:remove_const, :CSL_CONST302)
end

it "searches a path in the superclass before a module included in the superclass" do
ConstantSpecs::ModuleE::CSL_CONST303 = :const303_1
ConstantSpecs::ParentB::CSL_CONST303 = :const303_2
ConstantSpecs::ContainerB::ChildB.const_source_location(:CSL_CONST303).should == [__FILE__, __LINE__ - 1]
ensure
ConstantSpecs::ModuleE.send(:remove_const, :CSL_CONST303)
ConstantSpecs::ParentB.send(:remove_const, :CSL_CONST303)
end

it "searches a path in a module included in the superclass" do
ConstantSpecs::ModuleA::CSL_CONST304 = :const304_1
ConstantSpecs::ModuleE::CSL_CONST304 = :const304_2
ConstantSpecs::ContainerB::ChildB.const_source_location(:CSL_CONST304).should == [__FILE__, __LINE__ - 1]
ensure
ConstantSpecs::ModuleA.send(:remove_const, :CSL_CONST304)
ConstantSpecs::ModuleE.send(:remove_const, :CSL_CONST304)
end

it "searches a path in the superclass chain" do
ConstantSpecs::ModuleA::CSL_CONST305 = :const305
ConstantSpecs::ContainerB::ChildB.const_source_location(:CSL_CONST305).should == [__FILE__, __LINE__ - 1]
ensure
ConstantSpecs::ModuleA.send(:remove_const, :CSL_CONST305)
end

it "returns path to a toplevel constant when the receiver is a Class" do
Object::CSL_CONST306 = :const306
ConstantSpecs::ContainerB::ChildB.const_source_location(:CSL_CONST306).should == [__FILE__, __LINE__ - 1]
ensure
Object.send(:remove_const, :CSL_CONST306)
end

it "returns path to a toplevel constant when the receiver is a Module" do
Object::CSL_CONST308 = :const308
ConstantSpecs.const_source_location(:CSL_CONST308).should == [__FILE__, __LINE__ - 1]
ConstantSpecs::ModuleA.const_source_location(:CSL_CONST308).should == [__FILE__, __LINE__ - 2]
ensure
Object.send(:remove_const, :CSL_CONST308)
end

it "returns path to the updated value of a constant" do
Expand All @@ -63,6 +83,8 @@
ConstantSpecs::ClassB::CSL_CONST309 = :const309_2
}.should complain(/already initialized constant/)
ConstantSpecs::ClassB.const_source_location(:CSL_CONST309).should == [__FILE__, __LINE__ - 2]
ensure
ConstantSpecs::ClassB.send(:remove_const, :CSL_CONST309)
end
end

Expand Down
3 changes: 3 additions & 0 deletions core/module/define_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ class DefineMethodSpecClass
ChildClass = Class.new(ParentClass) { define_method(:foo) { :baz } }
ParentClass.send :define_method, :foo, ChildClass.instance_method(:foo)
}.should raise_error(TypeError, /bind argument must be a subclass of ChildClass/)
ensure
Object.send(:remove_const, :ParentClass)
Object.send(:remove_const, :ChildClass)
end

it "raises a TypeError when an UnboundMethod from one class is defined on an unrelated class" do
Expand Down
18 changes: 17 additions & 1 deletion core/module/include_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def self.append_features(mod)
end

it "does not raise a TypeError when the argument is an instance of a subclass of Module" do
-> { ModuleSpecs::SubclassSpec.include(ModuleSpecs::Subclass.new) }.should_not raise_error(TypeError)
class ModuleSpecs::SubclassSpec::AClass
end
-> { ModuleSpecs::SubclassSpec::AClass.include(ModuleSpecs::Subclass.new) }.should_not raise_error(TypeError)
ensure
ModuleSpecs::SubclassSpec.send(:remove_const, :AClass)
end

ruby_version_is ""..."3.2" do
Expand Down Expand Up @@ -427,6 +431,8 @@ def self.foo
M.const_set(:FOO, 'm')
B.foo.should == 'm'
end
ensure
ModuleSpecs.send(:remove_const, :ConstUpdated)
end

it "updates the constant when a module included after a call is later updated" do
Expand All @@ -453,6 +459,8 @@ module M
M.const_set(:FOO, 'm')
B.foo.should == 'm'
end
ensure
ModuleSpecs.send(:remove_const, :ConstLaterUpdated)
end

it "updates the constant when a module included in another module after a call is later updated" do
Expand All @@ -479,6 +487,8 @@ module M
M.const_set(:FOO, 'm')
B.foo.should == 'm'
end
ensure
ModuleSpecs.send(:remove_const, :ConstModuleLaterUpdated)
end

it "updates the constant when a nested included module is updated" do
Expand Down Expand Up @@ -507,6 +517,8 @@ def self.foo
N.const_set(:FOO, 'n')
B.foo.should == 'n'
end
ensure
ModuleSpecs.send(:remove_const, :ConstUpdatedNestedIncludeUpdated)
end

it "updates the constant when a new module is included" do
Expand All @@ -531,6 +543,8 @@ def self.foo
B.include(M)
B.foo.should == 'm'
end
ensure
ModuleSpecs.send(:remove_const, :ConstUpdatedNewInclude)
end

it "updates the constant when a new module with nested module is included" do
Expand Down Expand Up @@ -559,6 +573,8 @@ def self.foo
B.include M
B.foo.should == 'n'
end
ensure
ModuleSpecs.send(:remove_const, :ConstUpdatedNestedIncluded)
end

it "overrides a previous super method call" do
Expand Down
Loading