首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将gherkin解析为json

将gherkin解析为json
EN

Stack Overflow用户
提问于 2013-11-18 16:40:16
回答 1查看 964关注 0票数 0

我相信这是一个非常愚蠢的问题,但我无法理解。

我有以下红宝石代码:

代码语言:javascript
复制
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)

这将返回True。但是我想得到一个JSON格式的结果。我应该使用什么来获得所有步骤的JSON输出?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-18 16:57:18

好吧,我找到了。这个官方的例子运行得很好:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20053004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档