Class: Lucid::Formatter::GherkinFormatterAdapter

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

Overview

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

Direct Known Subclasses

Gpretty, Json

Instance Method Summary collapse

Constructor Details

#initialize(gherkin_formatter, print_empty_match) ⇒ GherkinFormatterAdapter

Returns a new instance of GherkinFormatterAdapter.



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

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

Instance Method Details

#after_feature(feature) ⇒ Object



78
79
80
# File 'lib/lucid/formatter/gherkin_formatter_adapter.rb', line 78

def after_feature(feature)
  @gf.eof
end

#after_features(features) ⇒ Object



82
83
84
# File 'lib/lucid/formatter/gherkin_formatter_adapter.rb', line 82

def after_features(features)
  @gf.done
end

#after_step(step) ⇒ Object

used for capturing duration



73
74
75
76
# File 'lib/lucid/formatter/gherkin_formatter_adapter.rb', line 73

def after_step(step)
  step_finish = (Time.now - @step_time)
  @gf.append_duration(step_finish)
end

#before_background(background) ⇒ Object



19
20
21
22
# File 'lib/lucid/formatter/gherkin_formatter_adapter.rb', line 19

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

#before_examples(examples) ⇒ Object



68
69
70
# File 'lib/lucid/formatter/gherkin_formatter_adapter.rb', line 68

def before_examples(examples)
  @gf.examples(examples.gherkin_statement)
end

#before_feature(feature) ⇒ Object



14
15
16
17
# File 'lib/lucid/formatter/gherkin_formatter_adapter.rb', line 14

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

#before_feature_element(feature_element) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lucid/formatter/gherkin_formatter_adapter.rb', line 24

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
    @gf.scenario_outline(feature_element.gherkin_statement)
  else
    raise "Bad type: #{feature_element.class}"
  end
end

#before_step(step) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lucid/formatter/gherkin_formatter_adapter.rb', line 37

def before_step(step)
  @gf.step(step.gherkin_statement)
  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(step_result) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lucid/formatter/gherkin_formatter_adapter.rb', line 50

def before_step_result(step_result)
  arguments = step_result.step_arguments.map{|a| Gherkin::Formatter::Argument.new(a.offset, a.val)}
  location = step_result.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
    @gf.match(match)
  end

  exception = step_result.exception
  error_message = exception ? "#{exception.message} (#{exception.class})\n#{exception.backtrace.join("\n")}" : nil
  unless @outline
    @gf.result(Gherkin::Formatter::Model::Result.new(step_result.status, nil, error_message))
  end
end

#embed(file, mime_type, label) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/lucid/formatter/gherkin_formatter_adapter.rb', line 86

def embed(file, mime_type, label)
  data = File.open(file, 'rb') { |f| f.read }
  if defined?(JRUBY_VERSION)
    data = data.to_java_bytes
  end
  @gf.embedding(mime_type, data)
end