jastix (owner)

Revisions

gist: 159109 Download_button fork
public
Public Clone URL: git://gist.github.com/159109.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require "rubygems"
require "spec"
 
module MatcherComposition
  include Spec::Matchers
  def +(other_matcher)
    # Build a new simple matcher that combines self and the other matcher
    simple_matcher("#{description} and #{other_matcher.description}") do |given, matcher|
      matcher.negative_failure_message = ""
 
      # Check both matchers. On the first failure, set the combined matcher's
      # failure_message to the failure_message of the matcher that just failed
      [self, other_matcher].all? do |m|
        success = m.matches?(given)
        matcher.negative_failure_message << "\n#{m.negative_failure_message}"
        unless success
          matcher.failure_message = m.failure_message
          next false
        end
        true
      end
    end
  end
end
 
class Spec::Matchers::SimpleMatcher
  include MatcherComposition
end
 
def be_multiple_of_2
  simple_matcher("be multiple of 2") do |given, matcher|
    matcher.failure_message = "expected #{given} to be a multiple of 2, but it wasn't"
    matcher.negative_failure_message = "expected #{given} to NOT be a multiple of 2, but it was"
    given % 2 == 0
  end
end
 
def be_multiple_of_5
  simple_matcher("be multiple of 5") do |given, matcher|
    matcher.failure_message = "expected #{given} to be a multiple of 5, but it wasn't"
    matcher.negative_failure_message = "expected #{given} to NOT be a multiple of 5, but it was"
    given % 5 == 0
  end
end
 
def be_multiple_of_7
  simple_matcher("be multiple of 7") do |given, matcher|
    matcher.failure_message = "expected #{given} to be a multiple of 7, but it wasn't"
    matcher.negative_failure_message = "expected #{given} to NOT be a multiple of 7, but it was"
    given % 7 == 0
  end
end
 
def be_multiple_of_10
  be_multiple_of_2 + be_multiple_of_5
end
 
def be_multiple_of_70
  be_multiple_of_2 + be_multiple_of_10 + be_multiple_of_7
end
 
describe "awesomeness" do
  it "works" do
    20.should be_multiple_of_10
  end
  
  it "fails" do
    5.should be_multiple_of_10
  end
  
  it "negative fails" do
    20.should_not be_multiple_of_10
  end
  
  it "combines 3" do
    70.should be_multiple_of_70
  end
  
  it "collects all failure messages" do
    10.should be_multiple_of_70
  end
 
  it "collects all negative failure messages" do
    140.should_not be_multiple_of_70
  end
 
  it "works inline as well" do
    10.should be_multiple_of_2 + be_multiple_of_5
  end
end