diff --git a/spec/matchers/be_comparison_spec.cr b/spec/matchers/be_comparison_spec.cr deleted file mode 100644 index 81e88f7..0000000 --- a/spec/matchers/be_comparison_spec.cr +++ /dev/null @@ -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 diff --git a/spec/matchers/truthy_matcher_spec.cr b/spec/matchers/truthy_matcher_spec.cr index edfc882..710adc1 100644 --- a/spec/matchers/truthy_matcher_spec.cr +++ b/spec/matchers/truthy_matcher_spec.cr @@ -1,5 +1,20 @@ 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 context "truthy" do describe "#match?" do @@ -134,4 +149,88 @@ describe Spectator::Matchers::TruthyMatcher do 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 diff --git a/src/spectator/dsl/matcher_dsl.cr b/src/spectator/dsl/matcher_dsl.cr index b63877f..59da80f 100644 --- a/src/spectator/dsl/matcher_dsl.cr +++ b/src/spectator/dsl/matcher_dsl.cr @@ -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. diff --git a/src/spectator/matchers/be_comparison.cr b/src/spectator/matchers/be_comparison.cr deleted file mode 100644 index c715ef1..0000000 --- a/src/spectator/matchers/be_comparison.cr +++ /dev/null @@ -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 diff --git a/src/spectator/matchers/truthy_matcher.cr b/src/spectator/matchers/truthy_matcher.cr index fedc9b8..79d9308 100644 --- a/src/spectator/matchers/truthy_matcher.cr +++ b/src/spectator/matchers/truthy_matcher.cr @@ -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