File: test_custom_matcher.rb

package info (click to toggle)
ruby-jnunemaker-matchy 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312 kB
  • sloc: ruby: 2,939; makefile: 2
file content (139 lines) | stat: -rw-r--r-- 2,987 bytes parent folder | download | duplicates (3)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require File.dirname(__FILE__) + '/test_helper.rb'

class TestCustomMatcher < Test::Unit::TestCase
  class Foo < Struct.new(:valid)
    def valid?
      valid == true
    end
    
    def items
      [1,2,3]
    end
  end
  
  custom_matcher :matcher_true do |receiver, matcher, args|
    true
  end
  
  custom_matcher :matcher_false do |receiver, matcher, args|
    false
  end
  
  def test_defines_method
    self.class.class_eval do
      custom_matcher :matcher_method do |receiver, matcher, args|
        true
      end
    end
    
    self.should respond_to(:matcher_method)
  end
  
  def test_pass_positive
    1.should matcher_true
  end
  
  def test_fail_positive
    lambda {
      1.should matcher_false
    }.should raise_error
  end
  
  def test_pass_negative
    1.should_not matcher_false
  end
  
  def test_fail_negative
    lambda {
      1.should_not matcher_true
    }.should raise_error
  end
  
  def test_arguments
    actual_args = []
    
    self.class.class_eval do
      custom_matcher :matcher do |receiver, matcher, args|
        actual_args = args
        true
      end
    end
    
    1.should matcher(1,2,3)
    actual_args.should == [1,2,3]
  end
  
  def test_receiver
    actual_receiver = nil
    
    self.class.class_eval do
      custom_matcher :matcher do |receiver, matcher, args|
        actual_receiver = receiver
        true
      end
    end
    
    receiver = 1
    receiver.should matcher
    actual_receiver.should == receiver
  end
  
  def test_calling_receiver_method
    self.class.class_eval do
      custom_matcher :be_nil do |receiver, matcher, args|
        receiver.nil?
      end
    end
    
    nil.should be_nil
    lambda {
      nil.should_not be_nil
    }.should raise_error
    
    'foo'.should_not be_nil
    lambda {
      'foo'.should be_nil
    }.should raise_error
  end
  
  def test_matcher
    self.class.class_eval do
      custom_matcher :be_valid do |receiver, matcher, args|
        matcher.positive_failure_message = "Expected to be valid but wasn't"
        matcher.negative_failure_message = "Expected to not be valid but was"
        receiver.valid?
      end
    end
    
    foo = Foo.new
    
    foo.valid = false
    lambda {
      foo.should be_valid
    }.should raise_error("Expected to be valid but wasn't.")
    
    foo.valid = true
    lambda {
      foo.should_not be_valid
    }.should raise_error("Expected to not be valid but was.")
  end
  
  def test_matcher_with_chained_messages
    self.class.class_eval do
      custom_matcher :have do |receiver, matcher, args|  
  		  count = args[0]
  		  something = matcher.chained_messages[0].name
  		  actual = receiver.send(something).size
  		  matcher.positive_failure_message = "Expected #{receiver} to have #{actual} #{something}, but found #{count} "
  		  actual == count
  		end
    end
    
    foo = Foo.new
    foo.should have(3).items
    
    lambda {
      foo.should have(2).items
    }.should raise_error
  end
end