Add Mutant::Matcher::Chain

This commit is contained in:
Markus Schirp 2012-08-29 14:00:37 +02:00
parent 6b300ae5c5
commit 9df6d1933a
3 changed files with 90 additions and 0 deletions

View file

@ -0,0 +1,50 @@
module Mutant
class Matcher
# A chain of matchers
class Chain < self
include Equalizer.new(:matchers)
# Enumerate subjects
#
# @return [Enumerator<Subject]
# returns subject enumerator if no block given
#
# @return [self]
# returnns self otherwise
#
# @api private
#
def each(&block)
return to_enum unless block_given?
@matchers.each do |matcher|
matcher.each(&block)
end
self
end
# Return the chain of matchers
#
# @return [Enumerable<Chain>]
#
# @api private
#
def matchers; @matchers; end
private
# Initialize chain matcher
#
# @param [Enumerable<Matcher>] matchers
#
# @return [undefined]
#
# @api private
#
def initialize(matchers)
@matchers = matchers
end
end
end
end

View file

@ -0,0 +1,28 @@
require 'spec_helper'
describe Mutant::Matcher::Chain, '#each' do
subject { object.each { |entry| yields << entry } }
let(:object) { described_class.new(matchers) }
let(:matchers) { [matcher_a, matcher_b] }
let(:matcher_a) { mock('Matcher A') }
let(:matcher_b) { mock('Matcher B') }
let(:subject_a) { mock('Subject A') }
let(:subject_b) { mock('Subject B') }
before do
matcher_a.stub(:each).and_yield(subject_a).and_return(matcher_a)
matcher_b.stub(:each).and_yield(subject_b).and_return(matcher_b)
end
it_should_behave_like 'an #each method'
let(:yields) { [] }
it 'should yield subjects' do
expect { subject }.to change { yields }.from([]).to([subject_a, subject_b])
end
end

View file

@ -0,0 +1,12 @@
require 'spec_helper'
describe Mutant::Matcher::Chain, '#matchers' do
subject { object.matchers }
let(:object) { described_class.new(matchers) }
let(:matchers) { mock('Matchers') }
it { should be(matchers) }
it_should_behave_like 'an idempotent method'
end