Domanda

Sono sicuro che questa è una domanda molto stupida, ma non posso ottenere la mia testa intorno ad esso.

Ho il seguente codice ruby:

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)

Questo ritorna True.Ma voglio ottenere un risultato formattato JSON.Cosa dovrei usare per ottenere l'output JSON di tutti i passaggi?

È stato utile?

Soluzione

ok, l'ho trovato. Questo esempio ufficiale funziona abbastanza bene:

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)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top