File: zip_spec.rb

package info (click to toggle)
ruby2.5 2.5.5-3%2Bdeb10u4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 101,532 kB
  • sloc: ruby: 732,598; ansic: 669,262; xml: 25,363; yacc: 20,963; javascript: 6,680; sh: 3,610; lisp: 2,627; makefile: 596; python: 198; sed: 76; perl: 62; awk: 36; asm: 35
file content (42 lines) | stat: -rw-r--r-- 1,713 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "Enumerable#zip" do

  it "combines each element of the receiver with the element of the same index in arrays given as arguments" do
    EnumerableSpecs::Numerous.new(1,2,3).zip([4,5,6],[7,8,9]).should == [[1,4,7],[2,5,8],[3,6,9]]
    EnumerableSpecs::Numerous.new(1,2,3).zip.should == [[1],[2],[3]]
  end

  it "passes each element of the result array to a block and return nil if a block is given" do
    expected = [[1,4,7],[2,5,8],[3,6,9]]
    EnumerableSpecs::Numerous.new(1,2,3).zip([4,5,6],[7,8,9]) do |result_component|
      result_component.should == expected.shift
    end.should == nil
    expected.size.should == 0
  end

  it "fills resulting array with nils if an argument array is too short" do
    EnumerableSpecs::Numerous.new(1,2,3).zip([4,5,6], [7,8]).should == [[1,4,7],[2,5,8],[3,6,nil]]
  end

  it "converts arguments to arrays using #to_ary" do
    convertable = EnumerableSpecs::ArrayConvertable.new(4,5,6)
    EnumerableSpecs::Numerous.new(1,2,3).zip(convertable).should == [[1,4],[2,5],[3,6]]
    convertable.called.should == :to_ary
  end

  it "converts arguments to enums using #to_enum" do
    convertable = EnumerableSpecs::EnumConvertable.new(4..6)
    EnumerableSpecs::Numerous.new(1,2,3).zip(convertable).should == [[1,4],[2,5],[3,6]]
    convertable.called.should == :to_enum
    convertable.sym.should == :each
  end

  it "gathers whole arrays as elements when each yields multiple" do
    multi = EnumerableSpecs::YieldsMulti.new
    multi.zip(multi).should == [[[1, 2], [1, 2]], [[3, 4, 5], [3, 4, 5]], [[6, 7, 8, 9], [6, 7, 8, 9]]]
  end

end