mhennemeyer-matchy 0.2.0

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,100 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestDefMatcher < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @obj = Object.new
7
+ end
8
+
9
+ def test_defines_method
10
+ def_matcher :method_ do |given, matcher, args|
11
+ end
12
+ self.should respond_to(:method_)
13
+ end
14
+
15
+ def test_object_responds_to_matches
16
+ def_matcher :method_ do |given, matcher, args|
17
+ end
18
+ method_.should respond_to(:matches?)
19
+ end
20
+
21
+ def test_fail_positive
22
+ def_matcher :matcher do |given, matcher, args|
23
+ false
24
+ end
25
+ lambda {1.should matcher}.should raise_error
26
+ end
27
+
28
+ def test_pass_positive
29
+ def_matcher :matcher do |given, matcher, args|
30
+ true
31
+ end
32
+ 1.should matcher
33
+ end
34
+
35
+ def test_fail_negative
36
+ def_matcher :matcher do |given, matcher, args|
37
+ true
38
+ end
39
+ lambda {1.should_not matcher}.should raise_error
40
+ end
41
+
42
+ def test_pass_negative
43
+ def_matcher :matcher do |given, matcher, args|
44
+ false
45
+ end
46
+ 1.should_not matcher
47
+ end
48
+
49
+ def test_takes_arguments
50
+ def_matcher :matcher do |given, matcher, args|
51
+ $args = args
52
+ true
53
+ end
54
+ @obj.should matcher(1,2,3)
55
+ $args.should eql([1,2,3])
56
+ end
57
+
58
+ def test_received_method
59
+ def_matcher :matcher do |given, matcher, args|
60
+ $msgs = matcher.msgs
61
+ true
62
+ end
63
+ @obj.should matcher.method1
64
+ $msgs[0].name.should eql(:method1)
65
+ end
66
+
67
+ def test_received_method_takes_args
68
+ def_matcher :matcher do |given, matcher, args|
69
+ $msgs = matcher.msgs
70
+ true
71
+ end
72
+ @obj.should matcher.method1(1,2,3)
73
+ $msgs[0].args.should eql([1,2,3])
74
+ end
75
+
76
+ def test_received_method_takes_block
77
+ def_matcher :matcher do |given, matcher, args|
78
+ $msgs = matcher.msgs
79
+ true
80
+ end
81
+ @obj.should matcher.method1 { "Hello, World!"}
82
+ $msgs[0].block.call.should eql("Hello, World!")
83
+ end
84
+
85
+ def test_received_method_chained
86
+ def_matcher :matcher do |given, matcher, args|
87
+ $msgs = matcher.msgs
88
+ true
89
+ end
90
+ @obj.should matcher.method1(1,2,3) { "Hello, World!"}.
91
+ method2(4,5,6) { "Hello chained messages" }
92
+
93
+ $msgs[0].name.should eql(:method1)
94
+ $msgs[1].name.should eql(:method2)
95
+ $msgs[0].args.should eql([1,2,3])
96
+ $msgs[1].args.should eql([4,5,6])
97
+ $msgs[0].block.call.should eql("Hello, World!")
98
+ $msgs[1].block.call.should eql("Hello chained messages")
99
+ end
100
+ 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,144 @@
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
+ def test_regexp_argument_message_fails_wrong_message
140
+ lambda do
141
+ lambda { raise "other message" }.should raise_error(/abc/)
142
+ end.should raise_error(/matching/)
143
+ end
144
+ end
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestMatcherBuilder < Test::Unit::TestCase
4
+ include Matchy::MatcherBuilder
5
+
6
+ def setup
7
+ @obj = Object.new
8
+ end
9
+
10
+ def test_matcher_responds_to_matches
11
+ block = lambda {|given, matcher, args| true}
12
+ build_matcher(:m, &block).should respond_to(:matches?)
13
+ end
14
+
15
+ def test_fail_positive
16
+ block = lambda {|given, matcher, args| false}
17
+ lambda {@obj.should build_matcher(:m, &block)}.should raise_error
18
+ end
19
+
20
+ def test_pass_positive
21
+ block = lambda {|given, matcher, args| true}
22
+ @obj.should build_matcher(:m, &block)
23
+ end
24
+
25
+ def test_fail_negative
26
+ block = lambda {|given, matcher, args| true}
27
+ lambda {@obj.should_not build_matcher(:m, &block)}.should raise_error
28
+ end
29
+
30
+ def test_pass_negative
31
+ block = lambda {|given, matcher, args| false}
32
+ @obj.should_not build_matcher(:m, &block)
33
+ end
34
+
35
+ def test_takes_arguments
36
+ block = lambda {|given, matcher, args| $args = args; true}
37
+ @obj.should build_matcher(:m,[1,2,3], &block)
38
+ $args.should eql([1,2,3])
39
+ end
40
+
41
+ def test_received_method
42
+ block = lambda {|given, matcher, args| $msgs = matcher.msgs; true}
43
+ @obj.should build_matcher(:m, &block).method1
44
+ $msgs[0].name.should eql(:method1)
45
+ end
46
+
47
+ def test_received_method_takes_args
48
+ block = lambda {|given, matcher, args| $msgs = matcher.msgs; true}
49
+ @obj.should build_matcher(:m, &block).method1(1,2,3)
50
+ $msgs[0].args.should eql([1,2,3])
51
+ end
52
+
53
+ def test_received_method_takes_block
54
+ block = lambda {|given, matcher, args| $msgs = matcher.msgs; true}
55
+ @obj.should build_matcher(:m, &block).method1 { "Hello, World!"}
56
+ $msgs[0].block.call.should eql("Hello, World!")
57
+ end
58
+
59
+ def test_received_method_chained
60
+ block = lambda {|given, matcher, args| $msgs = matcher.msgs; true}
61
+ @obj.should build_matcher(:m, &block).method1(1,2,3) { "Hello, World!"}.
62
+ method2(4,5,6) { "Hello chained messages" }
63
+
64
+ $msgs[0].name.should eql(:method1)
65
+ $msgs[1].name.should eql(:method2)
66
+ $msgs[0].args.should eql([1,2,3])
67
+ $msgs[1].args.should eql([4,5,6])
68
+ $msgs[0].block.call.should eql("Hello, World!")
69
+ $msgs[1].block.call.should eql("Hello chained messages")
70
+ end
71
+
72
+ end