Class: Gherkin::Formatter::JSONFormatter

Inherits:
Object
  • Object
show all
Includes:
Base64
Defined in:
lib/gherkin/formatter/json_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ JSONFormatter

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



17
18
19
# File 'lib/gherkin/formatter/json_formatter.rb', line 17

def initialize(io)
  @io = io
end

Instance Attribute Details

#gherkin_objectObject (readonly)

Returns the value of attribute gherkin_object.



12
13
14
# File 'lib/gherkin/formatter/json_formatter.rb', line 12

def gherkin_object
  @gherkin_object
end

Instance Method Details

#background(background) ⇒ Object



30
31
32
33
# File 'lib/gherkin/formatter/json_formatter.rb', line 30

def background(background)
  feature_elements << background.to_hash
  @step_index = 0
end

#embedding(mime_type, data) ⇒ Object



66
67
68
# File 'lib/gherkin/formatter/json_formatter.rb', line 66

def embedding(mime_type, data)
  embeddings << {'mime_type' => mime_type, 'data' => encode64s(data)}
end

#eofObject



70
71
72
# File 'lib/gherkin/formatter/json_formatter.rb', line 70

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

#examples(examples) ⇒ Object



45
46
47
# File 'lib/gherkin/formatter/json_formatter.rb', line 45

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

#feature(feature) ⇒ Object



26
27
28
# File 'lib/gherkin/formatter/json_formatter.rb', line 26

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

#last_stepObject



62
63
64
# File 'lib/gherkin/formatter/json_formatter.rb', line 62

def last_step
  current_steps[-1]
end

#match(match) ⇒ Object



53
54
55
# File 'lib/gherkin/formatter/json_formatter.rb', line 53

def match(match)
  current_steps[@step_index]['match'] = match.to_hash
end

#result(result) ⇒ Object



57
58
59
60
# File 'lib/gherkin/formatter/json_formatter.rb', line 57

def result(result)
  current_steps[@step_index]['result'] = result.to_hash
  @step_index += 1
end

#scenario(scenario) ⇒ Object



35
36
37
38
# File 'lib/gherkin/formatter/json_formatter.rb', line 35

def scenario(scenario)
  feature_elements << scenario.to_hash
  @step_index = 0
end

#scenario_outline(scenario_outline) ⇒ Object



40
41
42
43
# File 'lib/gherkin/formatter/json_formatter.rb', line 40

def scenario_outline(scenario_outline)
  feature_elements << scenario_outline.to_hash
  @step_index = 0
end

#step(step) ⇒ Object



49
50
51
# File 'lib/gherkin/formatter/json_formatter.rb', line 49

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

#uri(uri) ⇒ Object



21
22
23
24
# File 'lib/gherkin/formatter/json_formatter.rb', line 21

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