diff options
Diffstat (limited to 'spec/ruby/optional/capi/numeric_spec.rb')
| -rw-r--r-- | spec/ruby/optional/capi/numeric_spec.rb | 130 |
1 files changed, 70 insertions, 60 deletions
diff --git a/spec/ruby/optional/capi/numeric_spec.rb b/spec/ruby/optional/capi/numeric_spec.rb index d76f353850..abfed13503 100644 --- a/spec/ruby/optional/capi/numeric_spec.rb +++ b/spec/ruby/optional/capi/numeric_spec.rb @@ -9,7 +9,7 @@ describe "CApiNumericSpecs" do describe "NUM2INT" do it "raises a TypeError if passed nil" do - -> { @s.NUM2INT(nil) }.should raise_error(TypeError) + -> { @s.NUM2INT(nil) }.should.raise(TypeError) end it "converts a Float" do @@ -33,7 +33,7 @@ describe "CApiNumericSpecs" do end it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2INT(0xffff_ffff+1) }.should raise_error(RangeError) + -> { @s.NUM2INT(0xffff_ffff+1) }.should.raise(RangeError) end it "calls #to_int to coerce the value" do @@ -45,7 +45,7 @@ describe "CApiNumericSpecs" do describe "NUM2UINT" do it "raises a TypeError if passed nil" do - -> { @s.NUM2UINT(nil) }.should raise_error(TypeError) + -> { @s.NUM2UINT(nil) }.should.raise(TypeError) end it "converts a Float" do @@ -69,17 +69,17 @@ describe "CApiNumericSpecs" do end it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2UINT(0xffff_ffff+1) }.should raise_error(RangeError) + -> { @s.NUM2UINT(0xffff_ffff+1) }.should.raise(RangeError) end it "raises a RangeError if the value is less than 32bits negative" do - -> { @s.NUM2UINT(-0x8000_0000-1) }.should raise_error(RangeError) + -> { @s.NUM2UINT(-0x8000_0000-1) }.should.raise(RangeError) end it "raises a RangeError if the value is more than 64bits" do -> do @s.NUM2UINT(0xffff_ffff_ffff_ffff+1) - end.should raise_error(RangeError) + end.should.raise(RangeError) end it "calls #to_int to coerce the value" do @@ -91,7 +91,7 @@ describe "CApiNumericSpecs" do describe "NUM2LONG" do it "raises a TypeError if passed nil" do - -> { @s.NUM2LONG(nil) }.should raise_error(TypeError) + -> { @s.NUM2LONG(nil) }.should.raise(TypeError) end it "converts a Float" do @@ -106,7 +106,7 @@ describe "CApiNumericSpecs" do @s.NUM2LONG(5).should == 5 end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do it "converts -1 to an signed number" do @s.NUM2LONG(-1).should == -1 end @@ -116,11 +116,11 @@ describe "CApiNumericSpecs" do end it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2LONG(0xffff_ffff+1) }.should raise_error(RangeError) + -> { @s.NUM2LONG(0xffff_ffff+1) }.should.raise(RangeError) end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do it "converts -1 to an signed number" do @s.NUM2LONG(-1).should == -1 end @@ -132,7 +132,7 @@ describe "CApiNumericSpecs" do it "raises a RangeError if the value is more than 64bits" do -> do @s.NUM2LONG(0xffff_ffff_ffff_ffff+1) - end.should raise_error(RangeError) + end.should.raise(RangeError) end end @@ -143,9 +143,37 @@ describe "CApiNumericSpecs" do end end + describe "NUM2SHORT" do + it "raises a TypeError if passed nil" do + -> { @s.NUM2SHORT(nil) }.should.raise(TypeError) + end + + it "converts a Float" do + @s.NUM2SHORT(4.2).should == 4 + end + + it "converts a Fixnum" do + @s.NUM2SHORT(5).should == 5 + end + + it "converts -1 to an signed number" do + @s.NUM2SHORT(-1).should == -1 + end + + it "raises a RangeError if the value is more than 32bits" do + -> { @s.NUM2SHORT(0xffff_ffff+1) }.should.raise(RangeError) + end + + it "calls #to_int to coerce the value" do + obj = mock("number") + obj.should_receive(:to_int).and_return(2) + @s.NUM2SHORT(obj).should == 2 + end + end + describe "INT2NUM" do it "raises a TypeError if passed nil" do - -> { @s.INT2NUM(nil) }.should raise_error(TypeError) + -> { @s.INT2NUM(nil) }.should.raise(TypeError) end it "converts a Float" do @@ -153,7 +181,7 @@ describe "CApiNumericSpecs" do end it "raises a RangeError when passed a Bignum" do - -> { @s.INT2NUM(bignum_value) }.should raise_error(RangeError) + -> { @s.INT2NUM(bignum_value) }.should.raise(RangeError) end it "converts a Fixnum" do @@ -167,7 +195,7 @@ describe "CApiNumericSpecs" do describe "NUM2ULONG" do it "raises a TypeError if passed nil" do - -> { @s.NUM2ULONG(nil) }.should raise_error(TypeError) + -> { @s.NUM2ULONG(nil) }.should.raise(TypeError) end it "converts a Float" do @@ -182,7 +210,7 @@ describe "CApiNumericSpecs" do @s.NUM2ULONG(5).should == 5 end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do it "converts -1 to an unsigned number" do @s.NUM2ULONG(-1).should == 4294967295 end @@ -199,11 +227,11 @@ describe "CApiNumericSpecs" do end it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2ULONG(0xffff_ffff+1) }.should raise_error(RangeError) + -> { @s.NUM2ULONG(0xffff_ffff+1) }.should.raise(RangeError) end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do it "converts -1 to an unsigned number" do @s.NUM2ULONG(-1).should == 18446744073709551615 end @@ -222,7 +250,7 @@ describe "CApiNumericSpecs" do it "raises a RangeError if the value is more than 64bits" do -> do @s.NUM2ULONG(0xffff_ffff_ffff_ffff+1) - end.should raise_error(RangeError) + end.should.raise(RangeError) end end @@ -236,62 +264,55 @@ describe "CApiNumericSpecs" do describe "rb_Integer" do it "creates an Integer from a String" do i = @s.rb_Integer("8675309") - i.should be_kind_of(Integer) - i.should eql(8675309) + i.should == 8675309 end end describe "rb_ll2inum" do it "creates a Fixnum from a small signed long long" do i = @s.rb_ll2inum_14() - i.should be_kind_of(Fixnum) - i.should eql(14) + i.should == 14 end end describe "rb_ull2inum" do it "creates a Fixnum from a small unsigned long long" do i = @s.rb_ull2inum_14() - i.should be_kind_of(Fixnum) - i.should eql(14) + i.should == 14 end it "creates a positive Bignum from a negative long long" do i = @s.rb_ull2inum_n14() - i.should be_kind_of(Bignum) - i.should eql(2 ** (@s.size_of_long_long * 8) - 14) + i.should == (2 ** (@s.size_of_long_long * 8) - 14) end end describe "rb_int2inum" do it "creates a Fixnum from a long" do i = @s.rb_int2inum_14() - i.should be_kind_of(Fixnum) - i.should eql(14) + i.should == 14 end end describe "rb_uint2inum" do it "creates a Fixnum from a long" do i = @s.rb_uint2inum_14() - i.should be_kind_of(Fixnum) - i.should eql(14) + i.should == 14 end it "creates a positive Bignum from a negative long" do i = @s.rb_uint2inum_n14() - i.should be_kind_of(Bignum) - i.should eql(2 ** (@s.size_of_VALUE * 8) - 14) + i.should == (2 ** (@s.size_of_VALUE * 8) - 14) end end describe "NUM2DBL" do it "raises a TypeError if passed nil" do - -> { @s.NUM2DBL(nil) }.should raise_error(TypeError) + -> { @s.NUM2DBL(nil) }.should.raise(TypeError) end it "raises a TypeError if passed a String" do - -> { @s.NUM2DBL("1.2") }.should raise_error(TypeError) + -> { @s.NUM2DBL("1.2") }.should.raise(TypeError) end it "converts a Float" do @@ -327,13 +348,13 @@ describe "CApiNumericSpecs" do end it "raises a TypeError when passed an empty String" do - -> { @s.NUM2CHR("") }.should raise_error(TypeError) + -> { @s.NUM2CHR("") }.should.raise(TypeError) end end describe "rb_num_zerodiv" do it "raises a RuntimeError" do - -> { @s.rb_num_zerodiv() }.should raise_error(ZeroDivisionError, 'divided by 0') + -> { @s.rb_num_zerodiv() }.should.raise(ZeroDivisionError, 'divided by 0') end end @@ -365,7 +386,7 @@ describe "CApiNumericSpecs" do it "raises an ArgumentError when passed nil" do -> { @s.rb_cmpint(nil, 4) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end end @@ -389,7 +410,7 @@ describe "CApiNumericSpecs" do obj = mock("rb_num_coerce_bin") obj.should_receive(:coerce).with(2).and_return(nil) - -> { @s.rb_num_coerce_bin(2, obj, :+) }.should raise_error(TypeError) + -> { @s.rb_num_coerce_bin(2, obj, :+) }.should.raise(TypeError) end end @@ -409,30 +430,19 @@ describe "CApiNumericSpecs" do @s.rb_num_coerce_cmp(2, obj, :<=>).should == -1 end - ruby_version_is ""..."2.5" do - it "returns nil if passed nil" do - -> { - @result = @s.rb_num_coerce_cmp(nil, 2, :<=>) - }.should complain(/comparison operators will no more rescue exceptions/) - @result.should be_nil - end - end - - ruby_version_is "2.5" do - it "lets the exception go through if #coerce raises an exception" do - obj = mock("rb_num_coerce_cmp") - obj.should_receive(:coerce).with(2).and_raise(RuntimeError.new("my error")) - -> { - @s.rb_num_coerce_cmp(2, obj, :<=>) - }.should raise_error(RuntimeError, "my error") - end + it "lets the exception go through if #coerce raises an exception" do + obj = mock("rb_num_coerce_cmp") + obj.should_receive(:coerce).with(2).and_raise(RuntimeError.new("my error")) + -> { + @s.rb_num_coerce_cmp(2, obj, :<=>) + }.should.raise(RuntimeError, "my error") end it "returns nil if #coerce does not return an Array" do obj = mock("rb_num_coerce_cmp") obj.should_receive(:coerce).with(2).and_return(nil) - @s.rb_num_coerce_cmp(2, obj, :<=>).should be_nil + @s.rb_num_coerce_cmp(2, obj, :<=>).should == nil end end @@ -441,7 +451,7 @@ describe "CApiNumericSpecs" do obj = mock("rb_num_coerce_relop") obj.should_receive(:coerce).with(2).and_return([1, 2]) - @s.rb_num_coerce_relop(2, obj, :<).should be_true + @s.rb_num_coerce_relop(2, obj, :<).should == true end it "calls the specified method on the first argument returned by #coerce" do @@ -449,7 +459,7 @@ describe "CApiNumericSpecs" do obj.should_receive(:coerce).with(2).and_return([obj, 2]) obj.should_receive(:<).with(2).and_return(false) - @s.rb_num_coerce_relop(2, obj, :<).should be_false + @s.rb_num_coerce_relop(2, obj, :<).should == false end it "raises an ArgumentError if #<op> returns nil" do @@ -457,14 +467,14 @@ describe "CApiNumericSpecs" do obj.should_receive(:coerce).with(2).and_return([obj, 2]) obj.should_receive(:<).with(2).and_return(nil) - -> { @s.rb_num_coerce_relop(2, obj, :<) }.should raise_error(ArgumentError) + -> { @s.rb_num_coerce_relop(2, obj, :<) }.should.raise(ArgumentError) end it "raises an ArgumentError if #coerce does not return an Array" do obj = mock("rb_num_coerce_relop") obj.should_receive(:coerce).with(2).and_return(nil) - -> { @s.rb_num_coerce_relop(2, obj, :<) }.should raise_error(ArgumentError) + -> { @s.rb_num_coerce_relop(2, obj, :<) }.should.raise(ArgumentError) end end |
