diff options
Diffstat (limited to 'spec/ruby/core/array')
-rw-r--r-- | spec/ruby/core/array/at_spec.rb | 4 | ||||
-rw-r--r-- | spec/ruby/core/array/intersection_spec.rb | 4 | ||||
-rw-r--r-- | spec/ruby/core/array/max_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/array/min_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/array/pack/buffer_spec.rb | 52 | ||||
-rw-r--r-- | spec/ruby/core/array/permutation_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/array/pop_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/array/shift_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/array/sum_spec.rb | 44 |
9 files changed, 115 insertions, 7 deletions
diff --git a/spec/ruby/core/array/at_spec.rb b/spec/ruby/core/array/at_spec.rb index e40c26f2cc..f2af0e13a1 100644 --- a/spec/ruby/core/array/at_spec.rb +++ b/spec/ruby/core/array/at_spec.rb @@ -15,7 +15,7 @@ describe "Array#at" do a.at(7).should == nil end - it "returns the (-n)'th elemet from the last, for the given negative index n" do + it "returns the (-n)'th element from the last, for the given negative index n" do a = [1, 2, 3, 4, 5, 6] a.at(-1).should == 6 a.at(-2).should == 5 @@ -50,7 +50,7 @@ describe "Array#at" do lambda { [].at("cat") }.should raise_error(TypeError) end - it "raises an ArgumentError when 2 or more arguments is passed" do + it "raises an ArgumentError when 2 or more arguments are passed" do lambda { [:a, :b].at(0,1) }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/core/array/intersection_spec.rb b/spec/ruby/core/array/intersection_spec.rb index 4d6c2a12d3..e399509ea7 100644 --- a/spec/ruby/core/array/intersection_spec.rb +++ b/spec/ruby/core/array/intersection_spec.rb @@ -19,7 +19,7 @@ describe "Array#&" do it "does not modify the original Array" do a = [1, 1, 3, 5] - a & [1, 2, 3] + (a & [1, 2, 3]).should == [1, 3] a.should == [1, 1, 3, 5] end @@ -52,7 +52,7 @@ describe "Array#&" do obj1.stub!(:hash).and_return(0) obj2.stub!(:hash).and_return(0) obj1.should_receive(:eql?).at_least(1).and_return(true) - obj2.should_receive(:eql?).at_least(1).and_return(true) + obj2.stub!(:eql?).and_return(true) ([obj1] & [obj2]).should == [obj1] ([obj1, obj1, obj2, obj2] & [obj2]).should == [obj1] diff --git a/spec/ruby/core/array/max_spec.rb b/spec/ruby/core/array/max_spec.rb index cf6a48c2e3..db1d755645 100644 --- a/spec/ruby/core/array/max_spec.rb +++ b/spec/ruby/core/array/max_spec.rb @@ -1,6 +1,12 @@ require File.expand_path('../../../spec_helper', __FILE__) describe "Array#max" do + ruby_version_is "2.4" do + it "is defined on Array" do + [1].method(:max).owner.should equal Array + end + end + it "returns nil with no values" do [].max.should == nil end diff --git a/spec/ruby/core/array/min_spec.rb b/spec/ruby/core/array/min_spec.rb index 53fe4e0692..59f3814da2 100644 --- a/spec/ruby/core/array/min_spec.rb +++ b/spec/ruby/core/array/min_spec.rb @@ -1,6 +1,12 @@ require File.expand_path('../../../spec_helper', __FILE__) describe "Array#min" do + ruby_version_is "2.4" do + it "is defined on Array" do + [1].method(:max).owner.should equal Array + end + end + it "returns nil with no values" do [].min.should == nil end diff --git a/spec/ruby/core/array/pack/buffer_spec.rb b/spec/ruby/core/array/pack/buffer_spec.rb new file mode 100644 index 0000000000..928f7db731 --- /dev/null +++ b/spec/ruby/core/array/pack/buffer_spec.rb @@ -0,0 +1,52 @@ +# encoding: ascii-8bit + +require File.expand_path('../../../../spec_helper', __FILE__) + +ruby_version_is '2.4' do + describe "Aray#pack with `buffer` option" do + it "returns specified buffer" do + n = [ 65, 66, 67 ] + buffer = " "*3 + result = n.pack("ccc", buffer: buffer) #=> "ABC" + result.should equal(buffer) + end + + it "adds result at the end of buffer content" do + n = [ 65, 66, 67 ] # result without buffer is "ABC" + + buffer = "" + n.pack("ccc", buffer: buffer).should == "ABC" + + buffer = "123" + n.pack("ccc", buffer: buffer).should == "123ABC" + + buffer = "12345" + n.pack("ccc", buffer: buffer).should == "12345ABC" + end + + it "raises TypeError exception if buffer is not String" do + lambda { [65].pack("ccc", buffer: []) }.should raise_error( + TypeError, "buffer must be String, not Array") + end + + context "offset (@) is specified" do + it 'keeps buffer content if it is longer than offset' do + n = [ 65, 66, 67 ] + buffer = "123456" + n.pack("@3ccc", buffer: buffer).should == "123ABC" + end + + it "fills the gap with \0 if buffer content is shorter than offset" do + n = [ 65, 66, 67 ] + buffer = "123" + n.pack("@6ccc", buffer: buffer).should == "123\0\0\0ABC" + end + + it 'does not keep buffer content if it is longer than offset + result' do + n = [ 65, 66, 67 ] + buffer = "1234567890" + n.pack("@3ccc", buffer: buffer).should == "123ABC" + end + end + end +end diff --git a/spec/ruby/core/array/permutation_spec.rb b/spec/ruby/core/array/permutation_spec.rb index 8a80b93c3b..c0eba57a3e 100644 --- a/spec/ruby/core/array/permutation_spec.rb +++ b/spec/ruby/core/array/permutation_spec.rb @@ -52,7 +52,7 @@ describe "Array#permutation" do end it "returns no permutations when the given length has no permutations" do - @numbers.permutation(9).entries.size == 0 + @numbers.permutation(9).entries.size.should == 0 @numbers.permutation(9) { |n| @yielded << n } @yielded.should == [] end diff --git a/spec/ruby/core/array/pop_spec.rb b/spec/ruby/core/array/pop_spec.rb index 313dc4189e..ea649c6585 100644 --- a/spec/ruby/core/array/pop_spec.rb +++ b/spec/ruby/core/array/pop_spec.rb @@ -119,7 +119,7 @@ describe "Array#pop" do a.should == [] end - it "raises a TypeError when the passed n can be coerced to Integer" do + it "raises a TypeError when the passed n cannot be coerced to Integer" do lambda{ [1, 2].pop("cat") }.should raise_error(TypeError) lambda{ [1, 2].pop(nil) }.should raise_error(TypeError) end diff --git a/spec/ruby/core/array/shift_spec.rb b/spec/ruby/core/array/shift_spec.rb index e5d25e2050..a7b6f58392 100644 --- a/spec/ruby/core/array/shift_spec.rb +++ b/spec/ruby/core/array/shift_spec.rb @@ -104,7 +104,7 @@ describe "Array#shift" do a.should == [] end - it "raises a TypeError when the passed n can be coerced to Integer" do + it "raises a TypeError when the passed n cannot be coerced to Integer" do lambda{ [1, 2].shift("cat") }.should raise_error(TypeError) lambda{ [1, 2].shift(nil) }.should raise_error(TypeError) end diff --git a/spec/ruby/core/array/sum_spec.rb b/spec/ruby/core/array/sum_spec.rb new file mode 100644 index 0000000000..6548655c35 --- /dev/null +++ b/spec/ruby/core/array/sum_spec.rb @@ -0,0 +1,44 @@ +require File.expand_path('../../../spec_helper', __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 omitted" 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 "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 |