Simplify JSON formatter by reusing Example's to_json

This commit is contained in:
Michael Miller 2021-06-02 23:44:36 -06:00
parent 8536fcf58c
commit bd34b87e22
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436

View file

@ -21,75 +21,9 @@ module Spectator::Formatting
@json.start_array @json.start_array
end end
# Begins an example object and adds common fields known before running the example.
def example_started(notification)
example = notification.example
@json.start_object
@json.field("description", example.name? || "<anonymous>")
@json.field("full_description", example.to_s)
if location = example.location?
@json.field("file_path", location.path)
@json.field("line_number", location.line)
end
end
# Adds fields to the example object for all result types known after the example completes. # Adds fields to the example object for all result types known after the example completes.
def example_finished(notification) def example_finished(notification)
example = notification.example notification.example.to_json(@json)
result = example.result
@json.field("run_time", result.elapsed.total_seconds)
@json.field("expectations") do
@json.array do
result.expectations.each(&.to_json(@json))
end
end
end
# Adds success-specific fields to an example object and closes it.
def example_passed(_notification)
@json.field("status", "passed")
@json.end_object # End example object.
end
# Adds pending-specific fields to an example object and closes it.
def example_pending(_notification)
@json.field("status", "pending")
@json.field("pending_message", "Not implemented") # TODO: Fetch pending message from result.
@json.end_object # End example object.
end
# Adds failure-specific fields to an example object and closes it.
def example_failed(notification)
example = notification.example
result = example.result
@json.field("status", "failed")
build_exception_object(result.error) if result.responds_to?(:error)
@json.end_object # End example object.
end
# Adds error-specific fields to an example object and closes it.
def example_error(notification)
example = notification.example
result = example.result
@json.field("status", "error")
build_exception_object(result.error) if result.responds_to?(:error)
@json.end_object # End example object.
end
# Adds an exception field and object to the JSON document.
private def build_exception_object(error)
@json.field("exception") do
@json.object do
@json.field("class", error.class.name)
@json.field("message", error.message)
@json.field("backtrace", error.backtrace)
end
end
end end
# Marks the end of the examples array. # Marks the end of the examples array.