shard-spectator/src/spectator/result.cr

48 lines
1.4 KiB
Crystal
Raw Normal View History

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.
def initialize(@elapsed, @expectations = [] of Expectation)
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
# Indicates whether the example passed.
abstract def pass? : Bool
# Indicates whether the example failed.
abstract def fail? : Bool
# Indicates whether the example was skipped.
def pending? : Bool
!pass? && !fail?
end
2021-01-31 03:07:36 +00:00
# Creates a JSON object from the result information.
def to_json(json : ::JSON::Builder, example)
2021-01-31 03:07:36 +00:00
json.object do
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.
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