diff --git a/src/spectator/dsl/matcher_dsl.cr b/src/spectator/dsl/matcher_dsl.cr index 94db68c..ef2a4e2 100644 --- a/src/spectator/dsl/matcher_dsl.cr +++ b/src/spectator/dsl/matcher_dsl.cr @@ -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. diff --git a/src/spectator/matchers/be_comparison.cr b/src/spectator/matchers/be_comparison.cr new file mode 100644 index 0000000..aa0dac5 --- /dev/null +++ b/src/spectator/matchers/be_comparison.cr @@ -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