|
| 1 | +require File.expand_path('../../../../spec_helper', __FILE__) |
| 2 | +require File.expand_path('../fixtures/classes', __FILE__) |
| 3 | + |
| 4 | +describe "Enumerator::Lazy#grep_v" do |
| 5 | + before(:each) do |
| 6 | + @yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy |
| 7 | + @eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy |
| 8 | + ScratchPad.record [] |
| 9 | + end |
| 10 | + |
| 11 | + after(:each) do |
| 12 | + ScratchPad.clear |
| 13 | + end |
| 14 | + |
| 15 | + it "requires an argument" do |
| 16 | + enumerator_class::Lazy.instance_method(:grep_v).arity.should == 1 |
| 17 | + end |
| 18 | + |
| 19 | + it "returns a new instance of Enumerator::Lazy" do |
| 20 | + ret = @yieldsmixed.grep_v(Object) {} |
| 21 | + ret.should be_an_instance_of(enumerator_class::Lazy) |
| 22 | + ret.should_not equal(@yieldsmixed) |
| 23 | + |
| 24 | + ret = @yieldsmixed.grep_v(Object) |
| 25 | + ret.should be_an_instance_of(enumerator_class::Lazy) |
| 26 | + ret.should_not equal(@yieldsmixed) |
| 27 | + end |
| 28 | + |
| 29 | + it "sets nil to size" do |
| 30 | + enumerator_class::Lazy.new(Object.new, 100) {}.grep_v(Object) {}.size.should == nil |
| 31 | + enumerator_class::Lazy.new(Object.new, 100) {}.grep_v(Object).size.should == nil |
| 32 | + end |
| 33 | + |
| 34 | + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do |
| 35 | + it "stops after specified times when not given a block" do |
| 36 | + (0..Float::INFINITY).lazy.grep_v(3..5).first(3).should == [0, 1, 2] |
| 37 | + |
| 38 | + @eventsmixed.grep_v(Symbol).first(1) |
| 39 | + ScratchPad.recorded.should == [:before_yield] |
| 40 | + end |
| 41 | + |
| 42 | + it "stops after specified times when given a block" do |
| 43 | + (0..Float::INFINITY).lazy.grep_v(4..8, &:succ).first(3).should == [1, 2, 3] |
| 44 | + |
| 45 | + @eventsmixed.grep_v(Symbol) {}.first(1) |
| 46 | + ScratchPad.recorded.should == [:before_yield] |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + it "calls the block with a gathered array when yield with multiple arguments" do |
| 51 | + yields = [] |
| 52 | + @yieldsmixed.grep_v(Array) { |v| yields << v }.force |
| 53 | + yields.should == EnumeratorLazySpecs::YieldsMixed.gathered_non_array_yields |
| 54 | + |
| 55 | + @yieldsmixed.grep_v(Array).force.should == yields |
| 56 | + end |
| 57 | + |
| 58 | + describe "on a nested Lazy" do |
| 59 | + it "sets nil to size" do |
| 60 | + enumerator_class::Lazy.new(Object.new, 100) {}.grep_v(Object).grep_v(Object) {}.size.should == nil |
| 61 | + enumerator_class::Lazy.new(Object.new, 100) {}.grep_v(Object).grep_v(Object).size.should == nil |
| 62 | + end |
| 63 | + |
| 64 | + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do |
| 65 | + it "stops after specified times when not given a block" do |
| 66 | + (0..Float::INFINITY).lazy.grep_v(3..5).grep_v(6..10).first(3).should == [0, 1, 2] |
| 67 | + |
| 68 | + @eventsmixed.grep_v(Symbol).grep_v(String).first(1) |
| 69 | + ScratchPad.recorded.should == [:before_yield] |
| 70 | + end |
| 71 | + |
| 72 | + it "stops after specified times when given a block" do |
| 73 | + (0..Float::INFINITY).lazy |
| 74 | + .grep_v(1..2) { |n| n > 3 ? n : false } |
| 75 | + .grep_v(false) { |n| n.even? ? n : false } |
| 76 | + .first(3) |
| 77 | + .should == [4, false, 6] |
| 78 | + |
| 79 | + @eventsmixed.grep_v(Symbol) {}.grep_v(String) {}.first(1) |
| 80 | + ScratchPad.recorded.should == [:before_yield] |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments