Class: Cucumber::Formatter::GherkinFormatterAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/formatter/gherkin_formatter_adapter.rb

Overview

Adapts Cucumber formatter events to Gherkin formatter events This class will disappear when Cucumber is based on Gherkin’s model.

Direct Known Subclasses

Gpretty, Json

Instance Method Summary collapse

Constructor Details

#initialize(gherkin_formatter, print_empty_match, options) ⇒ GherkinFormatterAdapter

Returns a new instance of GherkinFormatterAdapter.



9
10
11
12
13
# File 'lib/cucumber/formatter/gherkin_formatter_adapter.rb', line 9

def initialize(gherkin_formatter, print_empty_match, options)
  @gf = gherkin_formatter
  @print_empty_match = print_empty_match
  @options = options
end

Instance Method Details

#after_feature(feature) ⇒ Object



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

def after_feature(feature)
  @gf.eof
end

#after_features(features) ⇒ Object



144
145
146
# File 'lib/cucumber/formatter/gherkin_formatter_adapter.rb', line 144

def after_features(features)
  @gf.done
end

#after_step(step) ⇒ Object

used for capturing duration



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

def after_step(step)
  step_finish = (Time.now - @step_time)
  unless @outline and @options[:expand] and not @in_instantiated_scenario
    @gf.append_duration(step_finish)
  end
end

#before_background(background) ⇒ Object



20
21
22
23
# File 'lib/cucumber/formatter/gherkin_formatter_adapter.rb', line 20

def before_background(background)
  @outline = false
  @gf.background(background.gherkin_statement)
end

#before_examples(examples) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/cucumber/formatter/gherkin_formatter_adapter.rb', line 122

def before_examples(examples)
  unless @options[:expand]
    @gf.examples(examples.gherkin_statement)
  else
    @in_instantiated_scenario = true
    @new_example_table = true
    @current_example_rows = to_hash(examples.gherkin_statement)['rows']
  end
end

#before_feature(feature) ⇒ Object



15
16
17
18
# File 'lib/cucumber/formatter/gherkin_formatter_adapter.rb', line 15

def before_feature(feature)
  @gf.uri(feature.file)
  @gf.feature(feature.gherkin_statement)
end

#before_feature_element(feature_element) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cucumber/formatter/gherkin_formatter_adapter.rb', line 25

def before_feature_element(feature_element)
  case(feature_element)
  when Ast::Scenario
    @outline = false
    @gf.scenario(feature_element.gherkin_statement)
  when Ast::ScenarioOutline
    @outline = true
    if @options[:expand]
      @in_instantiated_scenario = false
      @current_scenario_hash = to_hash(feature_element.gherkin_statement)
    else
      @gf.scenario_outline(feature_element.gherkin_statement)
    end
  else
    raise "Bad type: #{feature_element.class}"
  end
end

#before_step(step) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cucumber/formatter/gherkin_formatter_adapter.rb', line 65

def before_step(step)
  unless @outline and @options[:expand]
    @gf.step(step.gherkin_statement)
  else 
    if @in_instantiated_scenario
      @current_step_hash = to_hash(step.gherkin_statement)
    end
  end
  if @print_empty_match
    if(@outline)
      match = Gherkin::Formatter::Model::Match.new(step.gherkin_statement.outline_args, nil)
    else
      match = Gherkin::Formatter::Model::Match.new([], nil)
    end
    @gf.match(match)
  end
  @step_time = Time.now
end

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cucumber/formatter/gherkin_formatter_adapter.rb', line 84

def before_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background, file_colon_line)
  arguments = step_match.step_arguments.map{|a| Gherkin::Formatter::Argument.new(a.offset, a.val)}
  location = step_match.file_colon_line
  match = Gherkin::Formatter::Model::Match.new(arguments, location)
  if @print_empty_match
    # Trick the formatter to believe that's what was printed previously so we get arg highlights on #result
    @gf.instance_variable_set('@match', match)
  else
    unless @outline and @options[:expand]
      @gf.match(match)
    end
  end

  error_message = exception ? "#{exception.message} (#{exception.class})\n#{exception.backtrace.join("\n")}" : nil
  unless @outline
    @gf.result(Gherkin::Formatter::Model::Result.new(status, nil, error_message))
  else
    if @options[:expand] and @in_instantiated_scenario
      @current_match = match
      @current_result = Gherkin::Formatter::Model::Result.new(status, nil, error_message)
    end
  end
end

#embed(file, mime_type, label) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cucumber/formatter/gherkin_formatter_adapter.rb', line 148

def embed(file, mime_type, label)
  if File.file?(file)
    data = File.open(file, 'rb') { |f| f.read }
  else
    if mime_type =~ /;base64$/
      mime_type = mime_type[0..-8]
      data = Base64.decode64(file)
    else
      data = file
    end
  end
  if defined?(JRUBY_VERSION)
    data = data.to_java_bytes
  end
  @gf.embedding(mime_type, data)
end

#puts(message) ⇒ Object



165
166
167
# File 'lib/cucumber/formatter/gherkin_formatter_adapter.rb', line 165

def puts(message)
  @gf.write(message)
end

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cucumber/formatter/gherkin_formatter_adapter.rb', line 43

def scenario_name(keyword, name, file_colon_line, source_indent)
  if @outline and @options[:expand]
    return if not @in_instantiated_scenario
    if @new_example_table
      @example_row = 1
      @new_example_table = false
    else
      @example_row += 1
    end
    example_row_hash = @current_example_rows[@example_row].to_hash
    scenario = Gherkin::Formatter::Model::Scenario.new(
        @current_scenario_hash['comments'],
        @current_scenario_hash['tags'],
        @current_scenario_hash['keyword'],
        @current_scenario_hash['name'],
        @current_scenario_hash['description'],
        example_row_hash['line'],
        example_row_hash['id'])
    @gf.scenario(scenario)
  end
end

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



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cucumber/formatter/gherkin_formatter_adapter.rb', line 108

def step_name(keyword, step_match, status, source_indent, background, file_colon_line)
  if @outline and @options[:expand] and @in_instantiated_scenario
    @gf.step(Gherkin::Formatter::Model::Step.new(
        @current_step_hash['comments'],
        @current_step_hash['keyword'],
        step_match.format_args(),
        @current_step_hash['line'],
        @current_step_hash['rows'],
        @current_step_hash['doc_string']))
    @gf.match(@current_match)
    @gf.result(@current_result)
  end
end