Skip to content
This repository
Browse code

Add Matchers module with MatchAny and MatchAll matchers.

Also add Object#>< and Object#<> as short-hand methods for creating matchers.
  • Loading branch information...
commit 337d95c70a54272a70b1ec3d8fa9852433f7c0de 1 parent aa38b96
Christopher Bertels authored June 18, 2012
3  lib/boot.fy
@@ -51,4 +51,5 @@ require: "system"
51 51
 
52 52
 require: "documentation"
53 53
 require: "package"
54  
-require: "contracts"
  54
+require: "contracts"
  55
+require: "matchers"
79  lib/matchers.fy
... ...
@@ -0,0 +1,79 @@
  1
+class Matchers {
  2
+  class MatchAny {
  3
+    """
  4
+    MatchAny matcher. Matches any of the 2 values associated with it.
  5
+
  6
+    Example:
  7
+          m = MatchAny new: 1 with: 2
  8
+          m === 1  # => true
  9
+          m === 2  # => true
  10
+
  11
+    There's also a shorthand method defined with @Object#><@:
  12
+
  13
+    Example:
  14
+          m = 1 >< 2
  15
+          m === 1  # => true
  16
+          m === 2  # => true
  17
+    """
  18
+
  19
+    read_slots: ('a, 'b)
  20
+    def initialize: @a with: @b
  21
+
  22
+    def === object {
  23
+      """
  24
+      @other Object to match against.
  25
+      @return @true if @objects matches either @a or @b in @self.
  26
+      """
  27
+
  28
+      if: (@a === object) then: @{ return } else: {
  29
+        @b === object
  30
+      }
  31
+    }
  32
+
  33
+    expose_to_ruby: '===
  34
+
  35
+    def == other {
  36
+      match other {
  37
+        case MatchAny -> @a == (other a) && { @b == (other b)}
  38
+        case _ -> false
  39
+      }
  40
+    }
  41
+  }
  42
+
  43
+  class MatchAll {
  44
+    """
  45
+    MatchAll matcher. Matches only if all of the 2 values associated with it match a given value.
  46
+
  47
+    Example:
  48
+          m = MatchAll new: Array with: Enumerable
  49
+          m === \"foo\"  # => false
  50
+          m === [1,2,3]  # => true
  51
+
  52
+    There's also a shorthand method defined with @Object#<>@:
  53
+
  54
+    Example:
  55
+          (Array <> Enumerable) === [1,2,3]  # => true
  56
+    """
  57
+
  58
+    read_slots: ('a, 'b)
  59
+    def initialize: @a with: @b
  60
+
  61
+    def === object {
  62
+      """
  63
+      @object Object to match against.
  64
+      @return @true if it matches both @a and @b in @self.
  65
+      """
  66
+
  67
+      if: (@a === object) then: { @b === object }
  68
+    }
  69
+
  70
+    expose_to_ruby: '===
  71
+
  72
+    def == other {
  73
+      match other {
  74
+        case MatchAll -> @a == (other a) && { @b == (other b)}
  75
+        case _ -> nil
  76
+      }
  77
+    }
  78
+  }
  79
+}
20  lib/object.fy
@@ -749,4 +749,24 @@ class Object {
749 749
 
750 750
     methods - fancy_methods
751 751
   }
  752
+
  753
+  def >< other {
  754
+    """
  755
+    @other Other @Object@ to create a @MatchAny@ matcher with.
  756
+
  757
+    Shorthand for: `MatchAny new: self with: other`
  758
+    """
  759
+
  760
+    Matchers MatchAny new: self with: other
  761
+  }
  762
+
  763
+  def <> other {
  764
+    """
  765
+    @other Other @Object@ to create a @MatchAll@ matcher with.
  766
+
  767
+    Shorthand for: `MatchAll new: self with: other`
  768
+    """
  769
+
  770
+    Matchers MatchAll new: self with: other
  771
+  }
752 772
 }
18  tests/matchers.fy
... ...
@@ -0,0 +1,18 @@
  1
+FancySpec describe: Matchers with: {
  2
+  it: "matches with any value defined matching" when: {
  3
+    m = Matchers MatchAny new: 1 with: 2
  4
+    m === 0 is: false
  5
+    m === 1 is: true
  6
+    m === 2 is: true
  7
+    m === 3 is_not: true
  8
+  }
  9
+
  10
+  it: "matches only with all values defined matching" when: {
  11
+    m = Matchers MatchAll new: Integer with: Fixnum
  12
+    m === -1 is: true
  13
+    m === 0 is: true
  14
+    m === 1 is: true
  15
+    m === 2.1 is_not: true
  16
+    m === 0.0 is_not: true
  17
+  }
  18
+}

0 notes on commit 337d95c

Please sign in to comment.
Something went wrong with that request. Please try again.