diff options
Diffstat (limited to 'spec/ruby/core/module/const_defined_spec.rb')
| -rw-r--r-- | spec/ruby/core/module/const_defined_spec.rb | 51 |
1 files changed, 38 insertions, 13 deletions
diff --git a/spec/ruby/core/module/const_defined_spec.rb b/spec/ruby/core/module/const_defined_spec.rb index cb1674c7e7..8b137cd134 100644 --- a/spec/ruby/core/module/const_defined_spec.rb +++ b/spec/ruby/core/module/const_defined_spec.rb @@ -17,11 +17,16 @@ describe "Module#const_defined?" do ConstantSpecs::ContainerA::ChildA.const_defined?(:CS_CONST4).should be_true end - it "returns true if the constant is defined in a mixed-in module of the receiver" do + it "returns true if the constant is defined in a mixed-in module of the receiver's parent" do # CS_CONST10 is defined in a module included by ChildA ConstantSpecs::ContainerA::ChildA.const_defined?(:CS_CONST10).should be_true end + it "returns true if the constant is defined in a mixed-in module (with prepends) of the receiver" do + # CS_CONST11 is defined in the module included by ContainerPrepend + ConstantSpecs::ContainerPrepend.const_defined?(:CS_CONST11).should be_true + end + it "returns true if the constant is defined in Object and the receiver is a module" do # CS_CONST1 is defined in Object ConstantSpecs::ModuleA.const_defined?(:CS_CONST1).should be_true @@ -40,6 +45,11 @@ describe "Module#const_defined?" do ConstantSpecs::ContainerA::ChildA.const_defined?(:CS_CONST4, true).should be_true end + it "coerces the inherit flag to a boolean" do + ConstantSpecs::ContainerA::ChildA.const_defined?(:CS_CONST4, nil).should be_false + ConstantSpecs::ContainerA::ChildA.const_defined?(:CS_CONST4, :true).should be_true + end + it "returns true if the given String names a constant defined in the receiver" do ConstantSpecs.const_defined?("CS_CONST2").should == true ConstantSpecs.const_defined?("ModuleA").should == true @@ -55,6 +65,8 @@ describe "Module#const_defined?" do 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 @@ -70,10 +82,23 @@ describe "Module#const_defined?" do ConstantSpecs::ClassA.const_defined?(:CS_CONSTX).should == false end - it "calls #to_str to convert the given name to a String" do - name = mock("ClassA") - name.should_receive(:to_str).and_return("ClassA") - ConstantSpecs.const_defined?(name).should == true + describe "converts the given name to a String using #to_str" do + it "calls #to_str to convert the given name to a String" do + name = mock("ClassA") + name.should_receive(:to_str).and_return("ClassA") + ConstantSpecs.const_defined?(name).should == true + end + + it "raises a TypeError if the given name can't be converted to a String" do + -> { ConstantSpecs.const_defined?(nil) }.should raise_error(TypeError) + -> { ConstantSpecs.const_defined?([]) }.should raise_error(TypeError) + end + + it "raises a NoMethodError if the given argument raises a NoMethodError during type coercion to a String" do + name = mock("classA") + name.should_receive(:to_str).and_raise(NoMethodError) + -> { ConstantSpecs.const_defined?(name) }.should raise_error(NoMethodError) + end end it "special cases Object and checks it's included Modules" do @@ -105,19 +130,19 @@ describe "Module#const_defined?" do end it "raises a NameError if the name does not start with a capital letter" do - lambda { ConstantSpecs.const_defined? "name" }.should raise_error(NameError) + -> { ConstantSpecs.const_defined? "name" }.should raise_error(NameError) end it "raises a NameError if the name starts with '_'" do - lambda { ConstantSpecs.const_defined? "__CONSTX__" }.should raise_error(NameError) + -> { ConstantSpecs.const_defined? "__CONSTX__" }.should raise_error(NameError) end it "raises a NameError if the name starts with '@'" do - lambda { ConstantSpecs.const_defined? "@Name" }.should raise_error(NameError) + -> { ConstantSpecs.const_defined? "@Name" }.should raise_error(NameError) end it "raises a NameError if the name starts with '!'" do - lambda { ConstantSpecs.const_defined? "!Name" }.should raise_error(NameError) + -> { ConstantSpecs.const_defined? "!Name" }.should raise_error(NameError) end it "returns true or false for the nested name" do @@ -130,15 +155,15 @@ describe "Module#const_defined?" do it "raises a NameError if the name contains non-alphabetic characters except '_'" do ConstantSpecs.const_defined?("CS_CONSTX").should == false - lambda { ConstantSpecs.const_defined? "Name=" }.should raise_error(NameError) - lambda { ConstantSpecs.const_defined? "Name?" }.should raise_error(NameError) + -> { ConstantSpecs.const_defined? "Name=" }.should raise_error(NameError) + -> { ConstantSpecs.const_defined? "Name?" }.should raise_error(NameError) end it "raises a TypeError if conversion to a String by calling #to_str fails" do name = mock('123') - lambda { ConstantSpecs.const_defined? name }.should raise_error(TypeError) + -> { ConstantSpecs.const_defined? name }.should raise_error(TypeError) name.should_receive(:to_str).and_return(123) - lambda { ConstantSpecs.const_defined? name }.should raise_error(TypeError) + -> { ConstantSpecs.const_defined? name }.should raise_error(TypeError) end end |
