Class: Rutidy::Formatter

Inherits:
RSpec::Core::Formatters::BaseFormatter
  • Object
show all
Defined in:
lib/rutidy/formatter.rb

Overview

A real RSpec formatter (registered the same way RSpec's own built-in DocumentationFormatter is), not a post-processor of some other formatter's text output. example_group_started/example_group_finished fire as the runner actually descends/ascends the real describe/context tree, so @hierarchy always reflects genuine nesting -- unlike stock rspec --format json, whose full_description field is every group's description concatenated with plain spaces and no separator, which can't be split back into a tree without guessing. This formatter never needs to guess: every example gets its own hierarchy array straight from the stack RSpec itself maintains.

Load with rspec --require rutidy/formatter --format Rutidy::Formatter, or let rutidy's own CLI wrap that invocation for you.

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ Formatter

Returns a new instance of Formatter.



28
29
30
31
32
# File 'lib/rutidy/formatter.rb', line 28

def initialize(output)
  super
  @hierarchy = []
  @output_hash = {version: Rutidy::VERSION}
end

Instance Method Details

#close(_notification) ⇒ Object



74
75
76
# File 'lib/rutidy/formatter.rb', line 74

def close(_notification)
  output.write JSON.generate(@output_hash)
end

#dump_summary(summary) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/rutidy/formatter.rb', line 58

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
  }
  @output_hash[:summary_line] = summary.totals_line
end

#example_failed(notification) ⇒ Object



54
55
56
# File 'lib/rutidy/formatter.rb', line 54

def example_failed(notification)
  record(notification.example, notification)
end

#example_group_finished(_notification) ⇒ Object



42
43
44
# File 'lib/rutidy/formatter.rb', line 42

def example_group_finished(_notification)
  @hierarchy.pop
end

#example_group_started(notification) ⇒ Object



38
39
40
# File 'lib/rutidy/formatter.rb', line 38

def example_group_started(notification)
  @hierarchy << notification.group.description.strip
end

#example_passed(notification) ⇒ Object



46
47
48
# File 'lib/rutidy/formatter.rb', line 46

def example_passed(notification)
  record(notification.example)
end

#example_pending(notification) ⇒ Object



50
51
52
# File 'lib/rutidy/formatter.rb', line 50

def example_pending(notification)
  record(notification.example)
end

#message(notification) ⇒ Object



34
35
36
# File 'lib/rutidy/formatter.rb', line 34

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

#seed(notification) ⇒ Object



68
69
70
71
72
# File 'lib/rutidy/formatter.rb', line 68

def seed(notification)
  return unless notification.seed_used?

  @output_hash[:seed] = notification.seed
end