Sha256: cb69580da81a7ac287a12a41acde9a95f2905136908a8802731b1aafe8c78721

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 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]

    expect(a.comprehension).not_to be_kind_of(Array)
  end

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

    expect(a.comprehension.to_a).to eq([[1],[2],[3],[4],[5]])
  end

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

    expect(a.comprehension.to_a).to eq([a])
  end

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

    expect(a.comprehension.to_a).to eq([a])
  end

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

    expect(a.comprehension.to_a).to eq([[1],[2],[3],[4],[5]])
  end

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

    expect(a.comprehension.to_a).to eq([[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 } }]

    expect(a.comprehension.to_a).to eq([[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) }
      }
    ]

    expect(a.comprehension.to_a).to eq([
      [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]

    expect(a.comprehension.to_a).to eq([[1,1],[1,2],[1,3],[1,4],[1,5]])
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
combinatorics-0.5.0 spec/list_comprehension_spec.rb
combinatorics-0.4.4 spec/list_comprehension_spec.rb