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
|
@ -1,102 +0,0 @@
|
||||||
require "../spec_helper"
|
|
||||||
|
|
||||||
# This is a terrible hack,
|
|
||||||
# but I don't want to expose `ValueMatcher#expected` publicly
|
|
||||||
# just for this spec.
|
|
||||||
module Spectator::Matchers
|
|
||||||
struct ValueMatcher(ExpectedType)
|
|
||||||
def expected_value
|
|
||||||
expected
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def be_comparison
|
|
||||||
Spectator::Matchers::BeComparison.new
|
|
||||||
end
|
|
||||||
|
|
||||||
describe Spectator::Matchers::BeComparison do
|
|
||||||
describe "#<" do
|
|
||||||
it "returns a LessThanMatcher" do
|
|
||||||
value = 0
|
|
||||||
matcher = be_comparison < value
|
|
||||||
matcher.should be_a(Spectator::Matchers::LessThanMatcher(typeof(value)))
|
|
||||||
end
|
|
||||||
|
|
||||||
it "passes along the expected value" do
|
|
||||||
value = 42
|
|
||||||
matcher = be_comparison < value
|
|
||||||
matcher.expected_value.should eq(value)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#<=" do
|
|
||||||
it "returns a LessThanEqualMatcher" do
|
|
||||||
value = 0
|
|
||||||
matcher = be_comparison <= value
|
|
||||||
matcher.should be_a(Spectator::Matchers::LessThanEqualMatcher(typeof(value)))
|
|
||||||
end
|
|
||||||
|
|
||||||
it "passes along the expected value" do
|
|
||||||
value = 42
|
|
||||||
matcher = be_comparison <= value
|
|
||||||
matcher.expected_value.should eq(value)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#>" do
|
|
||||||
it "returns a GreaterThanMatcher" do
|
|
||||||
value = 0
|
|
||||||
matcher = be_comparison > value
|
|
||||||
matcher.should be_a(Spectator::Matchers::GreaterThanMatcher(typeof(value)))
|
|
||||||
end
|
|
||||||
|
|
||||||
it "passes along the expected value" do
|
|
||||||
value = 42
|
|
||||||
matcher = be_comparison > value
|
|
||||||
matcher.expected_value.should eq(value)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#>=" do
|
|
||||||
it "returns a GreaterThanEqualMatcher" do
|
|
||||||
value = 0
|
|
||||||
matcher = be_comparison >= value
|
|
||||||
matcher.should be_a(Spectator::Matchers::GreaterThanEqualMatcher(typeof(value)))
|
|
||||||
end
|
|
||||||
|
|
||||||
it "passes along the expected value" do
|
|
||||||
value = 42
|
|
||||||
matcher = be_comparison >= value
|
|
||||||
matcher.expected_value.should eq(value)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#==" do
|
|
||||||
it "returns an EqualityMatcher" do
|
|
||||||
value = 0
|
|
||||||
matcher = be_comparison == value
|
|
||||||
matcher.should be_a(Spectator::Matchers::EqualityMatcher(typeof(value)))
|
|
||||||
end
|
|
||||||
|
|
||||||
it "passes along the expected value" do
|
|
||||||
value = 42
|
|
||||||
matcher = be_comparison == value
|
|
||||||
matcher.expected_value.should eq(value)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#!=" do
|
|
||||||
it "returns an InequalityMatcher" do
|
|
||||||
value = 0
|
|
||||||
matcher = be_comparison != value
|
|
||||||
matcher.should be_a(Spectator::Matchers::InequalityMatcher(typeof(value)))
|
|
||||||
end
|
|
||||||
|
|
||||||
it "passes along the expected value" do
|
|
||||||
value = 42
|
|
||||||
matcher = be_comparison != value
|
|
||||||
matcher.expected_value.should eq(value)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,5 +1,20 @@
|
||||||
require "../spec_helper"
|
require "../spec_helper"
|
||||||
|
|
||||||
|
# This is a terrible hack,
|
||||||
|
# but I don't want to expose `ValueMatcher#expected` publicly
|
||||||
|
# just for this spec.
|
||||||
|
module Spectator::Matchers
|
||||||
|
struct ValueMatcher(ExpectedType)
|
||||||
|
def expected_value
|
||||||
|
expected
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def be_comparison
|
||||||
|
Spectator::Matchers::TruthyMatcher.new(true)
|
||||||
|
end
|
||||||
|
|
||||||
describe Spectator::Matchers::TruthyMatcher do
|
describe Spectator::Matchers::TruthyMatcher do
|
||||||
context "truthy" do
|
context "truthy" do
|
||||||
describe "#match?" do
|
describe "#match?" do
|
||||||
|
@ -134,4 +149,88 @@ describe Spectator::Matchers::TruthyMatcher do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#<" do
|
||||||
|
it "returns a LessThanMatcher" do
|
||||||
|
value = 0
|
||||||
|
matcher = be_comparison < value
|
||||||
|
matcher.should be_a(Spectator::Matchers::LessThanMatcher(typeof(value)))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "passes along the expected value" do
|
||||||
|
value = 42
|
||||||
|
matcher = be_comparison < value
|
||||||
|
matcher.expected_value.should eq(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#<=" do
|
||||||
|
it "returns a LessThanEqualMatcher" do
|
||||||
|
value = 0
|
||||||
|
matcher = be_comparison <= value
|
||||||
|
matcher.should be_a(Spectator::Matchers::LessThanEqualMatcher(typeof(value)))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "passes along the expected value" do
|
||||||
|
value = 42
|
||||||
|
matcher = be_comparison <= value
|
||||||
|
matcher.expected_value.should eq(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#>" do
|
||||||
|
it "returns a GreaterThanMatcher" do
|
||||||
|
value = 0
|
||||||
|
matcher = be_comparison > value
|
||||||
|
matcher.should be_a(Spectator::Matchers::GreaterThanMatcher(typeof(value)))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "passes along the expected value" do
|
||||||
|
value = 42
|
||||||
|
matcher = be_comparison > value
|
||||||
|
matcher.expected_value.should eq(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#>=" do
|
||||||
|
it "returns a GreaterThanEqualMatcher" do
|
||||||
|
value = 0
|
||||||
|
matcher = be_comparison >= value
|
||||||
|
matcher.should be_a(Spectator::Matchers::GreaterThanEqualMatcher(typeof(value)))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "passes along the expected value" do
|
||||||
|
value = 42
|
||||||
|
matcher = be_comparison >= value
|
||||||
|
matcher.expected_value.should eq(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#==" do
|
||||||
|
it "returns an EqualityMatcher" do
|
||||||
|
value = 0
|
||||||
|
matcher = be_comparison == value
|
||||||
|
matcher.should be_a(Spectator::Matchers::EqualityMatcher(typeof(value)))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "passes along the expected value" do
|
||||||
|
value = 42
|
||||||
|
matcher = be_comparison == value
|
||||||
|
matcher.expected_value.should eq(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#!=" do
|
||||||
|
it "returns an InequalityMatcher" do
|
||||||
|
value = 0
|
||||||
|
matcher = be_comparison != value
|
||||||
|
matcher.should be_a(Spectator::Matchers::InequalityMatcher(typeof(value)))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "passes along the expected value" do
|
||||||
|
value = 42
|
||||||
|
matcher = be_comparison != value
|
||||||
|
matcher.expected_value.should eq(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -28,7 +28,7 @@ module Spectator::DSL
|
||||||
end
|
end
|
||||||
|
|
||||||
# Indicates that some value when compared to another satisfies an operator.
|
# 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:
|
# Examples:
|
||||||
# ```
|
# ```
|
||||||
|
@ -36,9 +36,14 @@ module Spectator::DSL
|
||||||
# expect(5).to be >= 3
|
# 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
|
macro be
|
||||||
::Spectator::Matchers::BeComparison.new
|
::Spectator::Matchers::TruthyMatcher.new(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Indicates that some value should semantically equal another.
|
# 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,
|
# Falsey means a value is considered false by an if-statement,
|
||||||
# which are `false` and `nil` in Crystal.
|
# which are `false` and `nil` in Crystal.
|
||||||
# Truthy is the opposite of falsey.
|
# Truthy is the opposite of falsey.
|
||||||
|
#
|
||||||
|
# Additionally, different matchers can be created
|
||||||
|
# by using the `#<`, `#<=`, `#>`, `#>=`, `#==`, and `#!=` operators.
|
||||||
struct TruthyMatcher < ValueMatcher(Bool)
|
struct TruthyMatcher < ValueMatcher(Bool)
|
||||||
# Creates the truthy matcher.
|
# Creates the truthy matcher.
|
||||||
# The `truthy` argument should be true to match "truthy" values,
|
# 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
|
def negated_message(partial : Expectations::ValueExpectationPartial(ActualType)) : String forall ActualType
|
||||||
"Expected #{partial.label} to not be #{label}"
|
"Expected #{partial.label} to not be #{label}"
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue