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.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cucumber/formatter/json.rb', line 16

def initialize(config)
  Cucumber::Deprecate::CliOption.deprecate(
    config.error_stream,
    '--format=json',
    "Please use --format=message and stand-alone json-formatter.\n" \
    'json-formatter homepage: https://github.com/cucumber/cucumber/tree/master/json-formatter#cucumber-json-formatter',
    '6.0.0'
  )

  @io = ensure_io(config.out_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) ⇒ Object


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cucumber/formatter/json.rb', line 92

def attach(src, mime_type)
  if mime_type == 'text/x.cucumber.log+plain'
    test_step_output << src
    return
  end
  if File.file?(src)
    content = File.open(src, 'rb', &:read)
    data = encode64(content)
  elsif 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


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

def on_test_case_finished(event)
  _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


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cucumber/formatter/json.rb', line 36

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
  if builder.background?
    @in_background = true
    feature_elements << builder.background_hash
    @element_hash = builder.background_hash
  else
    @in_background = false
    feature_elements << @test_case_hash
    @element_hash = @test_case_hash
  end
  @any_step_failed = false
end

#on_test_run_finished(_event) ⇒ Object


88
89
90
# File 'lib/cucumber/formatter/json.rb', line 88

def on_test_run_finished(_event)
  @io.write(JSON.generate(@feature_hashes, pretty: true))
end

#on_test_step_finished(event) ⇒ Object


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

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


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cucumber/formatter/json.rb', line 56

def on_test_step_started(event)
  test_step = event.test_step
  return if internal_hook?(test_step)
  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