From a91b9e40f500e7787370f5c21f3490df7a635866 Mon Sep 17 00:00:00 2001 From: Leonardo Tegon Date: Wed, 11 Oct 2017 15:47:25 -0300 Subject: [PATCH 1/2] Add specs for `half` option in `Rational#round` In Ruby 2.4 the `round` method in `Rational` accepts a keyword argument (`half`). The specs values were based on https://github.com/ruby/ruby/blob/6b05153a3a75b74b64553d6a46f501d9ee0f0376/rational.c#L1577-L1585 and https://github.com/nilsding/spec/blob/80119090e7cd9071cb52905f13d52ca56b5a340d/core/float/round_spec.rb#L88-L98 --- shared/rational/round.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/shared/rational/round.rb b/shared/rational/round.rb index 8874daceea..4db7c454df 100644 --- a/shared/rational/round.rb +++ b/shared/rational/round.rb @@ -68,4 +68,29 @@ Rational(3, 2).round(2_097_171).should == Rational(3, 2) end end + + ruby_version_is "2.4" do + describe "with half option" do + it "returns an Integer" do + Rational(10, 4).round(half: :up).should == 3 + Rational(10, 4).round(half: :down).should == 2 + Rational(10, 4).round(half: :even).should == 2 + Rational(-10, 4).round(half: :up).should == -3 + Rational(-10, 4).round(half: :down).should == -2 + Rational(-10, 4).round(half: :even).should == -2 + end + + it "returns a Rational" do + Rational(25, 100).round(1, half: :up).should == Rational(3, 10) + Rational(25, 100).round(1, half: :down).should == Rational(1, 5) + Rational(25, 100).round(1, half: :even).should == Rational(1, 5) + Rational(35, 100).round(1, half: :up).should == Rational(2, 5) + Rational(35, 100).round(1, half: :down).should == Rational(3, 10) + Rational(35, 100).round(1, half: :even).should == Rational(2, 5) + Rational(-25, 100).round(1, half: :up).should == Rational(-3, 10) + Rational(-25, 100).round(1, half: :down).should == Rational(-1, 5) + Rational(-25, 100).round(1, half: :even).should == Rational(-1, 5) + end + end + end end From 6fe3c063cc35da40d9c0669148f139d281f7f934 Mon Sep 17 00:00:00 2001 From: Leonardo Tegon Date: Thu, 12 Oct 2017 11:12:17 -0300 Subject: [PATCH 2/2] Improve specs description --- shared/rational/round.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared/rational/round.rb b/shared/rational/round.rb index 4db7c454df..c7a4cc6d07 100644 --- a/shared/rational/round.rb +++ b/shared/rational/round.rb @@ -71,7 +71,7 @@ ruby_version_is "2.4" do describe "with half option" do - it "returns an Integer" do + it "returns an Integer when precision is not passed" do Rational(10, 4).round(half: :up).should == 3 Rational(10, 4).round(half: :down).should == 2 Rational(10, 4).round(half: :even).should == 2 @@ -80,7 +80,7 @@ Rational(-10, 4).round(half: :even).should == -2 end - it "returns a Rational" do + it "returns a Rational when the precision is greater than 0" do Rational(25, 100).round(1, half: :up).should == Rational(3, 10) Rational(25, 100).round(1, half: :down).should == Rational(1, 5) Rational(25, 100).round(1, half: :even).should == Rational(1, 5)