Sha256: ed36668eeab4df6232f6ae85cac2b4d9b79048a66e821ab5a9baf0f5cf5a9fe0

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

require 'spec_helper'
require 'combinatorics/list_comprehension'
require 'combinatorics/generator'

describe "Array#comprehension" do
  it "should return an Enumerator object if no block is given" do
    a = [1..5]

    a.comprehension.should_not be_kind_of(Array)
  end

  it "should yield iterations to the given block" do
    range = (1..5)
    a = [range]

    a.comprehension.to_a.should == [[1],[2],[3],[4],[5]]
  end

  it "should do nothing an Array of all non-enumerable objects" do
    a = [1,2,3]

    a.comprehension.to_a.should == [a]
  end

  it "should pass through an empty Array" do
    a = []

    a.comprehension.to_a.should == [a]
  end

  it "should iterate over the values within an enumerable value" do
    range = (1..5)
    a = [range]

    a.comprehension.to_a.should == [[1],[2],[3],[4],[5]]
  end

  it "should iterate over repeating values" do
    range = [1,2,3,1,2,4]
    a = [range]

    a.comprehension.to_a.should == [[1],[2],[3],[1],[2],[4]]
  end

  it "should iterate over values from a generator" do
    a = [Combinatorics::Generator.new { |g| 5.times { |i| g.yield i } }]

    a.comprehension.to_a.should == [[0],[1],[2],[3],[4]]
  end

  it "should iterate over values from a non-repeating generator" do
    multiplier = 0
    a = [
      [1,2],
      Combinatorics::Generator.new { |g|
        multiplier += 1
        5.times { |i| g.yield(i * multiplier) }
      }
    ]

    a.comprehension.to_a.should == [
      [1,0],[1,1],[1,2],[1,3],[1,4],
      [2,0],[2,2],[2,4],[2,6],[2,8]
    ]
  end

  it "should ignore non-enumerable values" do
    range = (1..5)
    a = [1,range]

    a.comprehension.to_a.should == [[1,1],[1,2],[1,3],[1,4],[1,5]]
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
combinatorics-0.4.3 spec/list_comprehension_spec.rb
combinatorics-0.4.1 spec/list_comprehension_spec.rb
combinatorics-0.3.1 spec/list_comprehension_spec.rb