From 9df6d1933a623efe6f7dd3dc7b0aa1043c8c0501 Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Wed, 29 Aug 2012 14:00:37 +0200 Subject: [PATCH] Add Mutant::Matcher::Chain --- lib/mutant/matcher/chain.rb | 50 +++++++++++++++++++ spec/unit/mutant/matcher/chain/each_spec.rb | 28 +++++++++++ .../mutant/matcher/chain/matchers_spec.rb | 12 +++++ 3 files changed, 90 insertions(+) create mode 100644 lib/mutant/matcher/chain.rb create mode 100644 spec/unit/mutant/matcher/chain/each_spec.rb create mode 100644 spec/unit/mutant/matcher/chain/matchers_spec.rb diff --git a/lib/mutant/matcher/chain.rb b/lib/mutant/matcher/chain.rb new file mode 100644 index 00000000..e7168812 --- /dev/null +++ b/lib/mutant/matcher/chain.rb @@ -0,0 +1,50 @@ +module Mutant + class Matcher + # A chain of matchers + class Chain < self + include Equalizer.new(:matchers) + + # Enumerate subjects + # + # @return [Enumerator] + # + # @api private + # + def matchers; @matchers; end + + private + + # Initialize chain matcher + # + # @param [Enumerable] matchers + # + # @return [undefined] + # + # @api private + # + def initialize(matchers) + @matchers = matchers + end + end + end +end diff --git a/spec/unit/mutant/matcher/chain/each_spec.rb b/spec/unit/mutant/matcher/chain/each_spec.rb new file mode 100644 index 00000000..d16ba79f --- /dev/null +++ b/spec/unit/mutant/matcher/chain/each_spec.rb @@ -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 diff --git a/spec/unit/mutant/matcher/chain/matchers_spec.rb b/spec/unit/mutant/matcher/chain/matchers_spec.rb new file mode 100644 index 00000000..3e5e43a3 --- /dev/null +++ b/spec/unit/mutant/matcher/chain/matchers_spec.rb @@ -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