Class: Gherkin::Formatter::JSONFormatter

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

Overview

This class doesn’t really generate JSON - instead it populates an Array that can easily be turned into JSON.

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ JSONFormatter

Returns a new instance of JSONFormatter.



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

def initialize(io)
  raise "Must be writeable" unless io.respond_to?(:write)
  @io = io
  @feature_hashes = []
end

Instance Method Details

#background(background) ⇒ Object



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

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

#closeObject



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

def close
  @io.write(@feature_hashes.to_json)
end

#embedding(mime_type, data) ⇒ Object



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

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

#eofObject



75
76
# File 'lib/gherkin/formatter/json_formatter.rb', line 75

def eof
end

#examples(examples) ⇒ Object



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

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

#feature(feature) ⇒ Object



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

def feature(feature)
  @feature_hash = feature.to_hash
  @feature_hash['uri'] = @uri
  @feature_hashes << @feature_hash
end

#last_stepObject



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

def last_step
  current_steps[-1]
end

#match(match) ⇒ Object



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

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

#result(result) ⇒ Object



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

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

#scenario(scenario) ⇒ Object



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

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

#scenario_outline(scenario_outline) ⇒ Object



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

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

#step(step) ⇒ Object



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

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

#uri(uri) ⇒ Object



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

def uri(uri)
  @uri = uri
end