Skip to content

Commit 7745919

Browse files
kareseregon
authored andcommitted
[test] move regression specs into ruby/spec (#9342)
All specs verified against CRuby 3.4.8
1 parent a154f4b commit 7745919

9 files changed

Lines changed: 83 additions & 0 deletions

File tree

core/integer/comparison_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@
157157
end
158158
end
159159

160+
describe "with a Float" do
161+
it "does not lose precision for values that don't fit in a double" do
162+
(bignum_value(1) <=> bignum_value.to_f).should == 1
163+
(bignum_value <=> bignum_value.to_f).should == 0
164+
((bignum_value - 1) <=> bignum_value.to_f).should == -1
165+
end
166+
end
167+
160168
# The tests below are taken from matz's revision 23730 for Ruby trunk
161169
it "returns 1 when self is Infinity and other is a Bignum" do
162170
(infinity_value <=> Float::MAX.to_i*2).should == 1

core/integer/eql_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require_relative '../../spec_helper'
2+
3+
describe "Integer#eql?" do
4+
context "bignum" do
5+
it "returns true for the same value" do
6+
bignum_value.eql?(bignum_value).should == true
7+
end
8+
9+
it "returns false for a different Integer value" do
10+
bignum_value.eql?(bignum_value(1)).should == false
11+
end
12+
13+
it "returns false for a Float with the same numeric value" do
14+
bignum_value.eql?(bignum_value.to_f).should == false
15+
end
16+
17+
it "returns false for a Rational with the same numeric value" do
18+
bignum_value.eql?(Rational(bignum_value)).should == false
19+
end
20+
21+
it "returns false for a Fixnum-range Integer" do
22+
bignum_value.eql?(42).should == false
23+
end
24+
end
25+
end

core/integer/fixtures/classes.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
module IntegerSpecs
22
class CoerceError < StandardError
33
end
4+
5+
class CoercibleNumeric
6+
def initialize(v) @v = v end
7+
def coerce(other) [self.class.new(other), self] end
8+
def >(other) @v.to_i > other.to_i end
9+
def >=(other) @v.to_i >= other.to_i end
10+
def <(other) @v.to_i < other.to_i end
11+
def <=(other) @v.to_i <= other.to_i end
12+
def to_i() @v.to_i end
13+
end
414
end

core/integer/gt_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,10 @@
3939
-> { @bignum > "4" }.should raise_error(ArgumentError)
4040
-> { @bignum > mock('str') }.should raise_error(ArgumentError)
4141
end
42+
43+
it "dispatches the correct operator after coercion" do
44+
(bignum_value > IntegerSpecs::CoercibleNumeric.new(1)).should == true
45+
(bignum_value > IntegerSpecs::CoercibleNumeric.new(bignum_value * 2)).should == false
46+
end
4247
end
4348
end

core/integer/gte_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,10 @@
3939
-> { @bignum >= "4" }.should raise_error(ArgumentError)
4040
-> { @bignum >= mock('str') }.should raise_error(ArgumentError)
4141
end
42+
43+
it "dispatches the correct operator after coercion" do
44+
(bignum_value >= IntegerSpecs::CoercibleNumeric.new(1)).should == true
45+
(bignum_value >= IntegerSpecs::CoercibleNumeric.new(bignum_value * 2)).should == false
46+
end
4247
end
4348
end

core/integer/lt_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,10 @@
4141
-> { @bignum < "4" }.should raise_error(ArgumentError)
4242
-> { @bignum < mock('str') }.should raise_error(ArgumentError)
4343
end
44+
45+
it "dispatches the correct operator after coercion" do
46+
(bignum_value < IntegerSpecs::CoercibleNumeric.new(bignum_value * 2)).should == true
47+
(bignum_value < IntegerSpecs::CoercibleNumeric.new(1)).should == false
48+
end
4449
end
4550
end

core/integer/lte_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,10 @@
4949
-> { @bignum <= "4" }.should raise_error(ArgumentError)
5050
-> { @bignum <= mock('str') }.should raise_error(ArgumentError)
5151
end
52+
53+
it "dispatches the correct operator after coercion" do
54+
(bignum_value <= IntegerSpecs::CoercibleNumeric.new(bignum_value * 2)).should == true
55+
(bignum_value <= IntegerSpecs::CoercibleNumeric.new(1)).should == false
56+
end
5257
end
5358
end

core/integer/shared/equal.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,10 @@
5454
@bignum.send(@method, obj).should == true
5555
@bignum.send(@method, obj).should == false
5656
end
57+
58+
it "does not lose precision when comparing with a Float" do
59+
(bignum_value(1).send(@method, bignum_value.to_f)).should == false
60+
(bignum_value.send(@method, bignum_value.to_f)).should == true
61+
end
5762
end
5863
end

core/matchdata/deconstruct_keys_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,19 @@
6060
m = /(?<f>foo)(?<b>bar)/.match("foobar")
6161
m.deconstruct_keys([:f, :b, :c]).should == {}
6262
end
63+
64+
it "includes non-participating captures as nil for nil argument" do
65+
m = "hello".match(/(?<a>hello)(?<b>world)?/)
66+
m.deconstruct_keys(nil).should == { a: "hello", b: nil }
67+
end
68+
69+
it "includes non-participating captures as nil for explicit keys" do
70+
m = "hello".match(/(?<a>hello)(?<b>world)?/)
71+
m.deconstruct_keys([:a, :b]).should == { a: "hello", b: nil }
72+
end
73+
74+
it "does not stop iterating at a non-participating capture" do
75+
m = "hello!".match(/(?<a>hello)(?<b>world)?(?<c>!)/)
76+
m.deconstruct_keys([:b, :c, :a]).should == { b: nil, c: "!", a: "hello" }
77+
end
6378
end

0 commit comments

Comments
 (0)