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

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

Constant Summary

Constants included from Helpers

Helpers::DEFAULT_PRECISION, Helpers::SUB_SECOND_PRECISION

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#duration, #example_count, #example_group, #examples, #failed_examples, #failure_count, #output, #pending_count, #pending_examples

Instance Method Summary collapse

Methods inherited from BaseFormatter

#dump_failures, #dump_pending, #example_failed, #example_group_finished, #example_group_started, #example_passed, #example_pending, #example_started, #format_backtrace, #start, #start_dump

Methods included from Helpers

#format_duration, #format_seconds, #pluralize, #strip_trailing_zeroes

Methods included from BacktraceFormatter

#format_backtrace

Constructor Details

#initialize(output) ⇒ JsonFormatter

Returns a new instance of JsonFormatter.



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

def initialize(output)
  super
  @output_hash = {}
end

Instance Attribute Details

#output_hashObject (readonly)

Returns the value of attribute output_hash.



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

def output_hash
  @output_hash
end

Instance Method Details

#closeObject



62
63
64
65
# File 'lib/rspec/core/formatters/json_formatter.rb', line 62

def close
  output.write @output_hash.to_json
  output.close if IO === output && output != $stdout
end

#dump_summary(duration, example_count, failure_count, pending_count) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/rspec/core/formatters/json_formatter.rb', line 21

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

#message(message) ⇒ Object



17
18
19
# File 'lib/rspec/core/formatters/json_formatter.rb', line 17

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

#stopObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rspec/core/formatters/json_formatter.rb', line 39

def stop
  super
  @output_hash[:examples] = examples.map do |example|
    {
      :description => example.description,
      :full_description => example.full_description,
      :status => example.execution_result[:status],
      # :example_group,
      # :execution_result,
      :file_path => example.[:file_path],
      :line_number  => example.[:line_number],
    }.tap do |hash|
      if e=example.exception
        hash[:exception] =  {
          :class => e.class.name,
          :message => e.message,
          :backtrace => e.backtrace,
        }
      end
    end
  end
end

#summary_line(example_count, failure_count, pending_count) ⇒ Object



32
33
34
35
36
37
# File 'lib/rspec/core/formatters/json_formatter.rb', line 32

def summary_line(example_count, failure_count, pending_count)
  summary = pluralize(example_count, "example")
  summary << ", " << pluralize(failure_count, "failure")
  summary << ", #{pending_count} pending" if pending_count > 0
  summary
end