From f3c3d63f062872c3f305731d7c7af4a16de81b7b Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Wed, 11 Oct 2017 18:55:50 +0300 Subject: [PATCH 1/3] Add specs for Array#sum --- core/array/fixtures/sum.rb | 22 +++++++++++++++++ core/array/sum_spec.rb | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 core/array/fixtures/sum.rb create mode 100644 core/array/sum_spec.rb diff --git a/core/array/fixtures/sum.rb b/core/array/fixtures/sum.rb new file mode 100644 index 0000000000..d81bc8dfb9 --- /dev/null +++ b/core/array/fixtures/sum.rb @@ -0,0 +1,22 @@ +module ArraySpec + module AdditionIsOverriden + module M + refine Integer do + def +(*args) + raise "Redefined method is called" + end + end + end + + using M + + def self.call_sum + [1, 2, 3].sum + end + + def self.call_explicit_adding + 1 + 2 + 3 + end + end +end + diff --git a/core/array/sum_spec.rb b/core/array/sum_spec.rb new file mode 100644 index 0000000000..2394c90758 --- /dev/null +++ b/core/array/sum_spec.rb @@ -0,0 +1,49 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/sum', __FILE__) + +ruby_version_is '2.4' do + describe "Array#sum" do + it "returns the sum of elements" do + [1, 2, 3].sum.should == 6 + end + + it "applies a block to each element before adding if it's given" do + [1, 2, 3].sum { |i| i * 10 }.should == 60 + end + + it "returns init value if array is empty" do + [].sum(-1).should == -1 + end + + it "returns 0 if array is empty and init is omited" do + [].sum.should == 0 + end + + it "adds init value to the sum of elemens" do + [1, 2, 3].sum(10).should == 16 + end + + it "can be used for non-numeric objects by providing init value" do + ["a", "b", "c"].sum("").should == "abc" + end + + it 'raises TypeError if any element are not numeric' do + lambda { ["a"].sum }.should raise_error(TypeError) + end + + it 'raises TypeError if any element cannot be added to init value' do + lambda { [1].sum([]) }.should raise_error(TypeError) + end + + it "may not respect redefinishion of :+ method of core classes" do + lambda { + ArraySpec::AdditionIsOverriden.call_explicit_adding + }.should raise_error(RuntimeError, "Redefined method is called") + + + lambda { + ArraySpec::AdditionIsOverriden.call_sum + }.should_not raise_error + end + end +end From b748531ff95723b08797c9515a8a0cdb865029f0 Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Wed, 11 Oct 2017 21:56:44 +0300 Subject: [PATCH 2/3] Code review. Implement test case without refinements --- core/array/fixtures/sum.rb | 22 ---------------------- core/array/sum_spec.rb | 22 +++++++++++++--------- 2 files changed, 13 insertions(+), 31 deletions(-) delete mode 100644 core/array/fixtures/sum.rb diff --git a/core/array/fixtures/sum.rb b/core/array/fixtures/sum.rb deleted file mode 100644 index d81bc8dfb9..0000000000 --- a/core/array/fixtures/sum.rb +++ /dev/null @@ -1,22 +0,0 @@ -module ArraySpec - module AdditionIsOverriden - module M - refine Integer do - def +(*args) - raise "Redefined method is called" - end - end - end - - using M - - def self.call_sum - [1, 2, 3].sum - end - - def self.call_explicit_adding - 1 + 2 + 3 - end - end -end - diff --git a/core/array/sum_spec.rb b/core/array/sum_spec.rb index 2394c90758..33ec6bbfba 100644 --- a/core/array/sum_spec.rb +++ b/core/array/sum_spec.rb @@ -1,5 +1,4 @@ require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/sum', __FILE__) ruby_version_is '2.4' do describe "Array#sum" do @@ -36,14 +35,19 @@ end it "may not respect redefinishion of :+ method of core classes" do - lambda { - ArraySpec::AdditionIsOverriden.call_explicit_adding - }.should raise_error(RuntimeError, "Redefined method is called") - - - lambda { - ArraySpec::AdditionIsOverriden.call_sum - }.should_not raise_error + code = <<-RUBY + class Integer + def +(*args) + raise "Redefined method is called" + end + end; + + [1, 2, 3].sum + RUBY + code = code.lines.map(&:chomp).join(' ') + + # `system` call returns true if status code is 0 so there is no exception + system(ruby_cmd(code)).should be_true end end end From e52d2b259cb9f3218799bc304929f86bf07d9b3e Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Wed, 11 Oct 2017 23:05:00 +0200 Subject: [PATCH 3/3] Add a spec testing what calls are made for Array#sum * Remove spec only testing that it does not crash. MRI does not respect monkey-patching here so better leave unspecified. --- core/array/sum_spec.rb | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/core/array/sum_spec.rb b/core/array/sum_spec.rb index 33ec6bbfba..ee15d65f67 100644 --- a/core/array/sum_spec.rb +++ b/core/array/sum_spec.rb @@ -34,20 +34,11 @@ lambda { [1].sum([]) }.should raise_error(TypeError) end - it "may not respect redefinishion of :+ method of core classes" do - code = <<-RUBY - class Integer - def +(*args) - raise "Redefined method is called" - end - end; - - [1, 2, 3].sum - RUBY - code = code.lines.map(&:chomp).join(' ') - - # `system` call returns true if status code is 0 so there is no exception - system(ruby_cmd(code)).should be_true + it "calls + to sum the elements" do + a = mock("a") + b = mock("b") + a.should_receive(:+).with(b).and_return(42) + [b].sum(a).should == 42 end end end