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
20
# 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 = []
  @current_step_or_hook = nil
end

Instance Method Details

#after(match, result) ⇒ Object



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

def after(match, result)
  add_hook(match, result, "after")
end

#append_duration(timestamp) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/gherkin/formatter/json_formatter.rb', line 65

def append_duration(timestamp)
	#check to make sure result exists (scenario outlines do not have results yet)
	if !@current_step_or_hook['result'].nil?
  	#convert to nanoseconds
  	timestamp = timestamp * 1000000000
  	rshash = @current_step_or_hook['result'].to_hash
  	rshash['duration'] = timestamp.to_i
  	@current_step_or_hook['result'] = rshash
 	end
end

#background(background) ⇒ Object



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

def background(background)
  feature_elements << background.to_hash
end

#before(match, result) ⇒ Object



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

def before(match, result)
  add_hook(match, result, "before")
end

#doneObject



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

def done
  @io.write(MultiJson.dump(@feature_hashes, :pretty => true))
end

#embedding(mime_type, data) ⇒ Object



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

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

#eofObject



92
93
# File 'lib/gherkin/formatter/json_formatter.rb', line 92

def eof
end

#examples(examples) ⇒ Object



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

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

#feature(feature) ⇒ Object



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

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

#match(match) ⇒ Object



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

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

#result(result) ⇒ Object



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

def result(result)
  @current_step_or_hook['result'] = result.to_hash
end

#scenario(scenario) ⇒ Object



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

def scenario(scenario)
  feature_elements << scenario.to_hash
end

#scenario_outline(scenario_outline) ⇒ Object



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

def scenario_outline(scenario_outline)
  feature_elements << scenario_outline.to_hash
end

#step(step) ⇒ Object



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

def step(step)
  @current_step_or_hook = step.to_hash
  steps << @current_step_or_hook
end

#uri(uri) ⇒ Object



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

def uri(uri)
  @uri = uri
end

#write(text) ⇒ Object



88
89
90
# File 'lib/gherkin/formatter/json_formatter.rb', line 88

def write(text)
  output << text
end