Class: Gherkin::JSONParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gherkin/json_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(formatter) ⇒ JSONParser

Returns a new instance of JSONParser.



9
10
11
# File 'lib/gherkin/json_parser.rb', line 9

def initialize(formatter)
  @formatter = formatter
end

Instance Method Details

#comments(o) ⇒ Object



60
61
62
63
64
# File 'lib/gherkin/json_parser.rb', line 60

def comments(o)
  (o['comments'] || []).map do |comment|
    Formatter::Model::Comment.new(comment['value'], comment['line'])
  end
end

#description(o) ⇒ Object



80
81
82
# File 'lib/gherkin/json_parser.rb', line 80

def description(o)
  o['description']
end

#feature_element(o) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/gherkin/json_parser.rb', line 33

def feature_element(o)
  case o['type']
  when 'background'
    Formatter::Model::Background.new(comments(o), keyword(o), name(o), description(o), line(o))
  when 'scenario'
    Formatter::Model::Scenario.new(comments(o), tags(o), keyword(o), name(o), description(o), line(o))
  when 'scenario_outline'
    Formatter::Model::ScenarioOutline.new(comments(o), tags(o), keyword(o), name(o), description(o), line(o))
  end
end

#keyword(o) ⇒ Object



72
73
74
# File 'lib/gherkin/json_parser.rb', line 72

def keyword(o)
  o['keyword']
end

#line(o) ⇒ Object



84
85
86
# File 'lib/gherkin/json_parser.rb', line 84

def line(o)
  o['line']
end

#name(o) ⇒ Object



76
77
78
# File 'lib/gherkin/json_parser.rb', line 76

def name(o)
  o['name']
end

#parse(o, feature_uri = 'unknown.json', line_offset = 0) ⇒ Object

Parse a gherkin object o, which can either be a JSON String, or a Hash (from a parsed JSON String).



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gherkin/json_parser.rb', line 15

def parse(o, feature_uri='unknown.json', line_offset=0)
  o = JSON.parse(o) if String === o
  @formatter.uri(feature_uri)

  Formatter::Model::Feature.new(comments(o), tags(o), keyword(o), name(o), description(o), line(o)).replay(@formatter)
  (o["elements"] || []).each do |feature_element|
    feature_element(feature_element).replay(@formatter)
    (feature_element["steps"] || []).each do |step|
      step(step).replay(@formatter)
    end
    (feature_element["examples"] || []).each do |eo|
      Formatter::Model::Examples.new(comments(eo), tags(eo), keyword(eo), name(eo), description(eo), line(eo), rows(eo['rows'])).replay(@formatter)
    end
  end

  @formatter.eof
end

#rows(o) ⇒ Object



56
57
58
# File 'lib/gherkin/json_parser.rb', line 56

def rows(o)
  o.map{|row| Formatter::Model::Row.new(comments(row), row['cells'], row['line'])}
end

#step(o) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gherkin/json_parser.rb', line 44

def step(o)
  multiline_arg = nil
  if(ma = o['multiline_arg'])
    if(ma['type'] == 'table')
      multiline_arg = rows(ma['value'])
    else
      multiline_arg = Formatter::Model::PyString.new(ma['value'], ma['line'])
    end
  end
  Formatter::Model::Step.new(comments(o), keyword(o), name(o), line(o), multiline_arg)
end

#tags(o) ⇒ Object



66
67
68
69
70
# File 'lib/gherkin/json_parser.rb', line 66

def tags(o)
  (o['tags'] || []).map do |tag|
    Formatter::Model::Tag.new(tag['name'], tag['line'])
  end
end