diff options
Diffstat (limited to 'spec/ruby/core/basicobject/instance_eval_spec.rb')
| -rw-r--r-- | spec/ruby/core/basicobject/instance_eval_spec.rb | 79 |
1 files changed, 33 insertions, 46 deletions
diff --git a/spec/ruby/core/basicobject/instance_eval_spec.rb b/spec/ruby/core/basicobject/instance_eval_spec.rb index 1f3a43f341..124d179b5a 100644 --- a/spec/ruby/core/basicobject/instance_eval_spec.rb +++ b/spec/ruby/core/basicobject/instance_eval_spec.rb @@ -7,31 +7,31 @@ describe "BasicObject#instance_eval" do end it "is a public instance method" do - BasicObject.should have_public_instance_method(:instance_eval) + BasicObject.public_instance_methods(false).should.include?(:instance_eval) end it "sets self to the receiver in the context of the passed block" do a = BasicObject.new - a.instance_eval { self }.equal?(a).should be_true + a.instance_eval { self }.equal?(a).should == true end it "evaluates strings" do a = BasicObject.new - a.instance_eval('self').equal?(a).should be_true + a.instance_eval('self').equal?(a).should == true end it "raises an ArgumentError when no arguments and no block are given" do - -> { "hola".instance_eval }.should raise_error(ArgumentError, "wrong number of arguments (given 0, expected 1..3)") + -> { "hola".instance_eval }.should.raise(ArgumentError, "wrong number of arguments (given 0, expected 1..3)") end it "raises an ArgumentError when a block and normal arguments are given" do - -> { "hola".instance_eval(4, 5) {|a,b| a + b } }.should raise_error(ArgumentError, "wrong number of arguments (given 2, expected 0)") + -> { "hola".instance_eval(4, 5) {|a,b| a + b } }.should.raise(ArgumentError, "wrong number of arguments (given 2, expected 0)") end it "raises an ArgumentError when more than 3 arguments are given" do -> { "hola".instance_eval("1 + 1", "some file", 0, "bogus") - }.should raise_error(ArgumentError, "wrong number of arguments (given 4, expected 1..3)") + }.should.raise(ArgumentError, "wrong number of arguments (given 4, expected 1..3)") end it "yields the object to the block" do @@ -51,7 +51,7 @@ describe "BasicObject#instance_eval" do end end f.foo.should == 1 - -> { Object.new.foo }.should raise_error(NoMethodError) + -> { Object.new.foo }.should.raise(NoMethodError) end it "preserves self in the original block when passed a block argument" do @@ -71,9 +71,9 @@ describe "BasicObject#instance_eval" do it "binds self to the receiver" do s = "hola" - (s == s.instance_eval { self }).should be_true + (s == s.instance_eval { self }).should == true o = mock('o') - (o == o.instance_eval("self")).should be_true + (o == o.instance_eval("self")).should == true end it "executes in the context of the receiver" do @@ -84,11 +84,9 @@ describe "BasicObject#instance_eval" do end - ruby_version_is "3.3" do - it "uses the caller location as default location" do - f = Object.new - f.instance_eval("[__FILE__, __LINE__]").should == ["(eval at #{__FILE__}:#{__LINE__})", 1] - end + it "uses the caller location as default location" do + f = Object.new + f.instance_eval("[__FILE__, __LINE__]").should == ["(eval at #{__FILE__}:#{__LINE__})", 1] end it "has access to receiver's instance variables" do @@ -97,15 +95,15 @@ describe "BasicObject#instance_eval" do end it "raises TypeError for frozen objects when tries to set receiver's instance variables" do - -> { nil.instance_eval { @foo = 42 } }.should raise_error(FrozenError, "can't modify frozen NilClass: nil") - -> { true.instance_eval { @foo = 42 } }.should raise_error(FrozenError, "can't modify frozen TrueClass: true") - -> { false.instance_eval { @foo = 42 } }.should raise_error(FrozenError, "can't modify frozen FalseClass: false") - -> { 1.instance_eval { @foo = 42 } }.should raise_error(FrozenError, "can't modify frozen Integer: 1") - -> { :symbol.instance_eval { @foo = 42 } }.should raise_error(FrozenError, "can't modify frozen Symbol: :symbol") + -> { nil.instance_eval { @foo = 42 } }.should.raise(FrozenError, "can't modify frozen NilClass: nil") + -> { true.instance_eval { @foo = 42 } }.should.raise(FrozenError, "can't modify frozen TrueClass: true") + -> { false.instance_eval { @foo = 42 } }.should.raise(FrozenError, "can't modify frozen FalseClass: false") + -> { 1.instance_eval { @foo = 42 } }.should.raise(FrozenError, "can't modify frozen Integer: 1") + -> { :symbol.instance_eval { @foo = 42 } }.should.raise(FrozenError, "can't modify frozen Symbol: :symbol") obj = Object.new obj.freeze - -> { obj.instance_eval { @foo = 42 } }.should raise_error(FrozenError) + -> { obj.instance_eval { @foo = 42 } }.should.raise(FrozenError) end it "treats block-local variables as local to the block" do @@ -130,7 +128,7 @@ describe "BasicObject#instance_eval" do class B; end B } - obj.singleton_class.const_get(:B).should be_an_instance_of(Class) + obj.singleton_class.const_get(:B).should.instance_of?(Class) end describe "constants lookup when a String given" do @@ -141,22 +139,11 @@ describe "BasicObject#instance_eval" do caller.get_constant_with_string(receiver).should == :singleton_class end - ruby_version_is ""..."3.1" do - it "looks in the caller scope next" do - receiver = BasicObjectSpecs::InstEval::Constants::ConstantInReceiverClass::ReceiverScope::Receiver.new - caller = BasicObjectSpecs::InstEval::Constants::ConstantInReceiverClass::CallerScope::Caller.new + it "looks in the receiver class next" do + receiver = BasicObjectSpecs::InstEval::Constants::ConstantInReceiverClass::ReceiverScope::Receiver.new + caller = BasicObjectSpecs::InstEval::Constants::ConstantInReceiverClass::CallerScope::Caller.new - caller.get_constant_with_string(receiver).should == :Caller - end - end - - ruby_version_is "3.1" do - it "looks in the receiver class next" do - receiver = BasicObjectSpecs::InstEval::Constants::ConstantInReceiverClass::ReceiverScope::Receiver.new - caller = BasicObjectSpecs::InstEval::Constants::ConstantInReceiverClass::CallerScope::Caller.new - - caller.get_constant_with_string(receiver).should == :Receiver - end + caller.get_constant_with_string(receiver).should == :Receiver end it "looks in the caller class next" do @@ -182,16 +169,16 @@ describe "BasicObject#instance_eval" do end it "doesn't get constants in the receiver if a block given" do - BasicObjectSpecs::InstEvalOuter::Inner::X_BY_BLOCK.should be_nil + BasicObjectSpecs::InstEvalOuter::Inner::X_BY_BLOCK.should == nil end it "raises a TypeError when defining methods on an immediate" do -> do 1.instance_eval { def foo; end } - end.should raise_error(TypeError) + end.should.raise(TypeError) -> do :foo.instance_eval { def foo; end } - end.should raise_error(TypeError) + end.should.raise(TypeError) end describe "class variables lookup" do @@ -230,23 +217,23 @@ describe "BasicObject#instance_eval" do it "does not have access to class variables in the receiver class when called with a String" do receiver = BasicObjectSpecs::InstEval::CVar::Get::ReceiverScope.new caller = BasicObjectSpecs::InstEval::CVar::Get::CallerWithoutCVarScope.new - -> { caller.get_class_variable_with_string(receiver) }.should raise_error(NameError, /uninitialized class variable @@cvar/) + -> { caller.get_class_variable_with_string(receiver) }.should.raise(NameError, /uninitialized class variable @@cvar/) end it "does not have access to class variables in the receiver's singleton class when called with a String" do receiver = BasicObjectSpecs::InstEval::CVar::Get::ReceiverWithCVarDefinedInSingletonClass caller = BasicObjectSpecs::InstEval::CVar::Get::CallerWithoutCVarScope.new - -> { caller.get_class_variable_with_string(receiver) }.should raise_error(NameError, /uninitialized class variable @@cvar/) + -> { caller.get_class_variable_with_string(receiver) }.should.raise(NameError, /uninitialized class variable @@cvar/) end end it "raises a TypeError when defining methods on numerics" do -> do (1.0).instance_eval { def foo; end } - end.should raise_error(TypeError) + end.should.raise(TypeError) -> do (1 << 64).instance_eval { def foo; end } - end.should raise_error(TypeError) + end.should.raise(TypeError) end it "evaluates procs originating from methods" do @@ -297,7 +284,7 @@ describe "BasicObject#instance_eval" do def source_code.to_str() :symbol end a = BasicObject.new - -> { a.instance_eval(source_code) }.should raise_error(TypeError, /can't convert Object to String/) + -> { a.instance_eval(source_code) }.should raise_consistent_error(TypeError, /can't convert Object into String/) end it "converts filename argument with #to_str method" do @@ -316,7 +303,7 @@ describe "BasicObject#instance_eval" do filename = Object.new def filename.to_str() :symbol end - -> { Object.new.instance_eval("raise", filename) }.should raise_error(TypeError, /can't convert Object to String/) + -> { Object.new.instance_eval("raise", filename) }.should raise_consistent_error(TypeError, /can't convert Object into String/) end it "converts lineno argument with #to_int method" do @@ -335,6 +322,6 @@ describe "BasicObject#instance_eval" do lineno = Object.new def lineno.to_int() :symbol end - -> { Object.new.instance_eval("raise", "file.rb", lineno) }.should raise_error(TypeError, /can't convert Object to Integer/) + -> { Object.new.instance_eval("raise", "file.rb", lineno) }.should raise_consistent_error(TypeError, /can't convert Object into Integer/) end end |
