Permalink
Browse files

Add Matchers module with MatchAny and MatchAll matchers.

Also add Object#>< and Object#<> as short-hand methods for creating matchers.
  • Loading branch information...
bakkdoor committed Jun 18, 2012
1 parent aa38b96 commit 337d95c70a54272a70b1ec3d8fa9852433f7c0de
Showing with 119 additions and 1 deletion.
  1. +2 −1 lib/boot.fy
  2. +79 −0 lib/matchers.fy
  3. +20 −0 lib/object.fy
  4. +18 −0 tests/matchers.fy
View
@@ -51,4 +51,5 @@ require: "system"
require: "documentation"
require: "package"
-require: "contracts"
+require: "contracts"
+require: "matchers"
View
@@ -0,0 +1,79 @@
+class Matchers {
+ class MatchAny {
+ """
+ MatchAny matcher. Matches any of the 2 values associated with it.
+
+ Example:
+ m = MatchAny new: 1 with: 2
+ m === 1 # => true
+ m === 2 # => true
+
+ There's also a shorthand method defined with @Object#><@:
+
+ Example:
+ m = 1 >< 2
+ m === 1 # => true
+ m === 2 # => true
+ """
+
+ read_slots: ('a, 'b)
+ def initialize: @a with: @b
+
+ def === object {
+ """
+ @other Object to match against.
+ @return @true if @objects matches either @a or @b in @self.
+ """
+
+ if: (@a === object) then: @{ return } else: {
+ @b === object
+ }
+ }
+
+ expose_to_ruby: '===
+
+ def == other {
+ match other {
+ case MatchAny -> @a == (other a) && { @b == (other b)}
+ case _ -> false
+ }
+ }
+ }
+
+ class MatchAll {
+ """
+ MatchAll matcher. Matches only if all of the 2 values associated with it match a given value.
+
+ Example:
+ m = MatchAll new: Array with: Enumerable
+ m === \"foo\" # => false
+ m === [1,2,3] # => true
+
+ There's also a shorthand method defined with @Object#<>@:
+
+ Example:
+ (Array <> Enumerable) === [1,2,3] # => true
+ """
+
+ read_slots: ('a, 'b)
+ def initialize: @a with: @b
+
+ def === object {
+ """
+ @object Object to match against.
+ @return @true if it matches both @a and @b in @self.
+ """
+
+ if: (@a === object) then: { @b === object }
+ }
+
+ expose_to_ruby: '===
+
+ def == other {
+ match other {
+ case MatchAll -> @a == (other a) && { @b == (other b)}
+ case _ -> nil
+ }
+ }
+ }
+}
View
@@ -749,4 +749,24 @@ class Object {
methods - fancy_methods
}
+
+ def >< other {
+ """
+ @other Other @Object@ to create a @MatchAny@ matcher with.
+
+ Shorthand for: `MatchAny new: self with: other`
+ """
+
+ Matchers MatchAny new: self with: other
+ }
+
+ def <> other {
+ """
+ @other Other @Object@ to create a @MatchAll@ matcher with.
+
+ Shorthand for: `MatchAll new: self with: other`
+ """
+
+ Matchers MatchAll new: self with: other
+ }
}
View
@@ -0,0 +1,18 @@
+FancySpec describe: Matchers with: {
+ it: "matches with any value defined matching" when: {
+ m = Matchers MatchAny new: 1 with: 2
+ m === 0 is: false
+ m === 1 is: true
+ m === 2 is: true
+ m === 3 is_not: true
+ }
+
+ it: "matches only with all values defined matching" when: {
+ m = Matchers MatchAll new: Integer with: Fixnum
+ m === -1 is: true
+ m === 0 is: true
+ m === 1 is: true
+ m === 2.1 is_not: true
+ m === 0.0 is_not: true
+ }
+}

0 comments on commit 337d95c

Please sign in to comment.