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

Direct Known Subclasses

JsonPretty

Defined Under Namespace

Classes: Builder

Instance Method Summary collapse

Methods included from Io

ensure_dir, ensure_file, ensure_io

Constructor Details

#initialize(config) ⇒ Json

Returns a new instance of Json



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

def initialize(config)
  @io = ensure_io(config.out_stream)
  @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

#embed(src, mime_type, _label) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cucumber/formatter/json.rb', line 84

def embed(src, mime_type, _label)
  if File.file?(src)
    content = File.open(src, 'rb', &:read)
    data = encode64(content)
  else
    if mime_type =~ /;base64$/
      mime_type = mime_type[0..-8]
      data = src
    else
      data = encode64(src)
    end
  end
  test_step_embeddings << { mime_type: mime_type, data: data }
end

#on_test_case_finished(event) ⇒ Object



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

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



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

def on_test_case_started(event)
  test_case = event.test_case
  builder = Builder.new(test_case)
  unless same_feature_as_previous_test_case?(test_case.feature)
    @feature_hash = builder.feature_hash
    @feature_hashes << @feature_hash
  end
  @test_case_hash = builder.test_case_hash
  if builder.background?
    feature_elements << builder.background_hash
    @element_hash = builder.background_hash
  else
    feature_elements << @test_case_hash
    @element_hash = @test_case_hash
  end
  @any_step_failed = false
end

#on_test_run_finished(_event) ⇒ Object



76
77
78
# File 'lib/cucumber/formatter/json.rb', line 76

def on_test_run_finished(_event)
  @io.write(MultiJson.dump(@feature_hashes, pretty: true))
end

#on_test_step_finished(event) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/cucumber/formatter/json.rb', line 62

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



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

def on_test_step_started(event)
  test_step = event.test_step
  return if internal_hook?(test_step)
  hook_query = HookQueryVisitor.new(test_step)
  if hook_query.hook?
    @step_or_hook_hash = {}
    hooks_of_type(hook_query) << @step_or_hook_hash
    return
  end
  if first_step_after_background?(test_step)
    feature_elements << @test_case_hash
    @element_hash = @test_case_hash
  end
  @step_or_hook_hash = create_step_hash(test_step.source.last)
  steps << @step_or_hook_hash
  @step_hash = @step_or_hook_hash
end

#puts(message) ⇒ Object



80
81
82
# File 'lib/cucumber/formatter/json.rb', line 80

def puts(message)
  test_step_output << message
end