Sha256: 63e9efe0c130b50cdf113e25d675b93fde84f442791944e8db05414faa1c6b31

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

# The 'gherkin' gem loads differently depending across versions. Try the old one first and then the new one
begin
  require 'gherkin'
rescue LoadError => e
  require 'gherkin/parser'
end

# Parsing will be with an 'adapter' appropriate to the version of the 'gherkin' gem that has been activated
if Gem.loaded_specs['gherkin'].version.version[/^3/]

  require 'gherkin/parser'
  require 'cuke_modeler/adapters/gherkin_3_adapter'


  module CukeModeler

    module Parsing
      class << self

        def parse_text(source_text)
          raise(ArgumentError, "Cannot parse #{source_text.class} objects. Strings only.") unless source_text.is_a?(String)

          parsed_result = Gherkin::Parser.new.parse(source_text)
          adapted_result = CukeModeler::Gherkin3Adapter.new.adapt(parsed_result)

          adapted_result
        end
      end
    end
  end

else

  require 'stringio'
  require 'gherkin/formatter/json_formatter'
  require 'gherkin'
  require 'json'
  require 'multi_json'
  require 'cuke_modeler/adapters/gherkin_2_adapter'


  module CukeModeler

    # A module providing source text parsing functionality.

    module Parsing

      class << self

        # Parses the Cucumber feature given in *source_text* and returns an array
        # containing the hash representation of its logical structure.
        def parse_text(source_text)
          raise(ArgumentError, "Cannot parse #{source_text.class} objects. Strings only.") unless source_text.is_a?(String)

          io = StringIO.new
          formatter = Gherkin::Formatter::JSONFormatter.new(io)
          parser = Gherkin::Parser::Parser.new(formatter)
          parser.parse(source_text, 'fake_file.txt', 0)
          formatter.done


          parsed_result = MultiJson.load(io.string)
          adapted_result = CukeModeler::Gherkin2Adapter.new.adapt(parsed_result)

          adapted_result
        end

      end

    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cuke_modeler-0.1.0 lib/cuke_modeler/parsing.rb