# File: collection-walker_spec.rb require_relative '../spec_helper' # Load mix-in module for creating a sample collection of macro-steps require_relative 'use-sample-collection' # Load the class under test require_relative '../../lib/macros4cuke/formatting-service' module Macros4Cuke # Open this namespace to avoid module qualifier prefixes describe FormattingService do include UseSampleCollection # Add convenience methods for sample collection # Default instantiation rule subject { FormattingService.new } before(:all) do # Fill the collection of macro-steps with sample steps fill_collection end after(:all) do # Clear the collection to prevent interference between spec files macro_coll.clear end context 'Initialization:' do it 'should be created without parameter' do expect { FormattingService.new }.not_to raise_error end it 'has no formatter at start' do expect(subject).to have(0).formatters end end # context context 'Provided services:' do it 'should know its registered formatter(s)' do formatter1 = double('fake_one') formatter1.should_receive(:implements).once.and_return([:on_collection]) subject.register(formatter1) expect(subject).to have(1).formatters formatter2 = double('fake_two') formatter2.should_receive(:implements).once.and_return([:on_step]) subject.register(formatter2) expect(subject).to have(2).formatters expect(subject.formatters).to eq([formatter1, formatter2]) end it "should complain when a formatter doesn't implement notifications" do formatter1 = double('formatter') formatter1.should_receive(:implements).once.and_return([]) err_type = Macros4Cuke::NoFormattingEventForFormatter err_msg = 'Formatter RSpec::Mocks::Mock does not'\ ' support any formatting event.' expect { subject.register(formatter1) }.to raise_error(err_type, err_msg) end it 'should complain when a formatter uses an unknown formatting event' do notifications = [ :on_collection, :on_collection_end, :non_standard, :on_step, :on_step_end ] formatter = double('formatter') formatter.should_receive(:implements).once.and_return(notifications) err_type = Macros4Cuke::UnknownFormattingEvent err_msg = "Formatter RSpec::Mocks::Mock uses\ the unknown formatting event 'non_standard'." expect { subject.register(formatter) }.to raise_error(err_type, err_msg) end it 'should support formatters using a subset of possible notifications' do # Case: formatter that supports a few notifications only formatter1 = double('formatter') supported_notifications = [ :on_collection, :on_collection_end, :on_step, :on_step_end ] formatter1.should_receive(:implements) .at_least(69).times .and_return(supported_notifications) subject.register(formatter1) # Test the notifications send to the formatter formatter1.should_receive(:on_collection).once do |level, coll| expect(level).to eq(0) expect(coll).to eq(macro_coll) end formatter1.should_receive(:on_step).twice do |level, a_step| expect(level).to eq(1) expect(macro_coll.macro_steps.values).to include(a_step) end formatter1.should_receive(:on_step_end).twice do |level, arg| expect(level).to eq(1) expect(arg).to be_nil end formatter1.should_receive(:on_collection_end).once do |level, arg| expect(level).to eq(0) expect(arg).to be_nil end subject.start!(macro_coll) end it 'should support multiple formatters' do formatter1 = double('formatter') supported_notifications = [:on_collection] formatter1.should_receive(:implements) .at_least(69 * 3).times .and_return(supported_notifications) # Cheating: registering three times the same formatter... 3.times { subject.register(formatter1) } # ... then the collection is formatted three times formatter1.should_receive(:on_collection).at_least(3).times subject.start!(macro_coll) end end # context end # describe end # module # End of file