From adeb5ff0ab642d9b3d2f24d63873f3b8cc163f11 Mon Sep 17 00:00:00 2001 From: Dmitriy Ivliev Date: Thu, 5 Nov 2020 17:52:29 +0200 Subject: [PATCH] adds dynamiclly assigned examples --- core/module/const_source_location_spec.rb | 113 +++++++++++++++++----- 1 file changed, 88 insertions(+), 25 deletions(-) diff --git a/core/module/const_source_location_spec.rb b/core/module/const_source_location_spec.rb index 602dd24a8..548be3241 100644 --- a/core/module/const_source_location_spec.rb +++ b/core/module/const_source_location_spec.rb @@ -7,38 +7,101 @@ @constants_fixture_path = File.expand_path('../../fixtures/constants.rb', __dir__) end - describe "with statically assigned constants" do - it "searches location path the immediate class or module first" do - ConstantSpecs::ClassA.const_source_location(:CS_CONST10).should == [@constants_fixture_path, 78] - ConstantSpecs::ModuleA.const_source_location(:CS_CONST10).should == [@constants_fixture_path, 36] - ConstantSpecs::ParentA.const_source_location(:CS_CONST10).should == [@constants_fixture_path, 100] - ConstantSpecs::ContainerA.const_source_location(:CS_CONST10).should == [@constants_fixture_path, 120] - ConstantSpecs::ContainerA::ChildA.const_source_location(:CS_CONST10).should == [@constants_fixture_path, 137] - end + ruby_version_is "2.7" do + describe "with statically assigned constants" do + it "searches location path the immediate class or module first" do + ConstantSpecs::ClassA.const_source_location(:CS_CONST10).should == [@constants_fixture_path, 78] + ConstantSpecs::ModuleA.const_source_location(:CS_CONST10).should == [@constants_fixture_path, 36] + ConstantSpecs::ParentA.const_source_location(:CS_CONST10).should == [@constants_fixture_path, 100] + ConstantSpecs::ContainerA.const_source_location(:CS_CONST10).should == [@constants_fixture_path, 120] + ConstantSpecs::ContainerA::ChildA.const_source_location(:CS_CONST10).should == [@constants_fixture_path, 137] + end - it "searches location path a module included in the immediate class before the superclass" do - ConstantSpecs::ContainerA::ChildA.const_source_location(:CS_CONST15).should == [@constants_fixture_path, 52] - end + it "searches location path a module included in the immediate class before the superclass" do + ConstantSpecs::ContainerA::ChildA.const_source_location(:CS_CONST15).should == [@constants_fixture_path, 52] + end - it "searches location path the superclass before a module included in the superclass" do - ConstantSpecs::ContainerA::ChildA.const_source_location(:CS_CONST11).should == [@constants_fixture_path, 101] - end + it "searches location path the superclass before a module included in the superclass" do + ConstantSpecs::ContainerA::ChildA.const_source_location(:CS_CONST11).should == [@constants_fixture_path, 101] + end - it "searches location path a module included in the superclass" do - ConstantSpecs::ContainerA::ChildA.const_source_location(:CS_CONST12).should == [@constants_fixture_path, 46] - end + it "searches location path a module included in the superclass" do + ConstantSpecs::ContainerA::ChildA.const_source_location(:CS_CONST12).should == [@constants_fixture_path, 46] + end - it "searches location path the superclass chain" do - ConstantSpecs::ContainerA::ChildA.const_source_location(:CS_CONST13).should == [@constants_fixture_path, 38] - end + it "searches location path the superclass chain" do + ConstantSpecs::ContainerA::ChildA.const_source_location(:CS_CONST13).should == [@constants_fixture_path, 38] + end - it "returns location path a toplevel constant when the receiver is a Class" do - ConstantSpecs::ContainerA::ChildA.const_source_location(:CS_CONST1).should == [@constants_fixture_path, 30] + it "returns location path a toplevel constant when the receiver is a Class" do + ConstantSpecs::ContainerA::ChildA.const_source_location(:CS_CONST1).should == [@constants_fixture_path, 30] + end + + it "returns location path a toplevel constant when the receiver is a Module" do + ConstantSpecs.const_source_location(:CS_CONST1).should == [@constants_fixture_path, 30] + ConstantSpecs::ModuleA.const_source_location(:CS_CONST1).should == [@constants_fixture_path, 30] + end end - it "returns location path a toplevel constant when the receiver is a Module" do - ConstantSpecs.const_source_location(:CS_CONST1).should == [@constants_fixture_path, 30] - ConstantSpecs::ModuleA.const_source_location(:CS_CONST1).should == [@constants_fixture_path, 30] + describe "with dynamically assigned constants" do + it "searches a path in the immediate class or module first" do + ConstantSpecs::ClassA::CS_CONST301 = :const301_1 + ConstantSpecs::ClassA.const_source_location(:CS_CONST301).should == [__FILE__, 48] + + ConstantSpecs::ModuleA::CS_CONST301 = :const301_2 + ConstantSpecs::ModuleA.const_source_location(:CS_CONST301).should == [__FILE__, 51] + + ConstantSpecs::ParentA::CS_CONST301 = :const301_3 + ConstantSpecs::ParentA.const_source_location(:CS_CONST301).should == [__FILE__, 54] + + ConstantSpecs::ContainerA::ChildA::CS_CONST301 = :const301_5 + ConstantSpecs::ContainerA::ChildA.const_source_location(:CS_CONST301).should == [__FILE__, 57] + end + + it "searches a path in 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_source_location(:CS_CONST302).should == [__FILE__, 63] + end + + it "searches a path in 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_source_location(:CS_CONST303).should == [__FILE__, 69] + end + + it "searches a path in a module included in the superclass" do + ConstantSpecs::ModuleA::CS_CONST304 = :const304_1 + ConstantSpecs::ModuleE::CS_CONST304 = :const304_2 + ConstantSpecs::ContainerB::ChildB.const_source_location(:CS_CONST304).should == [__FILE__, 75] + end + + it "searches a path in the superclass chain" do + ConstantSpecs::ModuleA::CS_CONST305 = :const305 + ConstantSpecs::ContainerB::ChildB.const_source_location(:CS_CONST305).should == [__FILE__, 80] + end + + it "returns path to a toplevel constant when the receiver is a Class" do + Object::CS_CONST306 = :const306 + ConstantSpecs::ContainerB::ChildB.const_source_location(:CS_CONST306).should == [__FILE__, 85] + end + + it "returns path to a toplevel constant when the receiver is a Module" do + Object::CS_CONST308 = :const308 + ConstantSpecs.const_source_location(:CS_CONST308).should == [__FILE__, 90] + ConstantSpecs::ModuleA.const_source_location(:CS_CONST308).should == [__FILE__, 90] + end + + it "returns path to the updated value of a constant" do + ConstantSpecs::ClassB::CS_CONST309 = :const309_1 + ConstantSpecs::ClassB.const_source_location(:CS_CONST309).should == [__FILE__, 96] + + -> { + ConstantSpecs::ClassB::CS_CONST309 = :const309_2 + }.should complain(/already initialized constant/) + + ConstantSpecs::ClassB.const_source_location(:CS_CONST309).should == [__FILE__, 100] + end end end end