diff --git a/core/basicobject/singleton_method_added_spec.rb b/core/basicobject/singleton_method_added_spec.rb index ab6b2a2d10..d6f37736cc 100644 --- a/core/basicobject/singleton_method_added_spec.rb +++ b/core/basicobject/singleton_method_added_spec.rb @@ -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 diff --git a/core/class/dup_spec.rb b/core/class/dup_spec.rb index c09ed71b31..1ff9abf7b4 100644 --- a/core/class/dup_spec.rb +++ b/core/class/dup_spec.rb @@ -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 diff --git a/core/class/new_spec.rb b/core/class/new_spec.rb index 93152a83ee..6fe54c3209 100644 --- a/core/class/new_spec.rb +++ b/core/class/new_spec.rb @@ -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 diff --git a/core/exception/errno_spec.rb b/core/exception/errno_spec.rb index a063e522ea..1ab4277700 100644 --- a/core/exception/errno_spec.rb +++ b/core/exception/errno_spec.rb @@ -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 diff --git a/core/exception/system_call_error_spec.rb b/core/exception/system_call_error_spec.rb index 73167bc288..2dafe5dc10 100644 --- a/core/exception/system_call_error_spec.rb +++ b/core/exception/system_call_error_spec.rb @@ -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 diff --git a/core/kernel/eval_spec.rb b/core/kernel/eval_spec.rb index f3b025fb7b..a8d8c43cb8 100644 --- a/core/kernel/eval_spec.rb +++ b/core/kernel/eval_spec.rb @@ -293,6 +293,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 @@ -306,6 +308,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 @@ -319,6 +323,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 @@ -333,6 +339,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 @@ -347,6 +355,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 @@ -366,6 +376,8 @@ class EvalSpecs EvalSpecs::Vπstring.should == "frozen" EvalSpecs::Vπstring.encoding.should == Encoding::UTF_8 EvalSpecs::Vπstring.frozen?.should be_true + 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 @@ -381,6 +393,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 @@ -400,6 +414,8 @@ class EvalSpecs value.should == "frozen" value.encoding.should == Encoding::BINARY value.frozen?.should be_true + 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 diff --git a/core/module/const_defined_spec.rb b/core/module/const_defined_spec.rb index 027cf5a245..8b137cd134 100644 --- a/core/module/const_defined_spec.rb +++ b/core/module/const_defined_spec.rb @@ -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 diff --git a/core/module/const_get_spec.rb b/core/module/const_get_spec.rb index 0233118f4b..06041ddf34 100644 --- a/core/module/const_get_spec.rb +++ b/core/module/const_get_spec.rb @@ -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 @@ -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 diff --git a/core/module/const_set_spec.rb b/core/module/const_set_spec.rb index 5bdfd7b68f..823768b882 100644 --- a/core/module/const_set_spec.rb +++ b/core/module/const_set_spec.rb @@ -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 @@ -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 @@ -55,6 +64,8 @@ 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 @@ -62,6 +73,8 @@ 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 diff --git a/core/module/const_source_location_spec.rb b/core/module/const_source_location_spec.rb index ded2aa51d7..4120b2da97 100644 --- a/core/module/const_source_location_spec.rb +++ b/core/module/const_source_location_spec.rb @@ -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 @@ -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 diff --git a/core/module/define_method_spec.rb b/core/module/define_method_spec.rb index e04bb87ceb..c5dfc53764 100644 --- a/core/module/define_method_spec.rb +++ b/core/module/define_method_spec.rb @@ -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 diff --git a/core/module/include_spec.rb b/core/module/include_spec.rb index c073bc31ca..0ad22bcb0a 100644 --- a/core/module/include_spec.rb +++ b/core/module/include_spec.rb @@ -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 it "imports constants to modules and classes" do @@ -399,6 +403,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 @@ -425,6 +431,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 @@ -451,6 +459,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 @@ -479,6 +489,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 @@ -503,6 +515,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 @@ -531,6 +545,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 diff --git a/core/module/name_spec.rb b/core/module/name_spec.rb index 0d1f4e24d5..320c95347e 100644 --- a/core/module/name_spec.rb +++ b/core/module/name_spec.rb @@ -30,6 +30,8 @@ module m::N; end m::N.name.should =~ /\A#::N\z/ ModuleSpecs::Anonymous::WasAnnon = m::N m::N.name.should == "ModuleSpecs::Anonymous::WasAnnon" + ensure + ModuleSpecs::Anonymous.send(:remove_const, :WasAnnon) end it "may be the repeated in different module objects" do @@ -76,12 +78,16 @@ module m::Child; end m = Module.new ModuleSpecs::Anonymous::A = m m.name.should == "ModuleSpecs::Anonymous::A" + ensure + ModuleSpecs::Anonymous.send(:remove_const, :A) end it "is set when assigning to a constant (constant path does not match outer module name)" do m = Module.new ModuleSpecs::Anonymous::SameChild::A = m m.name.should == "ModuleSpecs::Anonymous::Child::A" + ensure + ModuleSpecs::Anonymous::SameChild.send(:remove_const, :A) end it "is not modified when assigning to a new constant after it has been accessed" do @@ -90,6 +96,9 @@ module m::Child; end m.name.should == "ModuleSpecs::Anonymous::B" ModuleSpecs::Anonymous::C = m m.name.should == "ModuleSpecs::Anonymous::B" + ensure + ModuleSpecs::Anonymous.send(:remove_const, :B) + ModuleSpecs::Anonymous.send(:remove_const, :C) end it "is not modified when assigned to a different anonymous module" do @@ -125,6 +134,8 @@ module ModuleSpecs::Anonymous m::N = Module.new ModuleSpecs::Anonymous::E = m m::N.name.should == "ModuleSpecs::Anonymous::E::N" + ensure + ModuleSpecs::Anonymous.send(:remove_const, :E) end # https://bugs.ruby-lang.org/issues/19681 @@ -138,6 +149,8 @@ module ModuleSpecs::Anonymous "ModuleSpecs::Anonymous::StoredInMultiplePlaces::O" ] valid_names.should include(m::N.name) # You get one of the two, but you don't know which one. + ensure + ModuleSpecs::Anonymous.send(:remove_const, :StoredInMultiplePlaces) end it "returns a frozen String" do diff --git a/core/module/prepend_spec.rb b/core/module/prepend_spec.rb index c90fa9700e..538351a327 100644 --- a/core/module/prepend_spec.rb +++ b/core/module/prepend_spec.rb @@ -241,6 +241,8 @@ def self.foo B.prepend M B.foo.should == 'm' end + ensure + ModuleSpecs.send(:remove_const, :ConstUpdatePrepended) end it "updates the constant when a prepended module is updated" do @@ -261,6 +263,8 @@ def self.foo M.const_set(:FOO, 'm') B.foo.should == 'm' end + ensure + ModuleSpecs.send(:remove_const, :ConstPrependedUpdated) end it "updates the constant when there is a base included constant and the prepended module overrides it" do @@ -282,6 +286,8 @@ module M A.prepend M A.foo.should == 'm' end + ensure + ModuleSpecs.send(:remove_const, :ConstIncludedPrependedOverride) end it "updates the constant when there is a base included constant and the prepended module is later updated" do @@ -305,6 +311,8 @@ module M M.const_set(:FOO, 'm') A.foo.should == 'm' end + ensure + ModuleSpecs.send(:remove_const, :ConstIncludedPrependedLaterUpdated) end it "updates the constant when a module prepended after a constant is later updated" do @@ -328,6 +336,8 @@ def self.foo M.const_set(:FOO, 'm') B.foo.should == 'm' end + ensure + ModuleSpecs.send(:remove_const, :ConstUpdatedPrependedAfterLaterUpdated) end it "updates the constant when a module is prepended after another and the constant is defined later on that module" do @@ -352,6 +362,8 @@ module N N.const_set(:FOO, 'n') A.foo.should == 'n' end + ensure + ModuleSpecs.send(:remove_const, :ConstUpdatedPrependedAfterConstDefined) end it "updates the constant when a module is included in a prepended module and the constant is defined later" do @@ -379,6 +391,8 @@ module M N.const_set(:FOO, 'n') A.foo.should == 'n' end + ensure + ModuleSpecs.send(:remove_const, :ConstUpdatedIncludedInPrependedConstDefinedLater) end it "updates the constant when a new module with an included module is prepended" do @@ -405,6 +419,8 @@ module M B.prepend M B.foo.should == 'n' end + ensure + ModuleSpecs.send(:remove_const, :ConstUpdatedNewModuleIncludedPrepended) end it "raises a TypeError when the argument is not a Module" do @@ -412,7 +428,11 @@ module M end it "does not raise a TypeError when the argument is an instance of a subclass of Module" do - -> { ModuleSpecs::SubclassSpec.prepend(ModuleSpecs::Subclass.new) }.should_not raise_error(TypeError) + class ModuleSpecs::SubclassSpec::AClass + end + -> { ModuleSpecs::SubclassSpec::AClass.prepend(ModuleSpecs::Subclass.new) }.should_not raise_error(TypeError) + ensure + ModuleSpecs::SubclassSpec.send(:remove_const, :AClass) end it "imports constants" do diff --git a/core/module/remove_const_spec.rb b/core/module/remove_const_spec.rb index 0ac23f05a5..35a9d65105 100644 --- a/core/module/remove_const_spec.rb +++ b/core/module/remove_const_spec.rb @@ -101,5 +101,7 @@ def self.foo A.send(:remove_const,:FOO) A.foo.should == 'm' end + ensure + ConstantSpecs.send(:remove_const, :RemovedConstantUpdate) end end diff --git a/core/module/to_s_spec.rb b/core/module/to_s_spec.rb index 6b1a615ef9..83c0ae0825 100644 --- a/core/module/to_s_spec.rb +++ b/core/module/to_s_spec.rb @@ -51,6 +51,8 @@ module ModuleSpecs::RefinementInspect ModuleSpecs::RefinementInspect::R.name.should == 'ModuleSpecs::RefinementInspect::R' ModuleSpecs::RefinementInspect::R.to_s.should == '#' + ensure + ModuleSpecs.send(:remove_const, :RefinementInspect) end it 'does not call #inspect or #to_s for singleton classes' do diff --git a/core/struct/new_spec.rb b/core/struct/new_spec.rb index 8758051a81..9a3bfa2bf8 100644 --- a/core/struct/new_spec.rb +++ b/core/struct/new_spec.rb @@ -6,6 +6,8 @@ struct = Struct.new('Animal', :name, :legs, :eyeballs) struct.should == Struct::Animal struct.name.should == "Struct::Animal" + ensure + Struct.send(:remove_const, :Animal) end it "overwrites previously defined constants with string as first argument" do @@ -19,6 +21,8 @@ second.should == Struct::Person first.members.should_not == second.members + ensure + Struct.send(:remove_const, :Person) end it "calls to_str on its first argument (constant name)" do @@ -27,6 +31,8 @@ def obj.to_str() "Foo" end struct = Struct.new(obj) struct.should == Struct::Foo struct.name.should == "Struct::Foo" + ensure + Struct.send(:remove_const, :Foo) end it "creates a new anonymous class with nil first argument" do @@ -138,6 +144,8 @@ def platform it "creates a constant in subclass' namespace" do struct = StructClasses::Apple.new('Computer', :size) struct.should == StructClasses::Apple::Computer + ensure + StructClasses::Apple.send(:remove_const, :Computer) end it "creates an instance" do diff --git a/language/class_spec.rb b/language/class_spec.rb index eab3cd0651..0b770d69b5 100644 --- a/language/class_spec.rb +++ b/language/class_spec.rb @@ -271,6 +271,8 @@ def self.get_class_name AnonWithConstant.name.should == 'AnonWithConstant' klass.get_class_name.should == 'AnonWithConstant' + ensure + Object.send(:remove_const, :AnonWithConstant) end end end diff --git a/language/constants_spec.rb b/language/constants_spec.rb index 08c534487e..35913b9e30 100644 --- a/language/constants_spec.rb +++ b/language/constants_spec.rb @@ -72,39 +72,60 @@ module ConstantSpecs::SpecAdded1 ConstantSpecs::ModuleA::CS_CONST101 = :const101_5 ConstantSpecs::ModuleA::CS_CONST101.should == :const101_5 + ensure + ConstantSpecs::ClassB.send(:remove_const, :CS_CONST101) + ConstantSpecs::ParentB.send(:remove_const, :CS_CONST101) + ConstantSpecs::ContainerB.send(:remove_const, :CS_CONST101) + ConstantSpecs::ContainerB::ChildB.send(:remove_const, :CS_CONST101) + ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST101) end it "searches a module included in the immediate class before the superclass" do ConstantSpecs::ParentB::CS_CONST102 = :const102_1 ConstantSpecs::ModuleF::CS_CONST102 = :const102_2 ConstantSpecs::ContainerB::ChildB::CS_CONST102.should == :const102_2 + ensure + ConstantSpecs::ParentB.send(:remove_const, :CS_CONST102) + ConstantSpecs::ModuleF.send(:remove_const, :CS_CONST102) end it "searches the superclass before a module included in the superclass" do ConstantSpecs::ModuleE::CS_CONST103 = :const103_1 ConstantSpecs::ParentB::CS_CONST103 = :const103_2 ConstantSpecs::ContainerB::ChildB::CS_CONST103.should == :const103_2 + ensure + ConstantSpecs::ModuleE.send(:remove_const, :CS_CONST103) + ConstantSpecs::ParentB.send(:remove_const, :CS_CONST103) end it "searches a module included in the superclass" do ConstantSpecs::ModuleA::CS_CONST104 = :const104_1 ConstantSpecs::ModuleE::CS_CONST104 = :const104_2 ConstantSpecs::ContainerB::ChildB::CS_CONST104.should == :const104_2 + ensure + ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST104) + ConstantSpecs::ModuleE.send(:remove_const, :CS_CONST104) end it "searches the superclass chain" do ConstantSpecs::ModuleA::CS_CONST105 = :const105 ConstantSpecs::ContainerB::ChildB::CS_CONST105.should == :const105 + ensure + ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST105) end it "searches Object if no class or module qualifier is given" do CS_CONST106 = :const106 CS_CONST106.should == :const106 + ensure + Object.send(:remove_const, :CS_CONST106) end it "searches Object if a toplevel qualifier (::X) is given" do ::CS_CONST107 = :const107 ::CS_CONST107.should == :const107 + ensure + Object.send(:remove_const, :CS_CONST107) end it "does not search the singleton class of the class or module" do @@ -123,6 +144,9 @@ class << self end -> { ConstantSpecs::CS_CONST108 }.should raise_error(NameError) + ensure + ConstantSpecs::ContainerB::ChildB.singleton_class.send(:remove_const, :CS_CONST108) + ConstantSpecs.singleton_class.send(:remove_const, :CS_CONST108) end it "returns the updated value when a constant is reassigned" do @@ -133,6 +157,8 @@ class << self ConstantSpecs::ClassB::CS_CONST109 = :const109_2 }.should complain(/already initialized constant/) ConstantSpecs::ClassB::CS_CONST109.should == :const109_2 + ensure + ConstantSpecs::ClassB.send(:remove_const, :CS_CONST109) end ruby_version_is "3.2" do @@ -292,6 +318,12 @@ class ConstantSpecs::ClassB; CS_CONST201 = :const201_2; end ConstantSpecs::ClassB.new.const201.should == :const201_2 ConstantSpecs::ParentB.new.const201.should == :const201_3 ConstantSpecs::ContainerB::ChildB.new.const201.should == :const201_5 + ensure + ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST201) + ConstantSpecs::ClassB.send(:remove_const, :CS_CONST201) + ConstantSpecs::ParentB.send(:remove_const, :CS_CONST201) + ConstantSpecs::ContainerB.send(:remove_const, :CS_CONST201) + ConstantSpecs::ContainerB::ChildB.send(:remove_const, :CS_CONST201) end it "searches a module included in the immediate class before the superclass" do @@ -300,6 +332,9 @@ class ConstantSpecs::ClassB; CS_CONST201 = :const201_2; end ConstantSpecs::ContainerB::ChildB.const202.should == :const202_1 ConstantSpecs::ContainerB::ChildB.new.const202.should == :const202_1 + ensure + ConstantSpecs::ParentB.send(:remove_const, :CS_CONST202) + ConstantSpecs::ContainerB::ChildB.send(:remove_const, :CS_CONST202) end it "searches the superclass before a module included in the superclass" do @@ -308,6 +343,9 @@ class ConstantSpecs::ClassB; CS_CONST201 = :const201_2; end ConstantSpecs::ContainerB::ChildB.const203.should == :const203_1 ConstantSpecs::ContainerB::ChildB.new.const203.should == :const203_1 + ensure + ConstantSpecs::ParentB.send(:remove_const, :CS_CONST203) + ConstantSpecs::ModuleE.send(:remove_const, :CS_CONST203) end it "searches a module included in the superclass" do @@ -316,6 +354,9 @@ class ConstantSpecs::ClassB; CS_CONST201 = :const201_2; end ConstantSpecs::ContainerB::ChildB.const204.should == :const204_1 ConstantSpecs::ContainerB::ChildB.new.const204.should == :const204_1 + ensure + ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST204) + ConstantSpecs::ModuleE.send(:remove_const, :CS_CONST204) end it "searches the superclass chain" do @@ -323,6 +364,8 @@ class ConstantSpecs::ClassB; CS_CONST201 = :const201_2; end ConstantSpecs::ContainerB::ChildB.const205.should == :const205 ConstantSpecs::ContainerB::ChildB.new.const205.should == :const205 + ensure + ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST205) end it "searches the lexical scope of the method not the receiver's immediate class" do @@ -334,6 +377,9 @@ class << self end ConstantSpecs::ContainerB::ChildB.const206.should == :const206_1 + ensure + ConstantSpecs::ContainerB::ChildB.send(:remove_const, :CS_CONST206) + ConstantSpecs::ContainerB::ChildB.singleton_class.send(:remove_const, :CS_CONST206) end it "searches the lexical scope of a singleton method" do @@ -341,12 +387,17 @@ class << self ConstantSpecs::ClassB::CS_CONST207 = :const207_2 ConstantSpecs::CS_CONST208.const207.should == :const207_1 + ensure + ConstantSpecs.send(:remove_const, :CS_CONST207) + ConstantSpecs::ClassB.send(:remove_const, :CS_CONST207) end it "does not search the lexical scope of the caller" do ConstantSpecs::ClassB::CS_CONST209 = :const209 -> { ConstantSpecs::ClassB.const209 }.should raise_error(NameError) + ensure + ConstantSpecs::ClassB.send(:remove_const, :CS_CONST209) end it "searches the lexical scope of a block" do @@ -354,6 +405,9 @@ class << self ConstantSpecs::ParentB::CS_CONST210 = :const210_2 ConstantSpecs::ClassB.const210.should == :const210_1 + ensure + ConstantSpecs::ClassB.send(:remove_const, :CS_CONST210) + ConstantSpecs::ParentB.send(:remove_const, :CS_CONST210) end it "searches Object as a lexical scope only if Object is explicitly opened" do @@ -364,6 +418,11 @@ class << self Object::CS_CONST212 = :const212_2 ConstantSpecs::ParentB::CS_CONST212 = :const212_1 ConstantSpecs::ContainerB::ChildB.const212.should == :const212_1 + ensure + Object.send(:remove_const, :CS_CONST211) + ConstantSpecs::ParentB.send(:remove_const, :CS_CONST211) + Object.send(:remove_const, :CS_CONST212) + ConstantSpecs::ParentB.send(:remove_const, :CS_CONST212) end it "returns the updated value when a constant is reassigned" do @@ -376,6 +435,8 @@ class << self }.should complain(/already initialized constant/) ConstantSpecs::ContainerB::ChildB.const213.should == :const213_2 ConstantSpecs::ContainerB::ChildB.new.const213.should == :const213_2 + ensure + ConstantSpecs::ParentB.send(:remove_const, :CS_CONST213) end it "does not search the lexical scope of qualifying modules" do @@ -384,6 +445,8 @@ class << self -> do ConstantSpecs::ContainerB::ChildB.const214 end.should raise_error(NameError) + ensure + ConstantSpecs::ContainerB.send(:remove_const, :CS_CONST214) end end @@ -484,6 +547,10 @@ module PrivateModule PrivateModule::X.should == 1 end + ensure + module ::ConstantVisibility::ModuleContainer + PrivateModule.send(:remove_const, :X) + end end it "can be reopened as a class where constant is not private" do @@ -494,6 +561,10 @@ class PrivateClass PrivateClass::X.should == 1 end + ensure + module ::ConstantVisibility::ModuleContainer + PrivateClass.send(:remove_const, :X) + end end it "is not defined? with A::B form" do @@ -565,6 +636,10 @@ module PrivateModule PrivateModule::X.should == 1 end + ensure + class ::ConstantVisibility::ClassContainer + PrivateModule.send(:remove_const, :X) + end end it "can be reopened as a class where constant is not private" do @@ -575,6 +650,10 @@ class PrivateClass PrivateClass::X.should == 1 end + ensure + class ::ConstantVisibility::ClassContainer + PrivateClass.send(:remove_const, :X) + end end it "is not defined? with A::B form" do diff --git a/language/def_spec.rb b/language/def_spec.rb index c8531343c0..97071e6afb 100644 --- a/language/def_spec.rb +++ b/language/def_spec.rb @@ -514,6 +514,8 @@ def inherited_method;self;end obj = DefSpecNested.new obj.inherited_method.should == obj + ensure + DefSpecNested.send(:remove_const, :TARGET) end # See http://yugui.jp/articles/846#label-3 @@ -535,6 +537,8 @@ def body_method; end DefSpecNested.should_not have_instance_method :arg_method DefSpecNested.should_not have_instance_method :body_method + ensure + DefSpecNested.send(:remove_const, :OBJ) end it "creates an instance method inside Class.new" do diff --git a/language/module_spec.rb b/language/module_spec.rb index fffcf9c90d..0bfd148868 100644 --- a/language/module_spec.rb +++ b/language/module_spec.rb @@ -26,12 +26,16 @@ module m::N; end it "reopens an existing module" do module ModuleSpecs; Reopened = true; end ModuleSpecs::Reopened.should be_true + ensure + ModuleSpecs.send(:remove_const, :Reopened) end ruby_version_is '3.2' do it "does not reopen a module included in Object" do module IncludedModuleSpecs; Reopened = true; end ModuleSpecs::IncludedInObject::IncludedModuleSpecs.should_not == Object::IncludedModuleSpecs + ensure + IncludedModuleSpecs.send(:remove_const, :Reopened) end end @@ -76,6 +80,8 @@ module ModuleSpecs::Modules::Klass; end ::ModuleSpecs_CS1 = mod mod.name.should == "ModuleSpecs_CS1" + ensure + Object.send(:remove_const, :ModuleSpecs_CS1) end it "sets the name of a module scoped by an anonymous module" do @@ -96,5 +102,7 @@ module ModuleSpecs::Modules::Klass; end b.name.should == "ModuleSpecs_CS2::B" c.name.should == "ModuleSpecs_CS2::B::C" d.name.should == "ModuleSpecs_CS2::D" + ensure + Object.send(:remove_const, :ModuleSpecs_CS2) end end diff --git a/language/optional_assignments_spec.rb b/language/optional_assignments_spec.rb index 2443cc6b79..5fe3e3671b 100644 --- a/language/optional_assignments_spec.rb +++ b/language/optional_assignments_spec.rb @@ -698,6 +698,8 @@ module ConstantSpecs x.should == 1 y.should == 0 ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT3.should == nil + ensure + ConstantSpecs::ClassA.send(:remove_const, :NIL_OR_ASSIGNED_CONSTANT3) end end diff --git a/language/variables_spec.rb b/language/variables_spec.rb index 53d191b456..b4ba2e1b3b 100644 --- a/language/variables_spec.rb +++ b/language/variables_spec.rb @@ -381,6 +381,9 @@ module VariableSpecs SINGLE_RHS_1, SINGLE_RHS_2 = 1 [SINGLE_RHS_1, SINGLE_RHS_2].should == [1, nil] end + ensure + VariableSpecs.send(:remove_const, :SINGLE_RHS_1) + VariableSpecs.send(:remove_const, :SINGLE_RHS_2) end end @@ -619,6 +622,8 @@ module VariableSpecs (*SINGLE_SPLATTED_RHS) = *1 SINGLE_SPLATTED_RHS.should == [1] end + ensure + VariableSpecs.send(:remove_const, :SINGLE_SPLATTED_RHS) end end @@ -818,6 +823,9 @@ module VariableSpecs MRHS_VALUES_1.should == 1 MRHS_VALUES_2.should == 2 end + ensure + VariableSpecs.send(:remove_const, :MRHS_VALUES_1) + VariableSpecs.send(:remove_const, :MRHS_VALUES_2) end it "assigns all RHS values as an array to a single LHS constant" do @@ -825,6 +833,8 @@ module VariableSpecs MRHS_VALUES = 1, 2, 3 MRHS_VALUES.should == [1, 2, 3] end + ensure + VariableSpecs.send(:remove_const, :MRHS_VALUES) end end diff --git a/library/erb/def_class_spec.rb b/library/erb/def_class_spec.rb index 88bd385f4c..fb687531e0 100644 --- a/library/erb/def_class_spec.rb +++ b/library/erb/def_class_spec.rb @@ -24,6 +24,8 @@ def initialize(arg1, arg2) MyClass1ForErb = erb.def_class(MyClass1ForErb_, 'render()') MyClass1ForErb.method_defined?(:render).should == true MyClass1ForErb.new('foo', 123).render().should == expected + ensure + Object.send(:remove_const, :MyClass1ForErb) end end diff --git a/library/erb/def_module_spec.rb b/library/erb/def_module_spec.rb index 806e564ef0..5f67aeb2b9 100644 --- a/library/erb/def_module_spec.rb +++ b/library/erb/def_module_spec.rb @@ -22,6 +22,9 @@ class MyClass2ForErb include MyModule2ForErb end MyClass2ForErb.new.render('foo', 123).should == expected + ensure + Object.send(:remove_const, :MyClass2ForErb) + Object.send(:remove_const, :MyModule2ForErb) end end diff --git a/library/erb/defmethod/def_erb_method_spec.rb b/library/erb/defmethod/def_erb_method_spec.rb index dc1e044d9c..1cd7582936 100644 --- a/library/erb/defmethod/def_erb_method_spec.rb +++ b/library/erb/defmethod/def_erb_method_spec.rb @@ -58,6 +58,8 @@ def initialize(items) end end MyClass4ForErb.new([10,20,30]).render().should == expected + ensure + Object.send(:remove_const, :MY_INPUT4_FOR_ERB) end diff --git a/library/yaml/to_yaml_spec.rb b/library/yaml/to_yaml_spec.rb index 547009c942..08c5451416 100644 --- a/library/yaml/to_yaml_spec.rb +++ b/library/yaml/to_yaml_spec.rb @@ -65,6 +65,8 @@ it "returns the YAML representation of a Struct object" do Person = Struct.new(:name, :gender) Person.new("Jane", "female").to_yaml.should match_yaml("--- !ruby/struct:Person\nname: Jane\ngender: female\n") + ensure + Object.send(:remove_const, :Person) end it "returns the YAML representation of an unnamed Struct object" do diff --git a/optional/capi/class_spec.rb b/optional/capi/class_spec.rb index d0a9913570..b5bc923f16 100644 --- a/optional/capi/class_spec.rb +++ b/optional/capi/class_spec.rb @@ -365,6 +365,8 @@ "ClassUnder6", Class.new) }.should raise_error(TypeError) + ensure + CApiClassSpecs.send(:remove_const, :ClassUnder6) end it "defines a class for an existing Autoload" do diff --git a/optional/capi/module_spec.rb b/optional/capi/module_spec.rb index d7c0ab9c52..b7684e566b 100644 --- a/optional/capi/module_spec.rb +++ b/optional/capi/module_spec.rb @@ -22,6 +22,8 @@ it "sets a new constant on a module" do @m.rb_const_set(CApiModuleSpecs::C, :W, 7) CApiModuleSpecs::C::W.should == 7 + ensure + CApiModuleSpecs::C.send(:remove_const, :W) end it "sets an existing constant's value" do @@ -93,6 +95,8 @@ it "defines a new constant on a module" do @m.rb_define_const(CApiModuleSpecs::C, "V", 7) CApiModuleSpecs::C::V.should == 7 + ensure + CApiModuleSpecs::C.send(:remove_const, :V) end it "sets an existing constant's value" do