Sha256: abf649dda6846551f83fa2214386ef7807f93d0c2c8198c9cb8b71548486668b

Contents?: true

Size: 1.51 KB

Versions: 43

Compression:

Stored size: 1.51 KB

Contents

require 'json'
require 'gherkin/formatter/model'
require 'gherkin/native'

module Gherkin
  module Formatter
    class JSONFormatter
      native_impl('gherkin')
      
      attr_reader :gherkin_object
      
      # Creates a new instance that writes the resulting JSON to +io+.
      # If +io+ is nil, the JSON will not be written, but instead a Ruby
      # object can be retrieved with #gherkin_object
      def initialize(io)
        @io = io
      end

      def uri(uri)
        # We're ignoring the uri - we don't want it as part of the JSON
        # (The pretty formatter uses it just for visual niceness - comments)
      end

      def feature(feature)
        @gherkin_object = feature.to_hash
      end

      def background(background)
        feature_elements << background.to_hash
      end

      def scenario(scenario)
        feature_elements << scenario.to_hash
      end

      def scenario_outline(scenario_outline)
        feature_elements << scenario_outline.to_hash
      end

      def examples(examples)
        all_examples << examples.to_hash
      end

      def step(step)
        steps << step.to_hash
      end

      def eof
        @io.write(@gherkin_object.to_json) if @io
      end

    private

      def feature_elements
        @gherkin_object['elements'] ||= []
      end

      def feature_element
        feature_elements[-1]
      end

      def all_examples
        feature_element['examples'] ||= []
      end

      def steps
        feature_element['steps'] ||= []
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 2 rubygems

Version Path
vim-jar-0.1.2.0001 bundler/ruby/1.8/gems/gherkin-2.2.9/lib/gherkin/formatter/json_formatter.rb
vim-jar-0.1.2 bundler/ruby/1.8/gems/gherkin-2.2.9/lib/gherkin/formatter/json_formatter.rb
vim-jar-0.1.1 bundler/ruby/1.8/gems/gherkin-2.2.9/lib/gherkin/formatter/json_formatter.rb
vim-jar-0.1.0 bundler/ruby/1.8/gems/gherkin-2.2.9/lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.9 lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.9-x86-mswin32 lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.9-x86-mingw32 lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.9-universal-dotnet lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.9-java lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.8 lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.8-x86-mswin32 lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.8-x86-mingw32 lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.8-universal-dotnet lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.8-java lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.7 lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.7-x86-mswin32 lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.7-x86-mingw32 lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.7-universal-dotnet lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.7-java lib/gherkin/formatter/json_formatter.rb
gherkin-2.2.6 lib/gherkin/formatter/json_formatter.rb