0

I am sure this is a very stupid question but I cannot get my head around it.

I have following ruby code:

sample_test = "Feature: Some terse yet descriptive text of what is desired
Textual description of the business value of this feature
Business rules that govern the scope of the feature
Any additional information that will make the feature easier to understand
Scenario: Some determinable business situation
Given some precondition
  And some other precondition
 When some action by the actor
  And some other action
  And yet another action
 Then some testable outcome is achieved
  And something else we can check happens too"

io = StringIO.new
pretty_formatter    = Gherkin::Formatter::PrettyFormatter.new(io, true, false)
json_formatter      = Gherkin::Formatter::JSONFormatter.new(io)
parser = Gherkin::Parser::Parser.new(json_formatter)
result = parser.parse(sample_test, '', 0)

This returns True. But I want to get a JSON formatted result. What should I use to get JSON output of all the steps?

gok
  • 1,137
  • 1
  • 9
  • 30
  • 1
    Could you give an example of what the output should look like? It is unclear to me what the JSON blob should look like given the `sample_test`. – David Weiser Nov 18 '13 at 16:47
  • it doesn't matter, I just want to do my own stuff utilizing gherkin parser. I think this is a good example https://github.com/davidwilliam/cucumber_monitor#cucumbermonitor-api – gok Nov 18 '13 at 16:50
  • You'll have a better chance of getting answers if you show an example of what you want the output to be. – David Weiser Nov 18 '13 at 16:55
  • thanks @Davidann I found the answer now, despite all the googling prior asking the question. – gok Nov 18 '13 at 17:12

1 Answers1

0

ok, I found it. This official example works pretty well:

require 'gherkin/parser/parser'
require 'gherkin/formatter/json_formatter'
require 'stringio'
require 'multi_json'

# This example reads a couple of features and outputs them as JSON.

io = StringIO.new
formatter = Gherkin::Formatter::JSONFormatter.new(io)
parser = Gherkin::Parser::Parser.new(formatter)

sources = ["features/native_lexer.feature", "features/escaped_pipes.feature"]
sources.each do |s|
  path = File.expand_path(File.dirname(__FILE__) + '/../' + s)
  parser.parse(IO.read(path), path, 0)
end

formatter.done
puts MultiJson.dump(MultiJson.load(io.string), :pretty => true)
gok
  • 1,137
  • 1
  • 9
  • 30