shard-spectator/spec/helpers/result.cr

68 lines
1.7 KiB
Crystal
Raw Normal View History

module Spectator::SpecHelpers
# Information about an example compiled and run at runtime.
2020-08-16 17:00:46 +00:00
struct Result
# Status of the example after running.
enum Outcome
Success
Failure
Error
Unknown
end
# Full name and description of the example.
getter name : String
# Status of the example after running.
getter outcome : Outcome
2020-08-16 17:39:54 +00:00
# List of expectations ran in the example.
getter expectations : Array(Expectation)
# Creates the result.
2020-08-16 17:39:54 +00:00
def initialize(@name, @outcome, @expectations)
end
# Checks if the example was successful.
def success?
outcome.success?
end
# :ditto:
def successful?
outcome.success?
end
# Checks if the example failed, but did not error.
def failure?
outcome.failure?
end
# Checks if the example encountered an error.
def error?
outcome.error?
end
# Extracts the result information from a `JSON::Any` object.
def self.from_json_any(object : JSON::Any)
name = object["description"].as_s
outcome = parse_outcome_string(object["status"].as_s)
2020-08-16 17:39:54 +00:00
expectations = if (list = object["expectations"].as_a?)
list.map { |e| Expectation.from_json_any(e) }
else
[] of Expectation
end
new(name, outcome, expectations)
end
# Converts a result string, such as "fail" to an enum value.
private def self.parse_outcome_string(string)
case string
2021-01-31 03:14:25 +00:00
when /pass/i then Outcome::Success
when /fail/i then Outcome::Failure
when /error/i then Outcome::Error
else Outcome::Unknown
end
end
end
end