2021-06-03 04:48:48 +00:00
|
|
|
require "json"
|
2021-07-17 22:04:19 +00:00
|
|
|
require "./example_failed"
|
|
|
|
require "./location"
|
2020-10-17 20:56:31 +00:00
|
|
|
require "./result"
|
|
|
|
|
|
|
|
module Spectator
|
|
|
|
# Outcome that indicates an example failed.
|
|
|
|
# This typically means an assertion did not pass.
|
|
|
|
class FailResult < Result
|
|
|
|
# Error that occurred while running the example.
|
|
|
|
# This describes the primary reason for the failure.
|
|
|
|
getter error : Exception
|
|
|
|
|
|
|
|
# Creates a failure result.
|
|
|
|
# The *elapsed* argument is the length of time it took to run the example.
|
|
|
|
# The *error* is the exception raised that caused the failure.
|
2021-04-27 00:47:11 +00:00
|
|
|
def initialize(elapsed, @error, expectations = [] of Expectation)
|
|
|
|
super(elapsed, expectations)
|
2020-10-17 20:56:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Calls the `failure` method on *visitor*.
|
|
|
|
def accept(visitor)
|
2021-05-16 18:19:16 +00:00
|
|
|
visitor.fail(self)
|
2020-10-17 20:56:31 +00:00
|
|
|
end
|
|
|
|
|
2021-01-31 02:42:46 +00:00
|
|
|
# Calls the `failure` method on *visitor*.
|
|
|
|
def accept(visitor)
|
2021-05-16 01:42:59 +00:00
|
|
|
visitor.fail(yield self)
|
2021-01-31 02:42:46 +00:00
|
|
|
end
|
|
|
|
|
2021-05-13 03:39:50 +00:00
|
|
|
# Indicates whether the example passed.
|
|
|
|
def pass? : Bool
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Indicates whether the example failed.
|
|
|
|
def fail? : Bool
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2021-07-17 22:04:19 +00:00
|
|
|
# Attempts to retrieve the location where the example failed.
|
|
|
|
# This only works if the location of the failed expectation was reported.
|
|
|
|
# If available, returns a `Location`, otherwise `nil`.
|
2021-07-17 22:34:15 +00:00
|
|
|
def location? : Location?
|
2021-07-17 22:04:19 +00:00
|
|
|
return unless error = @error.as?(ExampleFailed)
|
|
|
|
|
|
|
|
error.location?
|
|
|
|
end
|
|
|
|
|
2021-07-17 22:34:15 +00:00
|
|
|
# Attempts to retrieve the location where the example failed.
|
|
|
|
# This only works if the location of the failed expectation was reported.
|
|
|
|
# If available, returns a `Location`, otherwise raises `NilAssertionError`.
|
|
|
|
def location : Location
|
|
|
|
location? || raise(NilAssertionError.new("Source location of result unavailable"))
|
|
|
|
end
|
|
|
|
|
2020-10-17 20:56:31 +00:00
|
|
|
# One-word description of the result.
|
|
|
|
def to_s(io)
|
|
|
|
io << "fail"
|
|
|
|
end
|
2021-01-31 02:42:46 +00:00
|
|
|
|
2021-06-03 04:48:48 +00:00
|
|
|
# Creates a JSON object from the result information.
|
|
|
|
def to_json(json : JSON::Builder)
|
2021-01-31 03:07:36 +00:00
|
|
|
super
|
2021-06-03 04:48:48 +00:00
|
|
|
json.field("status", json_status)
|
|
|
|
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
|
|
|
|
|
|
|
|
# String used for the JSON status field.
|
|
|
|
# Necessary for the error result to override the status, but nothing else from `#to_json`.
|
|
|
|
private def json_status
|
|
|
|
"failed"
|
2021-01-31 02:42:46 +00:00
|
|
|
end
|
2020-10-17 20:56:31 +00:00
|
|
|
end
|
|
|
|
end
|