Class: Res::Formatters::RubyCucumber2

Inherits:
Object
  • Object
show all
Includes:
Cucumber::Formatter::Io, FileUtils
Defined in:
lib/res/formatters/ruby_cucumber2.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime, path_or_io, options) ⇒ RubyCucumber2

Returns a new instance of RubyCucumber2.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/res/formatters/ruby_cucumber2.rb', line 15

def initialize(runtime, path_or_io, options)
  @runtime = runtime
  begin
    @io = ensure_io(path_or_io) 
  rescue
    @io = ensure_io(path_or_io, '')
  end
  @options = options
  @exceptions = []
  @indent = 0
  @prefixes = options[:prefixes] || {}
  @delayed_messages = []
  @_start_time = Time.now
end

Class Method Details

.split_uri(uri) ⇒ Object



245
246
247
248
# File 'lib/res/formatters/ruby_cucumber2.rb', line 245

def self.split_uri(uri)
  strings = uri.rpartition(/:/)
  { :file => strings[0], :line => strings[2].to_i, :urn => uri }
end

Instance Method Details

#after_background(background) ⇒ Object



143
144
# File 'lib/res/formatters/ruby_cucumber2.rb', line 143

def after_background(background)
end

#after_feature(feature) ⇒ Object



88
89
90
# File 'lib/res/formatters/ruby_cucumber2.rb', line 88

def after_feature(feature)
  @_feature[:finished] = Time.now()
end

#after_feature_element(feature_element) ⇒ Object

After a scenario



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/res/formatters/ruby_cucumber2.rb', line 114

def after_feature_element(feature_element)
  @_context = {}

  scenario_class = Cucumber::Formatter::LegacyApi::Ast::Scenario
  example_table_class = Cucumber::Core::Ast::Location

  fail =  @runtime.scenarios(:failed).select do |s|
    [scenario_class, example_table_class].include?(s.class)
  end.map do |s|
    if s.location.lines == feature_element.location.lines and s.location.file == feature_element.location.file
      s
    end          
  end

  if fail.compact.empty? and feature_element.respond_to? :status
    @_feature_element[:status] = feature_element.status if feature_element.status.to_s != "skipped"
  else
    fail = fail.compact
    @_feature_element[:status] = fail[0].status
  end

  @_feature_element[:finished] = Time.now
  @_feature_element[:values] = Res.perf_data.pop if !Res.perf_data.empty?
end

#after_features(features) ⇒ Object

Once everything has run – whack it in a ResultIR object and dump it as json



36
37
38
39
40
41
42
43
# File 'lib/res/formatters/ruby_cucumber2.rb', line 36

def after_features(features)
  results = @_features
  ir = ::Res::IR.new( :started     => @_start_time,
                      :finished    => Time.now(),
                      :results     => results,
                      :type        => 'Cucumber' )
  @io.puts ir.json
end

#after_multiline_arg(multiline_arg) ⇒ Object



192
193
194
195
# File 'lib/res/formatters/ruby_cucumber2.rb', line 192

def after_multiline_arg(multiline_arg)
  @_context[:args] = multiline_arg.to_s.gsub(/\e\[(\d+)m/, '')
  @_table = nil
end

#after_outline_table(outline_table) ⇒ Object



206
207
208
209
210
211
# File 'lib/res/formatters/ruby_cucumber2.rb', line 206

def after_outline_table(outline_table)
  headings = @_table.shift
  description = @_outlines.collect{ |o| o[:name] }.join("\n") + "\n" + headings[:name]
  @_feature_element[:children] = @_table
  @_feature_element[:description] = description
end

#after_table_cell(cell) ⇒ Object



236
237
# File 'lib/res/formatters/ruby_cucumber2.rb', line 236

def after_table_cell(cell)
end

#after_table_row(table_row) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/res/formatters/ruby_cucumber2.rb', line 218

def after_table_row(table_row)
  if table_row.class == Cucumber::Formatter::LegacyApi::Ast::ExampleTableRow

    @_current_table_row[:name] = table_row.name
    if table_row.exception
      @_current_table_row[:message] = table_row.exception.to_s
    end

    if table_row.status and table_row.status != "skipped" and table_row.status != nil
      @_current_table_row[:status] = table_row.status
    end
      
    @_current_table_row[:line] = table_row.line
    @_current_table_row[:urn] = @_feature_element[:file] + ":" + table_row.line.to_s
    @_table << @_current_table_row
  end
end

#after_tags(tags) ⇒ Object



66
67
# File 'lib/res/formatters/ruby_cucumber2.rb', line 66

def after_tags(tags)
end

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



146
147
# File 'lib/res/formatters/ruby_cucumber2.rb', line 146

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

#before_background(background) ⇒ Object



139
140
141
# File 'lib/res/formatters/ruby_cucumber2.rb', line 139

def before_background(background)
  #@_context[:background] = background
end

#before_feature(feature) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/res/formatters/ruby_cucumber2.rb', line 45

def before_feature(feature)
  @_feature = {}
  @_context = {}
  @_feature[:started] = Time.now()
  begin
    hash = RubyCucumber2.split_uri( feature.location.to_s )
    @_feature[:file] = hash[:file]
    @_feature[:line] = hash[:line]
    @_feature[:urn]  = hash[:urn]
  rescue
    @_feature[:uri] = 'unknown'
  end
  @_features << @_feature
  @_context = @_feature
end

#before_feature_element(feature_element) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/res/formatters/ruby_cucumber2.rb', line 92

def before_feature_element(feature_element)

  @_feature_element = {}
  @_context = {}
  @_feature_element[:started] = Time.now
  begin
    hash = RubyCucumber2.split_uri( feature_element.location.to_s )
    @_feature_element[:file] = hash[:file]
    @_feature_element[:line] = hash[:line]
    @_feature_element[:urn] = hash[:urn]
  rescue => e
    @_feature_element[:error] = e.message
    @_feature_element[:file] = 'unknown'
  end

  @_feature[:children] = [] if ! @_feature[:children]

  @_feature[:children] << @_feature_element
  @_context = @_feature_element
end

#before_features(features) ⇒ Object



30
31
32
# File 'lib/res/formatters/ruby_cucumber2.rb', line 30

def before_features(features)
  @_features = []
end

#before_multiline_arg(multiline_arg) ⇒ Object



189
190
# File 'lib/res/formatters/ruby_cucumber2.rb', line 189

def before_multiline_arg(multiline_arg)
end

#before_outline_table(outline_table) ⇒ Object

Before a scenario outline is encountered



198
199
200
201
202
203
204
# File 'lib/res/formatters/ruby_cucumber2.rb', line 198

def before_outline_table(outline_table)
  # Scenario outlines appear as children like normal scenarios,
  # but really we just want to construct normal-looking children
  # from them
  @_outlines = @_feature_element[:children]
  @_table = []
end

#before_step(step) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/res/formatters/ruby_cucumber2.rb', line 158

def before_step(step)
  @_step = {}

  # Background steps can appear totally divorced from scenerios (feature
  # elements). Need to make sure we're not including them as children
  # to scenario that don't exist
  return if @_feature_element && @_feature_element[:finished]

  @_feature_element = {} if !@_feature_element
  @_feature_element[:children] = [] if !@_feature_element[:children]
  @_feature_element[:children] << @_step
  @_context = @_step
end

#before_table_row(table_row) ⇒ Object



213
214
215
216
# File 'lib/res/formatters/ruby_cucumber2.rb', line 213

def before_table_row(table_row)
  @_current_table_row = { :type => 'Cucumber::ScenarioOutline::Example' }
  @_table = [] if !@_table
end

#comment_line(comment_line) ⇒ Object



61
62
63
64
# File 'lib/res/formatters/ruby_cucumber2.rb', line 61

def comment_line(comment_line)
  @_context[:comments] = [] if !@_context[:comments]
  @_context[:comments] << comment_line 
end

#examples_name(keyword, name) ⇒ Object



149
150
# File 'lib/res/formatters/ruby_cucumber2.rb', line 149

def examples_name(keyword, name)
end

#exception(exception, status) ⇒ Object



185
186
187
# File 'lib/res/formatters/ruby_cucumber2.rb', line 185

def exception(exception, status)
  @_context[:message] = exception.to_s
end

#feature_name(keyword, name) ⇒ Object

{ :type => ‘Feature’,

:name => 'Feature name',
:description => "As a blah\nAs a blah\n" }


78
79
80
81
82
83
84
85
86
# File 'lib/res/formatters/ruby_cucumber2.rb', line 78

def feature_name(keyword, name)
  @_feature[:type] = "Cucumber::" + keyword.gsub(/\s+/, "")

  lines = name.split("\n")
  lines = lines.collect { |l| l.strip }

  @_feature[:name] = lines.shift
  @_feature[:description] = lines.join("\n")
end

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



153
154
155
156
# File 'lib/res/formatters/ruby_cucumber2.rb', line 153

def scenario_name(keyword, name, file_colon_line, source_indent)
  @_context[:type] = "Cucumber::" + keyword.gsub(/\s+/, "")
  @_context[:name] = name || ''
end

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



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/res/formatters/ruby_cucumber2.rb', line 172

def step_name(keyword, step_match, status, source_indent, background, *args)

  file_colon_line = args[0] if args[0]

  @_step[:type] = "Cucumber::Step"
  name = keyword + step_match.format_args(lambda{|param| %{#{param}}}) 
  @_step[:name] = name
  @_step[:status] = status
  #@_step[:background] = background
  @_step[:type] = "Cucumber::Step"

end

#table_cell_value(value, status) ⇒ Object



239
240
241
242
243
# File 'lib/res/formatters/ruby_cucumber2.rb', line 239

def table_cell_value(value, status)
  @_current_table_row[:children] = [] if !@_current_table_row[:children]
  @_current_table_row[:children] << { :type => "Cucumber::ScenarioOutline::Parameter",
                                      :name => value, :status => status }
end

#tag_name(tag_name) ⇒ Object



69
70
71
72
73
# File 'lib/res/formatters/ruby_cucumber2.rb', line 69

def tag_name(tag_name)
  @_context[:tags] = [] if !@_context[:tag]
  # Strip @ from tags
  @_context[:tags] << tag_name[1..-1]
end