Move everything in BeComparison to TruthyMatcher

This is to match RSpec's ability to use "be" by itself.
For instance: `expect(foo).to be`
This commit is contained in:
Michael Miller 2019-01-23 22:10:03 -07:00
parent 28c13cd175
commit c31557e8ff
5 changed files with 164 additions and 167 deletions

View file

@ -28,7 +28,7 @@ module Spectator::DSL
end
# Indicates that some value when compared to another satisfies an operator.
# An operator should follow, such as: `<`, `<=`, `>`, or `>=`.
# An operator can follow, such as: `<`, `<=`, `>`, or `>=`.
#
# Examples:
# ```
@ -36,9 +36,14 @@ module Spectator::DSL
# expect(5).to be >= 3
# ```
#
# See `Spectator::Matchers::BeComparison` for supported operators and methods.
# Additionally, a value can just "be" truthy by omitting an operator.
# ```
# expect("foo").to be
# # is the same as:
# expect("foo").to be_truthy
# ```
macro be
::Spectator::Matchers::BeComparison.new
::Spectator::Matchers::TruthyMatcher.new(true)
end
# Indicates that some value should semantically equal another.

View file

@ -1,62 +0,0 @@
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 <(expected : ExpectedType) forall ExpectedType
LessThanMatcher.new(expected)
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 <=(expected : ExpectedType) forall ExpectedType
LessThanEqualMatcher.new(expected)
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 >(expected : ExpectedType) forall ExpectedType
GreaterThanMatcher.new(expected)
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 >=(expected : ExpectedType) forall ExpectedType
GreaterThanEqualMatcher.new(expected)
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 ==(expected : ExpectedType) forall ExpectedType
EqualityMatcher.new(expected)
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 !=(expected : ExpectedType) forall ExpectedType
InequalityMatcher.new(expected)
end
end
end

View file

@ -5,6 +5,9 @@ module Spectator::Matchers
# Falsey means a value is considered false by an if-statement,
# which are `false` and `nil` in Crystal.
# Truthy is the opposite of falsey.
#
# Additionally, different matchers can be created
# by using the `#<`, `#<=`, `#>`, `#>=`, `#==`, and `#!=` operators.
struct TruthyMatcher < ValueMatcher(Bool)
# Creates the truthy matcher.
# The `truthy` argument should be true to match "truthy" values,
@ -31,5 +34,59 @@ module Spectator::Matchers
def negated_message(partial : Expectations::ValueExpectationPartial(ActualType)) : String forall ActualType
"Expected #{partial.label} to not be #{label}"
end
# 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 <(expected : ExpectedType) forall ExpectedType
LessThanMatcher.new(expected)
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 <=(expected : ExpectedType) forall ExpectedType
LessThanEqualMatcher.new(expected)
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 >(expected : ExpectedType) forall ExpectedType
GreaterThanMatcher.new(expected)
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 >=(expected : ExpectedType) forall ExpectedType
GreaterThanEqualMatcher.new(expected)
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 ==(expected : ExpectedType) forall ExpectedType
EqualityMatcher.new(expected)
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 !=(expected : ExpectedType) forall ExpectedType
InequalityMatcher.new(expected)
end
end
end