Initial structure for "be OP value" syntax

This commit is contained in:
Michael Miller 2019-01-23 17:17:45 -07:00
parent 57c15b9088
commit b93ef1fd2d
2 changed files with 76 additions and 0 deletions

View file

@ -15,6 +15,20 @@ module Spectator::DSL
::Spectator::Matchers::EqualityMatcher.new({{expected.stringify}}, {{expected}})
end
# Indicates that some value when compared to another satisfies an operator.
# An operator should follow, such as: `<`, `<=`, `>`, or `>=`.
#
# Examples:
# ```
# expect(1 + 1).to be > 1
# expect(5).to be >= 3
# ```
#
# See `Spectator::Matchers::BeComparison` for supported operators and methods.
macro be
::Spectator::Matchers::BeComparison.new
end
# Indicates that some value should semantically equal another.
# The `===` operator is used for this check.
# This has identical behavior as a `when` condition in a `case` block.

View file

@ -0,0 +1,62 @@
module Spectator::Matchers
# Proxy type to provide the "be operator" syntax.
# This allows users to write tests like:
# ```
# expect(1 + 1).to be > 1
# ```
struct BeComparison
# Creates a matcher that checks if a value is less than an expected value.
# The spec would look like:
# ```
# expect(0).to be < 1
# ```
def <(other : ActualType) forall ActualType
raise NotImplementedError.new("be <")
end
# Creates a matcher that checks if a value is less than or equal to an expected value.
# The spec would look like:
# ```
# expect(0).to be <= 1
# ```
def <=(other : ActualType) forall ActualType
raise NotImplementedError.new("be <=")
end
# Creates a matcher that checks if a value is greater than an expected value.
# The spec would look like:
# ```
# expect(2).to be > 1
# ```
def >(other : ActualType) forall ActualType
raise NotImplementedError.new("be >")
end
# Creates a matcher that checks if a value is greater than or equal to an expected value.
# The spec would look like:
# ```
# expect(2).to be >= 1
# ```
def >=(other : ActualType) forall ActualType
raise NotImplementedError.new("be >=")
end
# Creates a matcher that checks if a value is equal to an expected value.
# The spec would look like:
# ```
# expect(0).to be == 0
# ```
def ==(other : ActualType) forall ActualType
raise NotImplementedError.new("be ==")
end
# Creates a matcher that checks if a value is not equal to an expected value.
# The spec would look like:
# ```
# expect(0).to be != 1
# ```
def !=(other : ActualType) forall ActualType
raise NotImplementedError.new("be !=")
end
end
end