Sha256: 0feff537db6aa16d0d3b504267ee2d52dc51402dd7ddcd981c3418a5606c6e66

Contents?: true

Size: 1.49 KB

Versions: 14

Compression:

Stored size: 1.49 KB

Contents

describe "Enumerable#detect" do
  before :each do
    ScratchPad.record []
    @elements = [2, 4, 6, 8, 10]
    @numerous = EnumerableSpecs::Numerous.new(*@elements)
    @empty = []
  end

  it "passes each entry in enum to block while block when block is false" do
    visited_elements = []
    @numerous.detect do |element|
      visited_elements << element
      false
    end
    visited_elements.should == @elements
  end

  it "returns nil when the block is false and there is no ifnone proc given" do
    @numerous.detect {|e| false}.should == nil
  end

  it "returns the first element for which the block is not false" do
    @elements.each do |element|
      # @numerous.detect {|e| e > element -1 }.should == element
    end
  end

  it "returns the value of the ifnone proc if the block is false" do
    fail_proc = lambda { "cheeseburgers" }
    @numerous.detect(fail_proc) {|e| false }.should == "cheeseburgers"
  end

  it "doesn't call the ifnone proc if an element is found" do
    fail_proc = lambda { raise "This should't have been called" }
    @numerous.detect(fail_proc) {|e| 2 == @elements.first }.should == 2
  end

  it "calls the ifnone proc only once when the block is false" do
    times = 0
    fail_proc = lambda { times += 1; raise if times > 1; "cheeseburgers" }
    @numerous.detect(fail_proc) {|e| false }.should == "cheeseburgers"
  end

  it "calls the ifnone proc when there are no elements" do
    fail_proc = lambda { "yay" }
    @empty.detect(fail_proc) {|e| true}.should == "yay"
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
opal-0.3.41 spec/core/enumerable/detect_spec.rb
opal-0.3.40 spec/core/enumerable/detect_spec.rb
opal-0.3.39 spec/core/enumerable/detect_spec.rb
opal-0.3.38 spec/core/enumerable/detect_spec.rb
opal-0.3.37 spec/core/enumerable/detect_spec.rb
opal-0.3.36 spec/core/enumerable/detect_spec.rb
opal-0.3.35 spec/core/enumerable/detect_spec.rb
opal-0.3.34 spec/core/enumerable/detect_spec.rb
opal-0.3.33 spec/core/enumerable/detect_spec.rb
opal-0.3.32 spec/core/enumerable/detect_spec.rb
opal-0.3.31 spec/core/enumerable/detect_spec.rb
opal-0.3.30 spec/core/enumerable/detect_spec.rb
opal-0.3.29 spec/core/enumerable/detect_spec.rb
opal-0.3.28 spec/core/enumerable/detect_spec.rb