mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Capture expectations
This commit is contained in:
parent
14608c8b2d
commit
fab216419c
3 changed files with 45 additions and 5 deletions
28
spec/helpers/expectation.cr
Normal file
28
spec/helpers/expectation.cr
Normal 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
|
|
@ -15,8 +15,11 @@ module Spectator::SpecHelpers
|
||||||
# Status of the example after running.
|
# Status of the example after running.
|
||||||
getter outcome : Outcome
|
getter outcome : Outcome
|
||||||
|
|
||||||
|
# List of expectations ran in the example.
|
||||||
|
getter expectations : Array(Expectation)
|
||||||
|
|
||||||
# Creates the result.
|
# Creates the result.
|
||||||
def initialize(@name, @outcome)
|
def initialize(@name, @outcome, @expectations)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Checks if the example was successful.
|
# Checks if the example was successful.
|
||||||
|
@ -43,16 +46,21 @@ module Spectator::SpecHelpers
|
||||||
def self.from_json_any(object : JSON::Any)
|
def self.from_json_any(object : JSON::Any)
|
||||||
name = object["name"].as_s
|
name = object["name"].as_s
|
||||||
outcome = parse_outcome_string(object["result"].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
|
end
|
||||||
|
|
||||||
# Converts a result string, such as "fail" to an enum value.
|
# Converts a result string, such as "fail" to an enum value.
|
||||||
private def self.parse_outcome_string(string)
|
private def self.parse_outcome_string(string)
|
||||||
case string
|
case string
|
||||||
when /success/i then Outcome::Success
|
when /success/i then Outcome::Success
|
||||||
when /fail/i then Outcome::Failure
|
when /fail/i then Outcome::Failure
|
||||||
when /error/i then Outcome::Error
|
when /error/i then Outcome::Error
|
||||||
else Outcome::Unknown
|
else Outcome::Unknown
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,6 +20,10 @@ Spectator.describe "Runtime compilation" do
|
||||||
expect(passing_example).to be_successful
|
expect(passing_example).to be_successful
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "can retrieve expectations" do
|
||||||
|
expect(passing_example.expectations).to_not be_empty
|
||||||
|
end
|
||||||
|
|
||||||
given_example failing_example do
|
given_example failing_example do
|
||||||
it "does something" do
|
it "does something" do
|
||||||
expect(true).to be_false
|
expect(true).to be_false
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue