Class: Cucumber::Formatter::JSON

Inherits:
Object
  • Object
show all
Includes:
Io
Defined in:
lib/cucumber/formatter/json.rb,
lib/cucumber/formatter/json/version.rb

Constant Summary collapse

FORMATS =
Hash.new{|hash, format| hash[format] = method(format).to_proc}
VERSION =
'0.0.2'

Instance Method Summary collapse

Constructor Details

#initialize(step_mother, path_or_io, options) ⇒ JSON

Returns a new instance of JSON.



11
12
13
14
15
16
17
# File 'lib/cucumber/formatter/json.rb', line 11

def initialize(step_mother, path_or_io, options)
  @io = ensure_io(path_or_io, "json")
  @options = options
  @status_counts = Hash.new{|h,k| h[k] = 0}
  @indent = 0
  @hash = {}
end

Instance Method Details

#after_background(background) ⇒ Object



42
43
44
# File 'lib/cucumber/formatter/json.rb', line 42

def after_background(background)
  @in_background = nil
end

#after_feature_element(element) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'lib/cucumber/formatter/json.rb', line 182

def after_feature_element(element)
  @hash[:features] ||= []
  @hash[:features] << @feature
  @hash[:failing_features] ||= []
  if @element_exceptions.size > 0
    @hash[:failing_features] << @feature
  end
  @feature = ''
end

#after_features(steps) ⇒ Object



175
176
177
178
179
180
# File 'lib/cucumber/formatter/json.rb', line 175

def after_features(steps)
  @hash[:status_counts] = @status_counts
  @io.print(@hash.to_json)
  @io.flush
  @io.close
end

#after_multiline_arg(multiline_arg) ⇒ Object



130
131
132
# File 'lib/cucumber/formatter/json.rb', line 130

def after_multiline_arg(multiline_arg)
  @table = nil
end

#after_outline_table(outline_table) ⇒ Object



73
74
75
76
# File 'lib/cucumber/formatter/json.rb', line 73

def after_outline_table(outline_table)
  @table = nil
  @indent = 4
end

#after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background) ⇒ Object

mine



158
159
160
# File 'lib/cucumber/formatter/json.rb', line 158

def after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background)
  record_result(status, :step_match => step_match)
end

#after_table_cell(cell) ⇒ Object



140
141
142
143
# File 'lib/cucumber/formatter/json.rb', line 140

def after_table_cell(cell)
  return unless @table
  @col_index += 1
end

#after_table_row(table_row) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cucumber/formatter/json.rb', line 162

def after_table_row(table_row)
  unless @header_row
    record_result(table_row.status) if table_row.respond_to?(:status)
  end
  @header_row = false if @header_row
  return if !@table || @hide_this_step
  print_table_row_announcements
  @feature <<  "\n"
  if table_row.exception && !@exceptions.include?(table_row.exception)
    print_exception(table_row.exception, table_row.status, @indent)
  end
end

#background_name(keyword, name, file_colon_line, source_indent) ⇒ Object



46
47
48
# File 'lib/cucumber/formatter/json.rb', line 46

def background_name(keyword, name, file_colon_line, source_indent)
  print_feature_element_name(keyword, name, file_colon_line, source_indent)
end

#before_background(background) ⇒ Object



36
37
38
39
40
# File 'lib/cucumber/formatter/json.rb', line 36

def before_background(background)
  @indent = 2
  @scenario_indent = 2
  @in_background = true
end

#before_examples(*args) ⇒ Object



19
20
21
# File 'lib/cucumber/formatter/json.rb', line 19

def before_examples(*args)
  @header_row = true
end

#before_examples_array(examples_array) ⇒ Object



54
55
56
57
# File 'lib/cucumber/formatter/json.rb', line 54

def before_examples_array(examples_array)
  @indent = 4
  @visiting_first_example_name = true
end

#before_feature(feature) ⇒ Object



23
24
25
26
27
# File 'lib/cucumber/formatter/json.rb', line 23

def before_feature(feature)
  @exceptions = []
  @indent = 0
  @feature = ''
end

#before_feature_element(feature_element) ⇒ Object



29
30
31
32
33
34
# File 'lib/cucumber/formatter/json.rb', line 29

def before_feature_element(feature_element)
  @indent = 2
  @element_exceptions = []
  @feature = ""
  @scenario_indent = 2
end

#before_multiline_arg(multiline_arg) ⇒ Object



125
126
127
128
# File 'lib/cucumber/formatter/json.rb', line 125

def before_multiline_arg(multiline_arg)
  return if @options[:no_multiline] || @hide_this_step
  @table = multiline_arg
end

#before_outline_table(outline_table) ⇒ Object



69
70
71
# File 'lib/cucumber/formatter/json.rb', line 69

def before_outline_table(outline_table)
  @table = outline_table
end

#before_step(step) ⇒ Object



82
83
84
85
# File 'lib/cucumber/formatter/json.rb', line 82

def before_step(step)
  @current_step = step
  @indent = 6
end

#before_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cucumber/formatter/json.rb', line 87

def before_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background)
  @hide_this_step = false
  if exception
    if @exceptions.include?(exception)
      @hide_this_step = true
      return
    end
    @element_exceptions << exception
    @exceptions << exception
  end
  if status != :failed && @in_background ^ background
    @hide_this_step = true
    return
  end
  @status = status
end

#before_table_row(table_row) ⇒ Object



134
135
136
137
138
# File 'lib/cucumber/formatter/json.rb', line 134

def before_table_row(table_row)
  return if !@table || @hide_this_step
  @col_index = 0
  @feature <<  '  |'.indent(@indent-2)
end

#examples_name(keyword, name) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/cucumber/formatter/json.rb', line 59

def examples_name(keyword, name)
  @feature <<  "\n" unless @visiting_first_example_name
  @visiting_first_example_name = false
  names = name.strip.empty? ? [name.strip] : name.split("\n")
  @feature <<  "    #{keyword}: #{names[0]}\n"
  names[1..-1].each {|s| @feature <<  "      #{s}\n" } unless names.empty?
  @indent = 6
  @scenario_indent = 6
end

#exception(exception, status) ⇒ Object



120
121
122
123
# File 'lib/cucumber/formatter/json.rb', line 120

def exception(exception, status)
  return if @hide_this_step
  print_exception(exception, status, @indent)
end

#py_string(string) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/cucumber/formatter/json.rb', line 112

def py_string(string)
  return if @hide_this_step
  s = %{"""\n#{string}\n"""}.indent(@indent)
  s = s.split("\n").map{|l| l =~ /^\s+$/ ? '' : l}.join("\n")
  @feature <<  format_string(s, @current_step.status)
  @feature <<  "\n"
end

#scenario_name(keyword, name, file_colon_line, source_indent) ⇒ Object



78
79
80
# File 'lib/cucumber/formatter/json.rb', line 78

def scenario_name(keyword, name, file_colon_line, source_indent)
  print_feature_element_name(keyword, name, file_colon_line, source_indent)
end

#step_name(keyword, step_match, status, source_indent, background) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/cucumber/formatter/json.rb', line 104

def step_name(keyword, step_match, status, source_indent, background)
  return if @hide_this_step
  source_indent = nil unless @options[:source]
  name_to_report = format_step(keyword, step_match, status, source_indent)
  @feature <<  name_to_report.indent(@scenario_indent + 2)
  @feature <<  "\n"
end

#table_cell_value(value, status) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/cucumber/formatter/json.rb', line 145

def table_cell_value(value, status)
  return if !@table || @hide_this_step
  status ||= @status || :passed
  width = @table.col_width(@col_index)
  cell_text = value.to_s || ''
  padded = cell_text + (' ' * (width - cell_text.jlength))
  prefix = cell_prefix(status)
  @feature <<  ' ' + format_string("#{prefix}#{padded}", status) + ::Term::ANSIColor.reset(" |")
  @feature <<  "\n"
end