Add matcher to check compiled type of values

This commit is contained in:
Michael Miller 2021-02-12 18:33:50 -07:00
parent 3083f82132
commit 08451df643
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
3 changed files with 75 additions and 2 deletions

View file

@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support dynamic creation of examples. - Support dynamic creation of examples.
- Capture and log information for hooks. - Capture and log information for hooks.
- Tags can be added to examples and example groups. - Tags can be added to examples and example groups.
- Add matcher to check compiled type of values.
### Changed ### Changed
- Simplify and reduce defined types and generics. Should speed up compilation times. - Simplify and reduce defined types and generics. Should speed up compilation times.

View file

@ -123,7 +123,7 @@ module Spectator::DSL
end end
# Indicates that some value should be of a specified type. # Indicates that some value should be of a specified type.
# The value's runtime class is checked. # The value's runtime type is checked.
# A type name or type union should be used for *expected*. # A type name or type union should be used for *expected*.
# #
# Examples: # Examples:
@ -135,7 +135,7 @@ module Spectator::DSL
end end
# Indicates that some value should be of a specified type. # Indicates that some value should be of a specified type.
# The value's runtime class is checked. # The value's runtime type is checked.
# A type name or type union should be used for *expected*. # A type name or type union should be used for *expected*.
# This method is identical to `#be_an_instance_of`, # This method is identical to `#be_an_instance_of`,
# and exists just to improve grammar. # and exists just to improve grammar.
@ -148,6 +148,19 @@ module Spectator::DSL
be_instance_of({{expected}}) be_instance_of({{expected}})
end end
# Indicates that some value should be of a specified type at compile time.
# The value's compile time type is checked.
# This can test is a variable or value returned by a method is inferred to the expected type.
#
# Examples:
# ```
# value = 42 || "foobar"
# expect(value).to compile_as(Int32 | String)
# ```
macro compile_as(expected)
::Spectator::Matchers::CompiledTypeMatcher({{expected}}).new
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.
# #

View file

@ -0,0 +1,59 @@
require "./matcher"
module Spectator::Matchers
# Matcher that tests a value is of a specified type at compile time.
# The values are compared with the `typeof` method.
# This can be used to inspect the inferred type of methods and variables.
struct CompiledTypeMatcher(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
"compiles as #{Expected}"
end
# Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : Expression(T)) : Bool forall T
Expected == typeof(actual.value)
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} does not compile as #{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} compiles as #{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: typeof(actual.value).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: typeof(actual.value).inspect,
}
end
end
end