# frozen_string_literal: true # File: to-gherkin_spec.rb require 'stringio' require_relative '../../spec_helper' require_relative '../../../lib/macros4cuke/macro-step' # Load mix-in module for creating a sample collection of macro-steps require_relative '../use-sample-collection' require_relative '../../../lib/macros4cuke/formatting-service' # Load the class under test require_relative '../../../lib/macros4cuke/formatter/to-gherkin' module Macros4Cuke module Formatter # Open this namespace to get rid of module qualifier prefixes describe ToGherkin do include UseSampleCollection # Add convenience methods for sample collection let(:destination) { StringIO.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 'Creation and initialization:' do it 'should be created with an IO object' do an_io = StringIO.new expect { ToGherkin.new(an_io) }.not_to raise_error end end # context context 'Provided services:' do # Default instantiation rule subject { ToGherkin.new(destination) } # The expected feature file created from the sample collection let(:expected_output) do version = Macros4Cuke::Version now = Time.now.asctime trace_details = <<-SNIPPET # Feature file generated by Macro4Cuke #{version} on #{now} Feature: the set of macro-step definitions As a feature file writer So that I write higher-level steps Scenario: Macro 1 Given I define the step "* I [enter my credentials as ]:" to mean: """ Given I landed in the homepage When I click "Sign in" And I fill in "Username" with "" And I fill in "Password" with "" And I click "Submit" """ Scenario: Macro 2 Given I define the step "* I [fill in the form with]:" to mean: """ When I fill in "first_name" with "" And I fill in "last_name" with "" And I fill in "street_address" with "" And I fill in "zip" with "" And I fill in "city" with "" And I fill in "country" with "" # Let's assume that the e-mail field is optional. # The step between the ... will be executed # when the argument has a value assigned to it. And I fill in "email" with "" # Let's also assume that comment is also optional # See the slightly different syntax: the conditional section # ... may fit in a single line And I fill in "comment" with "" And I click "Save" """ SNIPPET trace_details end it 'should render the trace event for a given macro-step collection' do service = FormattingService.new service.register(subject) expect { service.start!(macro_coll) }.not_to raise_error # Line-by-line comparison actual_lines = subject.io.string.split(/\r\n?|\n/) expected_lines = expected_output.split(/\r\n?|\n/) # Exclude line 1 (because it is time-dependent) from the comparison actual_lines.shift expected_lines.shift # Remove starting/ending spaces actual_lines.map!(&:strip) expected_lines.map!(&:strip) expect(actual_lines).to eq(expected_lines) end end # context end # describe end # module end # module # End of file