mcmire-matchy 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,63 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestChangeExpectations < Test::Unit::TestCase
4
+ def test_change
5
+ var = 1
6
+ lambda {var += 1}.should change {var}
7
+ end
8
+
9
+ def test_change_fails
10
+ var = 1
11
+ lambda do
12
+ lambda { }.should change {var}
13
+ end.should raise_error
14
+ end
15
+
16
+ def test_change_by
17
+ var = 1
18
+ lambda {var += 1}.should change {var}.by(1)
19
+ end
20
+
21
+ def test_change_by_fails
22
+ var = 1
23
+ lambda do
24
+ lambda {var += 2}.should change {var}.by(1)
25
+ end.should raise_error
26
+ end
27
+
28
+ def test_change_by_at_least
29
+ var = 1
30
+ lambda {var += 1}.should change {var}.by_at_least(1)
31
+ end
32
+
33
+ def test_change_by_at_least_fails
34
+ var = 1
35
+ lambda do
36
+ lambda {var += 0.9}.should change {var}.by_at_least(1)
37
+ end.should raise_error
38
+ end
39
+
40
+ def test_change_by_at_most
41
+ var = 1
42
+ lambda {var += 1}.should change {var}.by_at_most(1)
43
+ end
44
+
45
+ def test_change_by_at_most_fails
46
+ var = 1
47
+ lambda do
48
+ lambda {var += 1.1}.should change {var}.by_at_most(1)
49
+ end.should raise_error
50
+ end
51
+
52
+ def test_change_from_to
53
+ var = 1
54
+ lambda {var += 1}.should change {var}.from(1).to(2)
55
+ end
56
+
57
+ def test_change_from_to_fails
58
+ var = 1
59
+ lambda do
60
+ lambda {var += 1.1}.should change {var}.from(1).to(2)
61
+ end.should raise_error
62
+ end
63
+ end
@@ -0,0 +1,139 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestCustomMatcher < Test::Unit::TestCase
4
+ class Foo < Struct.new(:valid)
5
+ def valid?
6
+ valid == true
7
+ end
8
+
9
+ def items
10
+ [1,2,3]
11
+ end
12
+ end
13
+
14
+ custom_matcher :matcher_true do |receiver, matcher, args|
15
+ true
16
+ end
17
+
18
+ custom_matcher :matcher_false do |receiver, matcher, args|
19
+ false
20
+ end
21
+
22
+ def test_defines_method
23
+ self.class.class_eval do
24
+ custom_matcher :matcher_method do |receiver, matcher, args|
25
+ true
26
+ end
27
+ end
28
+
29
+ self.should respond_to(:matcher_method)
30
+ end
31
+
32
+ def test_pass_positive
33
+ 1.should matcher_true
34
+ end
35
+
36
+ def test_fail_positive
37
+ lambda {
38
+ 1.should matcher_false
39
+ }.should raise_error
40
+ end
41
+
42
+ def test_pass_negative
43
+ 1.should_not matcher_false
44
+ end
45
+
46
+ def test_fail_negative
47
+ lambda {
48
+ 1.should_not matcher_true
49
+ }.should raise_error
50
+ end
51
+
52
+ def test_arguments
53
+ actual_args = []
54
+
55
+ self.class.class_eval do
56
+ custom_matcher :matcher do |receiver, matcher, args|
57
+ actual_args = args
58
+ true
59
+ end
60
+ end
61
+
62
+ 1.should matcher(1,2,3)
63
+ actual_args.should == [1,2,3]
64
+ end
65
+
66
+ def test_receiver
67
+ actual_receiver = nil
68
+
69
+ self.class.class_eval do
70
+ custom_matcher :matcher do |receiver, matcher, args|
71
+ actual_receiver = receiver
72
+ true
73
+ end
74
+ end
75
+
76
+ receiver = 1
77
+ receiver.should matcher
78
+ actual_receiver.should == receiver
79
+ end
80
+
81
+ def test_calling_receiver_method
82
+ self.class.class_eval do
83
+ custom_matcher :be_nil do |receiver, matcher, args|
84
+ receiver.nil?
85
+ end
86
+ end
87
+
88
+ nil.should be_nil
89
+ lambda {
90
+ nil.should_not be_nil
91
+ }.should raise_error
92
+
93
+ 'foo'.should_not be_nil
94
+ lambda {
95
+ 'foo'.should be_nil
96
+ }.should raise_error
97
+ end
98
+
99
+ def test_matcher
100
+ self.class.class_eval do
101
+ custom_matcher :be_valid do |receiver, matcher, args|
102
+ matcher.positive_failure_message = "Expected to be valid but wasn't"
103
+ matcher.negative_failure_message = "Expected to not be valid but was"
104
+ receiver.valid?
105
+ end
106
+ end
107
+
108
+ foo = Foo.new
109
+
110
+ foo.valid = false
111
+ lambda {
112
+ foo.should be_valid
113
+ }.should raise_error("Expected to be valid but wasn't.")
114
+
115
+ foo.valid = true
116
+ lambda {
117
+ foo.should_not be_valid
118
+ }.should raise_error("Expected to not be valid but was.")
119
+ end
120
+
121
+ def test_matcher_with_chained_messages
122
+ self.class.class_eval do
123
+ custom_matcher :have do |receiver, matcher, args|
124
+ count = args[0]
125
+ something = matcher.chained_messages[0].name
126
+ actual = receiver.send(something).size
127
+ matcher.positive_failure_message = "Expected #{receiver} to have #{actual} #{something}, but found #{count} "
128
+ actual == count
129
+ end
130
+ end
131
+
132
+ foo = Foo.new
133
+ foo.should have(3).items
134
+
135
+ lambda {
136
+ foo.should have(2).items
137
+ }.should raise_error
138
+ end
139
+ end
@@ -0,0 +1,91 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestEnumerableExpectations < Test::Unit::TestCase
4
+ def test_include
5
+ [1,2,3,4].should include(4)
6
+ end
7
+
8
+ def test_include_fail
9
+ lambda {
10
+ [1,2,3,4].should include(6)
11
+ }.should raise_error(Test::Unit::AssertionFailedError)
12
+ end
13
+
14
+ def test_exclude
15
+ [1,2,3,4].should exclude(9)
16
+ end
17
+
18
+ def test_exclude_fail
19
+ lambda {
20
+ [1,2,3,4].should exclude(4)
21
+ }.should raise_error(Test::Unit::AssertionFailedError)
22
+ end
23
+
24
+ def test_multi_include
25
+ [1,2,3,4].should include(1,2)
26
+ end
27
+
28
+ def test_multi_include_fail
29
+ lambda {
30
+ [1,2,3,4].should include(6,7,8)
31
+ }.should raise_error(Test::Unit::AssertionFailedError)
32
+ end
33
+
34
+ def test_multi_exclude
35
+ [1,2,3,4].should exclude(13,14)
36
+ end
37
+
38
+ def test_multi_exclude_fail
39
+ lambda {
40
+ [1,2,3,4].should exclude(2,3,4)
41
+ }.should raise_error(Test::Unit::AssertionFailedError)
42
+ end
43
+
44
+ def test_negative_include
45
+ [1,2,3,4].should_not include(9)
46
+ end
47
+
48
+ def test_negative_include_fail
49
+ lambda {
50
+ [1,2,3,4].should_not include(4)
51
+ }.should raise_error(Test::Unit::AssertionFailedError)
52
+ end
53
+
54
+ def test_negative_exclude
55
+ [1,2,3,4].should_not exclude(3)
56
+ end
57
+
58
+ def test_negative_exclude_fail
59
+ lambda {
60
+ [1,2,3,4].should_not exclude(6,7)
61
+ }.should raise_error(Test::Unit::AssertionFailedError)
62
+ end
63
+
64
+ def test_include_fail_message
65
+ obj = include(1)
66
+ obj.matches?([4,5,6])
67
+
68
+ obj.failure_message.should be("Expected [4, 5, 6] to include [1].")
69
+ end
70
+
71
+ def test_include_negative_fail_message
72
+ obj = include(1)
73
+ obj.matches?([4,5,6])
74
+
75
+ obj.negative_failure_message.should be("Expected [4, 5, 6] to not include [1].")
76
+ end
77
+
78
+ def test_exclude_fail_message
79
+ obj = exclude(4)
80
+ obj.matches?([4,5,6])
81
+
82
+ obj.failure_message.should be("Expected [4, 5, 6] to exclude [4].")
83
+ end
84
+
85
+ def test_exclude_negative_fail_message
86
+ obj = exclude(4)
87
+ obj.matches?([4,5,6])
88
+
89
+ obj.negative_failure_message.should be("Expected [4, 5, 6] to not exclude [4].")
90
+ end
91
+ end
@@ -0,0 +1,156 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestErrorExpectations < Test::Unit::TestCase
4
+ def test_raises_error
5
+ lambda { raise "FAIL" }.should raise_error
6
+ end
7
+
8
+ def test_raises_error_fail
9
+ lambda {
10
+ lambda { "WIN" }.should raise_error
11
+ }.should raise_error(Test::Unit::AssertionFailedError)
12
+ end
13
+
14
+ def test_negative_raises_error
15
+ lambda { "WIN" }.should_not raise_error
16
+ end
17
+
18
+ def test_negative_raises_error_fail
19
+ lambda {
20
+ lambda { raise "FAIL" }.should_not raise_error
21
+ }.should raise_error(Test::Unit::AssertionFailedError)
22
+ end
23
+
24
+ def test_raises_specific_error
25
+ lambda { raise TypeError }.should raise_error(TypeError)
26
+ end
27
+
28
+ def test_raises_specific_error_fail_with_no_error
29
+ lambda {
30
+ lambda { "WIN" }.should raise_error(TypeError)
31
+ }.should raise_error(Test::Unit::AssertionFailedError)
32
+ end
33
+
34
+ def test_raises_specific_error_fail_with_different_error
35
+ lambda {
36
+ lambda { raise StandardError }.should raise_error(TypeError)
37
+ }.should raise_error(Test::Unit::AssertionFailedError)
38
+ end
39
+
40
+ def test_throws_symbol
41
+ lambda {
42
+ throw :win
43
+ }.should throw_symbol(:win)
44
+ end
45
+
46
+ def test_throws_symbol_fails_with_different_symbol
47
+ lambda {
48
+ lambda {
49
+ throw :fail
50
+ }.should throw_symbol(:win)
51
+ }.should raise_error(Test::Unit::AssertionFailedError)
52
+ end
53
+
54
+ def test_negative_throws_symbol
55
+ lambda {
56
+ "not this time!"
57
+ }.should_not throw_symbol(:win)
58
+ end
59
+
60
+ def test_negative_throws_symbol_fails_with_different_symbol
61
+
62
+ lambda{
63
+ lambda {
64
+ throw :fail
65
+ }.should_not throw_symbol(:fail)
66
+ }.should raise_error(Test::Unit::AssertionFailedError)
67
+
68
+ end
69
+
70
+ def test_error_fail_message
71
+ obj = raise_error(TypeError)
72
+ obj.matches?(lambda { raise NameError })
73
+
74
+ obj.failure_message.should =~ /Expected #<(.*)> to raise TypeError, but NameError was raised instead./
75
+ end
76
+
77
+ def test_error_fail_message_when_no_error
78
+ obj = raise_error(TypeError)
79
+ obj.matches?(lambda { "moop" })
80
+
81
+ obj.failure_message.should =~ /Expected #<(.*)> to raise TypeError, but none was raised./
82
+ end
83
+
84
+ def test_error_negative_fail_message
85
+ obj = raise_error(TypeError)
86
+ obj.matches?(lambda { raise TypeError })
87
+
88
+ obj.negative_failure_message.should =~ /Expected #<(.*)> to not raise TypeError./
89
+ end
90
+
91
+ def test_throw_fail_message
92
+ obj = throw_symbol(:fail)
93
+ obj.matches?(lambda { throw :lame })
94
+
95
+ obj.failure_message.should =~ /Expected #<(.*)> to throw :fail, but :lame was thrown instead./
96
+ end
97
+
98
+ def test_throw_fail_message_when_no_symbol
99
+ obj = throw_symbol(:fail)
100
+ obj.matches?(lambda { "moop" })
101
+
102
+ obj.failure_message.should =~ /Expected #<(.*)> to throw :fail, but no symbol was thrown./
103
+ end
104
+
105
+ def test_throw_negative_fail_message
106
+ obj = throw_symbol(:fail)
107
+ obj.matches?(lambda { throw :fail })
108
+
109
+ obj.negative_failure_message.should =~ /Expected #<(.*)> to not throw :fail./
110
+ end
111
+
112
+ def test_string_argument_message
113
+ lambda {raise "message"}.should raise_error("message")
114
+ end
115
+
116
+ def test_string_argument_message_fails_no_error
117
+ lambda do
118
+ lambda { 1 }.should raise_error("message")
119
+
120
+ end.should raise_error
121
+ end
122
+
123
+ def test_string_argument_message_fails_wrong_message
124
+ lambda do
125
+ lambda { raise "other message" }.should raise_error("message")
126
+ end.should raise_error
127
+ end
128
+
129
+ def test_regexp_argument_message
130
+ lambda {raise "message"}.should raise_error(/essa/)
131
+ end
132
+
133
+ def test_regexp_argument_message_fails_no_error
134
+ lambda do
135
+ lambda { 1 }.should raise_error(/essa/)
136
+ end.should raise_error
137
+ end
138
+
139
+ class MyCustomException < StandardError; end
140
+
141
+ def test_error_and_message
142
+ lambda { raise MyCustomException, "other message" }.should raise_error(MyCustomException, /^other/)
143
+ end
144
+
145
+ def test_error_and_wrong_message
146
+ lambda do
147
+ lambda { raise MyCustomException, "other message" }.should raise_error(MyCustomException, /^not/)
148
+ end.should raise_error(/matching/)
149
+ end
150
+
151
+ def test_regexp_argument_message_fails_wrong_message
152
+ lambda do
153
+ lambda { raise "other message" }.should raise_error(/abc/)
154
+ end.should raise_error(/matching/)
155
+ end
156
+ end