mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add instance_of matcher to check exact type
This commit is contained in:
parent
8cfed440ed
commit
93c442d1e2
2 changed files with 83 additions and 0 deletions
|
@ -122,6 +122,32 @@ module Spectator
|
||||||
be_a({{expected}})
|
be_a({{expected}})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Indicates that some value should be of a specified type.
|
||||||
|
# The value's runtime class is checked.
|
||||||
|
# A type name or type union should be used for *expected*.
|
||||||
|
#
|
||||||
|
# Examples:
|
||||||
|
# ```
|
||||||
|
# expect(123).to be_instance_of(Int32)
|
||||||
|
# ```
|
||||||
|
macro be_instance_of(expected)
|
||||||
|
::Spectator::Matchers::InstanceMatcher({{expected}}).new
|
||||||
|
end
|
||||||
|
|
||||||
|
# Indicates that some value should be of a specified type.
|
||||||
|
# The value's runtime class is checked.
|
||||||
|
# A type name or type union should be used for *expected*.
|
||||||
|
# This method is identical to `#be_an_instance_of`,
|
||||||
|
# and exists just to improve grammar.
|
||||||
|
#
|
||||||
|
# Examples:
|
||||||
|
# ```
|
||||||
|
# expect(123).to be_an_instance_of(Int32)
|
||||||
|
# ```
|
||||||
|
macro be_an_instance_of(expected)
|
||||||
|
be_instance_of({{expected}})
|
||||||
|
end
|
||||||
|
|
||||||
# Indicates that some value should respond to a method call.
|
# Indicates that some value should respond to a method call.
|
||||||
# One or more method names can be provided.
|
# One or more method names can be provided.
|
||||||
#
|
#
|
||||||
|
|
57
src/spectator/matchers/instance_matcher.cr
Normal file
57
src/spectator/matchers/instance_matcher.cr
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
require "./matcher"
|
||||||
|
|
||||||
|
module Spectator::Matchers
|
||||||
|
# Matcher that tests a value is of a specified type.
|
||||||
|
struct InstanceMatcher(Expected) < StandardMatcher
|
||||||
|
# Short text about the matcher's purpose.
|
||||||
|
# This explains what condition satisfies the matcher.
|
||||||
|
# The description is used when the one-liner syntax is used.
|
||||||
|
def description : String
|
||||||
|
"is an instance of #{Expected}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Checks whether the matcher is satisifed with the expression given to it.
|
||||||
|
private def match?(actual : TestExpression(T)) : Bool forall T
|
||||||
|
actual.value.class == Expected
|
||||||
|
end
|
||||||
|
|
||||||
|
# Message displayed when the matcher isn't satisifed.
|
||||||
|
#
|
||||||
|
# This is only called when `#match?` returns false.
|
||||||
|
#
|
||||||
|
# The message should typically only contain the test expression labels.
|
||||||
|
# Actual values should be returned by `#values`.
|
||||||
|
private def failure_message(actual) : String
|
||||||
|
"#{actual.label} is not an instance of #{Expected}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Message displayed when the matcher isn't satisifed and is negated.
|
||||||
|
# This is essentially what would satisfy the matcher if it wasn't negated.
|
||||||
|
#
|
||||||
|
# This is only called when `#does_not_match?` returns false.
|
||||||
|
#
|
||||||
|
# The message should typically only contain the test expression labels.
|
||||||
|
# Actual values should be returned by `#values`.
|
||||||
|
private def failure_message_when_negated(actual) : String
|
||||||
|
"#{actual.label} is an instance of #{Expected}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Additional information about the match failure.
|
||||||
|
# The return value is a NamedTuple with Strings for each value.
|
||||||
|
private def values(actual)
|
||||||
|
{
|
||||||
|
expected: Expected.to_s,
|
||||||
|
actual: actual.value.class.inspect,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
# Additional information about the match failure when negated.
|
||||||
|
# The return value is a NamedTuple with Strings for each value.
|
||||||
|
private def negated_values(actual)
|
||||||
|
{
|
||||||
|
expected: "Not #{Expected}",
|
||||||
|
actual: actual.value.class.inspect,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue