Re-add JSON output for some types

This commit is contained in:
Michael Miller 2021-01-30 20:07:36 -07:00
parent 9d139dfeed
commit bda554739c
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
7 changed files with 69 additions and 19 deletions

View file

@ -20,9 +20,28 @@ module Spectator
io << "error"
end
# TODO
def to_json(builder)
builder.string("ERROR")
# Adds the common JSON fields for all result types
# and fields specific to errored results.
private def add_json_fields(json : ::JSON::Builder)
super
json.field("exceptions") do
exception = error
json.array do
while exception
error_to_json(exception, json) if exception
exception = error.cause
end
end
end
end
# Adds a single exception to a JSON builder.
private def error_to_json(error : Exception, json : ::JSON::Builder)
json.object do
json.field("type", error.class.to_s)
json.field("message", error.message)
json.field("backtrace", error.backtrace)
end
end
end
end

View file

@ -149,9 +149,10 @@ module Spectator
io << result
end
# TODO
def to_json(builder)
builder.string("EXAMPLE")
# Creates the JSON representation of the example,
# which is just its name.
def to_json(json : ::JSON::Builder)
json.string(to_s)
end
# Wraps an example to behave like a `Proc`.

View file

@ -52,6 +52,29 @@ module Spectator
def initialize(@match_data : Matchers::MatchData, @source : Source? = nil)
end
# Creates the JSON representation of the expectation.
def to_json(json : ::JSON::Builder)
json.object do
json.field("source") { @source.to_json(json) }
json.field("satisfied", satisfied?)
if (failed = @match_data.as?(Matchers::FailedMatchData))
failed_to_json(failed, json)
end
end
end
# Adds failure information to a JSON structure.
private def failed_to_json(failed : Matchers::FailedMatchData, json : ::JSON::Builder)
json.field("failure", failed.failure_message)
json.field("values") do
json.object do
failed.values.each do |pair|
json.field(pair.first, pair.last)
end
end
end
end
# Stores part of an expectation.
# This covers the actual value (or block) being inspected and its source.
# This is the type returned by an `expect` block in the DSL.

View file

@ -30,9 +30,10 @@ module Spectator
io << "fail"
end
# TODO
def to_json(builder)
builder.string("FAIL")
# Adds all of the JSON fields for finished results and failed results.
private def add_json_fields(json : ::JSON::Builder)
super
json.field("error", error.message)
end
end
end

View file

@ -17,10 +17,5 @@ module Spectator
def to_s(io)
io << "pass"
end
# TODO
def to_json(builder)
builder.string("PASS")
end
end
end

View file

@ -25,10 +25,5 @@ module Spectator
def to_s(io)
io << "pending"
end
# TODO
def to_json(builder)
builder.string("PENDING")
end
end
end

View file

@ -20,5 +20,21 @@ module Spectator
# Calls the corresponding method for the type of result.
# This is the visitor design pattern.
abstract def accept(visitor)
# Creates a JSON object from the result information.
def to_json(json : ::JSON::Builder)
json.object do
add_json_fields(json)
end
end
# Adds the common fields for a result to a JSON builder.
private def add_json_fields(json : ::JSON::Builder)
json.field("name", example)
json.field("location", example.source)
json.field("result", to_s)
json.field("time", elapsed.total_seconds)
json.field("expectations", expectations)
end
end
end