Class: Cucumber::Formatter::Json

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

Overview

The formatter used for --format json

Defined Under Namespace

Classes: Builder

Instance Method Summary collapse

Methods included from Io

ensure_dir, ensure_file, ensure_io, included, io?, url?

Constructor Details

#initialize(config) ⇒ Json

Returns a new instance of Json.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cucumber/formatter/json.rb', line 15

def initialize(config)
  @io = ensure_io(config.out_stream, config.error_stream)
  @ast_lookup = AstLookup.new(config)
  @feature_hashes = []
  @step_or_hook_hash = {}
  config.on_event :test_case_started, &method(:on_test_case_started)
  config.on_event :test_case_finished, &method(:on_test_case_finished)
  config.on_event :test_step_started, &method(:on_test_step_started)
  config.on_event :test_step_finished, &method(:on_test_step_finished)
  config.on_event :test_run_finished, &method(:on_test_run_finished)
end

Instance Method Details

#attach(src, mime_type, _filename) ⇒ Object



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

def attach(src, mime_type, _filename)
  if mime_type == 'text/x.cucumber.log+plain'
    test_step_output << src
    return
  end
  if mime_type =~ /;base64$/
    mime_type = mime_type[0..-8]
    data = src
  else
    data = encode64(src)
  end
  test_step_embeddings << { mime_type: mime_type, data: data }
end

#on_test_case_finished(event) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/cucumber/formatter/json.rb', line 76

def on_test_case_finished(event)
  feature_elements << @test_case_hash if @in_background

  _test_case, result = *event.attributes
  result = result.with_filtered_backtrace(Cucumber::Formatter::BacktraceFilter)
  add_failed_around_hook(result) if result.failed? && !@any_step_failed
end

#on_test_case_started(event) ⇒ Object



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

def on_test_case_started(event)
  test_case = event.test_case
  builder = Builder.new(test_case, @ast_lookup)
  unless same_feature_as_previous_test_case?(test_case)
    @feature_hash = builder.feature_hash
    @feature_hashes << @feature_hash
  end
  @test_case_hash = builder.test_case_hash

  @element_hash = nil
  @element_background_hash = builder.background_hash
  @in_background = builder.background?

  @any_step_failed = false
end

#on_test_run_finished(_event) ⇒ Object



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

def on_test_run_finished(_event)
  @io.write(JSON.pretty_generate(@feature_hashes))
end

#on_test_step_finished(event) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/cucumber/formatter/json.rb', line 67

def on_test_step_finished(event)
  test_step, result = *event.attributes
  result = result.with_filtered_backtrace(Cucumber::Formatter::BacktraceFilter)
  return if internal_hook?(test_step)

  add_match_and_result(test_step, result)
  @any_step_failed = true if result.failed?
end

#on_test_step_started(event) ⇒ Object



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

def on_test_step_started(event)
  test_step = event.test_step
  return if internal_hook?(test_step)

  if @element_hash.nil?
    @element_hash = create_element_hash(test_step)
    feature_elements << @element_hash
  end

  if test_step.hook?
    @step_or_hook_hash = {}
    hooks_of_type(test_step) << @step_or_hook_hash
    return
  end
  if first_step_after_background?(test_step)
    @in_background = false
    feature_elements << @test_case_hash
    @element_hash = @test_case_hash
  end
  @step_or_hook_hash = create_step_hash(test_step)
  steps << @step_or_hook_hash
  @step_hash = @step_or_hook_hash
end