Class: RSpec::Core::Formatters::JsonFormatter

Inherits:
BaseFormatter show all
Defined in:
lib/rspec/core/formatters/json_formatter.rb

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#example_group, #output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#example_group_started, #start

Constructor Details

#initialize(output) ⇒ JsonFormatter

Returns a new instance of JsonFormatter



13
14
15
16
17
18
# File 'lib/rspec/core/formatters/json_formatter.rb', line 13

def initialize(output)
  super
  @output_hash = {
    :version => RSpec::Core::Version::STRING
  }
end

Instance Attribute Details

#output_hashObject (readonly)

Returns the value of attribute output_hash



11
12
13
# File 'lib/rspec/core/formatters/json_formatter.rb', line 11

def output_hash
  @output_hash
end

Instance Method Details

#close(_notification) ⇒ Object



55
56
57
# File 'lib/rspec/core/formatters/json_formatter.rb', line 55

def close(_notification)
  output.write @output_hash.to_json
end

#dump_profile(profile) ⇒ Object



59
60
61
62
63
# File 'lib/rspec/core/formatters/json_formatter.rb', line 59

def dump_profile(profile)
  @output_hash[:profile] = {}
  dump_profile_slowest_examples(profile)
  dump_profile_slowest_example_groups(profile)
end

#dump_profile_slowest_example_groups(profile) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



78
79
80
81
82
83
# File 'lib/rspec/core/formatters/json_formatter.rb', line 78

def dump_profile_slowest_example_groups(profile)
  @output_hash[:profile] ||= {}
  @output_hash[:profile][:groups] = profile.slowest_groups.map do |loc, hash|
    hash.update(:location => loc)
  end
end

#dump_profile_slowest_examples(profile) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



66
67
68
69
70
71
72
73
74
75
# File 'lib/rspec/core/formatters/json_formatter.rb', line 66

def dump_profile_slowest_examples(profile)
  @output_hash[:profile] = {}
  @output_hash[:profile][:examples] = profile.slowest_examples.map do |example|
    format_example(example).tap do |hash|
      hash[:run_time] = example.execution_result.run_time
    end
  end
  @output_hash[:profile][:slowest] = profile.slow_duration
  @output_hash[:profile][:total] = profile.duration
end

#dump_summary(summary) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/rspec/core/formatters/json_formatter.rb', line 24

def dump_summary(summary)
  @output_hash[:summary] = {
    :duration => summary.duration,
    :example_count => summary.example_count,
    :failure_count => summary.failure_count,
    :pending_count => summary.pending_count,
    :errors_outside_of_examples_count => summary.errors_outside_of_examples_count
  }
  @output_hash[:summary_line] = summary.totals_line
end

#message(notification) ⇒ Object



20
21
22
# File 'lib/rspec/core/formatters/json_formatter.rb', line 20

def message(notification)
  (@output_hash[:messages] ||= []) << notification.message
end

#seed(notification) ⇒ Object



50
51
52
53
# File 'lib/rspec/core/formatters/json_formatter.rb', line 50

def seed(notification)
  return unless notification.seed_used?
  @output_hash[:seed] = notification.seed
end

#stop(notification) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rspec/core/formatters/json_formatter.rb', line 35

def stop(notification)
  @output_hash[:examples] = notification.examples.map do |example|
    format_example(example).tap do |hash|
      e = example.exception
      if e
        hash[:exception] =  {
          :class => e.class.name,
          :message => e.message,
          :backtrace => e.backtrace,
        }
      end
    end
  end
end