2021-06-03 04:48:48 +00:00
|
|
|
require "json"
|
|
|
|
require "./expectation"
|
|
|
|
|
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-05-16 18:31:01 +00:00
|
|
|
# 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.
|
2021-06-03 04:48:48 +00:00
|
|
|
def to_json(json : JSON::Builder)
|
|
|
|
json.field("run_time", @elapsed.total_seconds)
|
|
|
|
json.field("expectations") do
|
|
|
|
json.array do
|
|
|
|
@expectations.each(&.to_json(json))
|
|
|
|
end
|
2021-01-31 03:07:36 +00:00
|
|
|
end
|
|
|
|
end
|
2018-08-31 03:07:14 +00:00
|
|
|
end
|
|
|
|
end
|