2018-08-31 03:07:14 +00:00
|
|
|
module Spectator
|
2018-11-16 16:48:35 +00:00
|
|
|
# Base class that represents the outcome of running an example.
|
|
|
|
# Sub-classes contain additional information specific to the type of result.
|
2018-09-15 19:25:11 +00:00
|
|
|
abstract class Result
|
2020-10-17 20:56:31 +00:00
|
|
|
# Length of time it took to run the example.
|
|
|
|
getter elapsed : Time::Span
|
2018-11-16 16:48:35 +00:00
|
|
|
|
2020-10-17 20:56:31 +00:00
|
|
|
# The assertions checked in the example.
|
2021-01-31 02:42:46 +00:00
|
|
|
getter expectations : Enumerable(Expectation)
|
2019-02-21 04:00:22 +00:00
|
|
|
|
2020-10-17 20:56:31 +00:00
|
|
|
# Creates the result.
|
|
|
|
# *elapsed* is the length of time it took to run the example.
|
2021-04-27 00:47:11 +00:00
|
|
|
def initialize(@elapsed, @expectations = [] of Expectation)
|
2019-03-23 03:23:14 +00:00
|
|
|
end
|
|
|
|
|
2020-10-17 20:56:31 +00:00
|
|
|
# Calls the corresponding method for the type of result.
|
|
|
|
# This is the visitor design pattern.
|
|
|
|
abstract def accept(visitor)
|
2021-01-31 03:07:36 +00:00
|
|
|
|
2021-05-13 03:39:50 +00:00
|
|
|
# Indicates whether the example passed.
|
|
|
|
abstract def pass? : Bool
|
|
|
|
|
|
|
|
# Indicates whether the example failed.
|
|
|
|
abstract def fail? : Bool
|
|
|
|
|
2021-01-31 03:07:36 +00:00
|
|
|
# Creates a JSON object from the result information.
|
2021-04-27 00:47:11 +00:00
|
|
|
def to_json(json : ::JSON::Builder, example)
|
2021-01-31 03:07:36 +00:00
|
|
|
json.object do
|
2021-04-27 00:47:11 +00:00
|
|
|
add_json_fields(json, example)
|
2021-01-31 03:07:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Adds the common fields for a result to a JSON builder.
|
2021-04-27 00:47:11 +00:00
|
|
|
private def add_json_fields(json : ::JSON::Builder, example)
|
2021-01-31 03:07:36 +00:00
|
|
|
json.field("name", example)
|
2021-02-13 05:46:22 +00:00
|
|
|
json.field("location", example.location)
|
2021-01-31 03:07:36 +00:00
|
|
|
json.field("result", to_s)
|
|
|
|
json.field("time", elapsed.total_seconds)
|
|
|
|
json.field("expectations", expectations)
|
|
|
|
end
|
2018-08-31 03:07:14 +00:00
|
|
|
end
|
|
|
|
end
|