mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
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:
parent
28c13cd175
commit
c31557e8ff
5 changed files with 164 additions and 167 deletions
|
@ -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.
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue