Capture expectations

This commit is contained in:
Michael Miller 2020-08-16 11:39:54 -06:00
parent 14608c8b2d
commit fab216419c
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
3 changed files with 45 additions and 5 deletions

View file

@ -0,0 +1,28 @@
module Spectator::SpecHelpers
# Information about an `expect` call in an example.
struct Expectation
# Indicates whether the expectation passed or failed.
getter? satisfied : Bool
# Message when the expectation failed.
# Only available when `#satisfied?` is false.
getter! message : String
# Additional information about the expectation.
# Only available when `#satisfied?` is false.
getter! values : Hash(String, String)
# Creates the expectation outcome.
def initialize(@satisfied, @message, @values)
end
# Extracts the expectation information from a `JSON::Any` object.
def self.from_json_any(object : JSON::Any)
satisfied = object["satisfied"].as_bool
message = object["failure"]?.try(&.as_s?)
values = object["values"]?.try(&.as_h?)
values = values.transform_values(&.as_s) if values
new(satisfied, message, values)
end
end
end

View file

@ -15,8 +15,11 @@ module Spectator::SpecHelpers
# Status of the example after running.
getter outcome : Outcome
# List of expectations ran in the example.
getter expectations : Array(Expectation)
# Creates the result.
def initialize(@name, @outcome)
def initialize(@name, @outcome, @expectations)
end
# Checks if the example was successful.
@ -43,16 +46,21 @@ module Spectator::SpecHelpers
def self.from_json_any(object : JSON::Any)
name = object["name"].as_s
outcome = parse_outcome_string(object["result"].as_s)
new(name, outcome)
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
when /success/i then Outcome::Success
when /fail/i then Outcome::Failure
when /error/i then Outcome::Error
else Outcome::Unknown
when /fail/i then Outcome::Failure
when /error/i then Outcome::Error
else Outcome::Unknown
end
end
end

View file

@ -20,6 +20,10 @@ Spectator.describe "Runtime compilation" do
expect(passing_example).to be_successful
end
it "can retrieve expectations" do
expect(passing_example.expectations).to_not be_empty
end
given_example failing_example do
it "does something" do
expect(true).to be_false