class NdjsonFormatter

Constants

VERSION

Public Class Methods

new(io) click to toggle source
# File lib/ndjson_formatter.rb, line 13
def initialize(io)
  @io = io
  @testables = {}
end

Public Instance Methods

example_failed(failed_example_notification) click to toggle source
# File lib/ndjson_formatter.rb, line 47
def example_failed(failed_example_notification)
  update_testable(failed_example_notification.example,
                  status: "failed",
                  message: failed_example_notification.message_lines,
                  backtrace: failed_example_notification.formatted_backtrace)
end
example_group_started(group_notification) click to toggle source
# File lib/ndjson_formatter.rb, line 18
def example_group_started(group_notification)
  group = group_notification.group
  insert_testable({
    id: group.id,
    type: "suite",
    label: group.description,
    file: group.file_path,
    line: group.metadata[:line_number].to_i,
    children: [],
    parent_id: group_parent_id(group),
  })
end
example_passed(example_notification) click to toggle source
# File lib/ndjson_formatter.rb, line 43
def example_passed(example_notification)
  update_testable(example_notification.example, status: "passed")
end
example_pending(example_notification) click to toggle source
# File lib/ndjson_formatter.rb, line 54
def example_pending(example_notification)
  update_testable(example_notification.example,
                  status: "pending",
                  message: Array(example_notification.example.execution_result.pending_message))
end
example_started(example_notification) click to toggle source
# File lib/ndjson_formatter.rb, line 31
def example_started(example_notification)
  ex = example_notification.example
  insert_testable({
    id: ex.id,
    type: "test",
    label: ex.description,
    file: ex.file_path,
    line: ex.metadata[:line_number],
    parent_id: example_parent_id(ex),
  })
end
stop(_arg) click to toggle source
# File lib/ndjson_formatter.rb, line 60
def stop(_arg)
  dump
end

Private Instance Methods

dump() click to toggle source
# File lib/ndjson_formatter.rb, line 66
def dump
  @io.puts JSON.dump(@top_level)
end
example_parent_id(testable = nil) click to toggle source
# File lib/ndjson_formatter.rb, line 99
def example_parent_id(testable = nil)
  format_id(testable.metadata[:example_group])
end
format_id(metadata) click to toggle source
# File lib/ndjson_formatter.rb, line 94
def format_id(metadata)
  return unless metadata
  "#{metadata.fetch(:file_path)}[#{metadata.fetch(:scoped_id)}]"
end
group_parent_id(testable = nil) click to toggle source
# File lib/ndjson_formatter.rb, line 103
def group_parent_id(testable = nil)
  return if testable.nil?
  format_id(testable.metadata[:parent_example_group])
end
insert_testable(testable) click to toggle source
# File lib/ndjson_formatter.rb, line 78
def insert_testable(testable)
  parent_id = testable[:parent_id]
  parentless_testable = strip_parent_id(testable)
  if top_level?(testable)
    dump unless @top_level.nil?
    @top_level = parentless_testable
  else
    @testables[parent_id][:children] << parentless_testable
  end
  @testables[parentless_testable[:id]] = parentless_testable
end
strip_parent_id(testable) click to toggle source
# File lib/ndjson_formatter.rb, line 70
def strip_parent_id(testable)
  testable.reject { |k, _| [:parent_id].include?(k) }
end
top_level?(testable) click to toggle source
# File lib/ndjson_formatter.rb, line 74
def top_level?(testable)
  testable[:parent_id].nil?
end
update_testable(testable, attributes) click to toggle source
# File lib/ndjson_formatter.rb, line 90
def update_testable(testable, attributes)
  @testables[testable.id].merge!(attributes)
end