jeremymcanally-matchy 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,11 @@
1
1
  module Matchy
2
2
  module Expectations
3
3
  class RaiseErrorExpectation < Base
4
+ def initialize(expected, test_case)
5
+ @error = nil
6
+ super
7
+ end
8
+
4
9
  def matches?(receiver)
5
10
  @receiver = receiver
6
11
  begin
@@ -31,6 +36,11 @@ module Matchy
31
36
  end
32
37
 
33
38
  class ThrowSymbolExpectation < Base
39
+ def initialize(expected, test_case)
40
+ @thrown_symbol = nil
41
+ super
42
+ end
43
+
34
44
  def matches?(receiver)
35
45
  @receiver = receiver
36
46
  begin
@@ -82,7 +82,7 @@ module Matchy
82
82
  end
83
83
 
84
84
  def fail!(operator)
85
- flunk failure_message(@match ? failure_message(operator) : negative_failure_message(operator))
85
+ flunk @match ? failure_message(operator) : negative_failure_message(operator)
86
86
  end
87
87
 
88
88
  def failure_message(operator)
@@ -16,6 +16,21 @@ module Matchy
16
16
  end
17
17
  end
18
18
 
19
+ class BeKindOfExpectation < Base
20
+ def matches?(receiver)
21
+ @receiver = receiver
22
+ @receiver.kind_of?(@expected)
23
+ end
24
+
25
+ def failure_message
26
+ "Expected #{@receiver.inspect} to be kind of #{@expected.inspect}."
27
+ end
28
+
29
+ def negative_failure_message
30
+ "Expected #{@receiver.inspect} to not be kind of #{@expected.inspect}."
31
+ end
32
+ end
33
+
19
34
  class BeCloseExpectation < Base
20
35
  def initialize(expected, delta, test_case)
21
36
  @expected = expected
@@ -102,6 +117,21 @@ module Matchy
102
117
  end
103
118
  end
104
119
 
120
+ class RespondToExpectation < Base
121
+ def matches?(receiver)
122
+ @receiver = receiver
123
+ @receiver.respond_to?(@expected)
124
+ end
125
+
126
+ def failure_message
127
+ "Expected #{@receiver.inspect} to respond to #{@expected.inspect}."
128
+ end
129
+
130
+ def negative_failure_message
131
+ "Expected #{@receiver.inspect} to not respond to #{@expected.inspect}."
132
+ end
133
+ end
134
+
105
135
  module TestCaseExtensions
106
136
  # Simply checks if the receiver matches the expected object.
107
137
  # TODO: Fill this out to implement much of the RSpec functionality (and then some)
@@ -115,6 +145,16 @@ module Matchy
115
145
  Matchy::Expectations::BeExpectation.new(obj, self)
116
146
  end
117
147
 
148
+ # Checks if the given object is kind_of? the expected class
149
+ #
150
+ # ==== Examples
151
+ #
152
+ # "hello".should be_kind_of(String)
153
+ # 3.should be_kind_of(Fixnum)
154
+ def be_kind_of(klass)
155
+ Matchy::Expectations::BeKindOfExpectation.new(klass, self)
156
+ end
157
+
118
158
  # Checks if the given object is within a given object and delta.
119
159
  #
120
160
  # ==== Examples
@@ -175,7 +215,17 @@ module Matchy
175
215
  #
176
216
  def satisfy(obj)
177
217
  Matchy::Expectations::SatisfyExpectation.new(obj, self)
178
- end
218
+ end
219
+
220
+ # Checks if the given object responds to the given method
221
+ #
222
+ # ==== Examples
223
+ #
224
+ # "foo".should respond_to(:length)
225
+ # {}.should respond_to(:has_key?)
226
+ def respond_to(meth)
227
+ Matchy::Expectations::RespondToExpectation.new(meth, self)
228
+ end
179
229
  end
180
230
  end
181
231
  end
@@ -16,6 +16,8 @@ module Matchy
16
16
  end
17
17
  end
18
18
 
19
+ alias :will :should
20
+
19
21
  # Tests that an expectation doesn't match the given object.
20
22
  #
21
23
  # ==== Examples
@@ -32,6 +34,9 @@ module Matchy
32
34
  end
33
35
  end
34
36
 
37
+ alias :will_not :should_not
38
+ alias :wont :should_not
39
+
35
40
  protected
36
41
  def match_expectation(expectation, match)
37
42
  if expectation.matches?(self) != match
@@ -1,8 +1,8 @@
1
1
  module Matchy
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 0
5
- TINY = 1
4
+ MINOR = 1
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "matchy"
3
- s.version = "0.0.1"
3
+ s.version = "0.1.0"
4
4
  s.date = "2008-10-05"
5
5
  s.summary = "RSpec-esque matchers for use in Test::Unit"
6
6
  s.email = "jeremy@entp.com"
@@ -65,27 +65,27 @@ class TestEnumerableExpectations < Test::Unit::TestCase
65
65
  obj = include(1)
66
66
  obj.matches?([4,5,6])
67
67
 
68
- obj.failure_message.should be "Expected [4, 5, 6] to include [1]."
68
+ obj.failure_message.should be("Expected [4, 5, 6] to include [1].")
69
69
  end
70
70
 
71
71
  def test_include_negative_fail_message
72
72
  obj = include(1)
73
73
  obj.matches?([4,5,6])
74
74
 
75
- obj.negative_failure_message.should be "Expected [4, 5, 6] to not include [1]."
75
+ obj.negative_failure_message.should be("Expected [4, 5, 6] to not include [1].")
76
76
  end
77
77
 
78
78
  def test_exclude_fail_message
79
79
  obj = exclude(4)
80
80
  obj.matches?([4,5,6])
81
81
 
82
- obj.failure_message.should be "Expected [4, 5, 6] to exclude [4]."
82
+ obj.failure_message.should be("Expected [4, 5, 6] to exclude [4].")
83
83
  end
84
84
 
85
85
  def test_exclude_negative_fail_message
86
86
  obj = exclude(4)
87
87
  obj.matches?([4,5,6])
88
88
 
89
- obj.negative_failure_message.should be "Expected [4, 5, 6] to not exclude [4]."
89
+ obj.negative_failure_message.should be("Expected [4, 5, 6] to not exclude [4].")
90
90
  end
91
91
  end
@@ -10,10 +10,22 @@ class TestModals < Test::Unit::TestCase
10
10
  3.should(@expectation)
11
11
  end
12
12
 
13
+ def test_will
14
+ 3.will(@expectation)
15
+ end
16
+
13
17
  def test_should_not
14
18
  3.should_not(@bad_expectation)
15
19
  end
16
20
 
21
+ def test_will_not
22
+ 3.will_not(@bad_expectation)
23
+ end
24
+
25
+ def test_wont
26
+ 3.wont(@bad_expectation)
27
+ end
28
+
17
29
  def test_should_operator_expectation_returned
18
30
  obj = 3.should
19
31
  assert_equal Matchy::Expectations::OperatorExpectation, obj.class
@@ -138,22 +138,20 @@ class TestOperatorExpectations < Test::Unit::TestCase
138
138
  def test_fail_message
139
139
  obj = Matchy::Expectations::OperatorExpectation.new(3, true)
140
140
 
141
- def obj.fail!(fail)
141
+ def obj.flunk(msg)
142
+ msg
142
143
  end
143
144
 
144
- obj == 4
145
-
146
- obj.send(:failure_message, "==").should =~ /Expected 3 to == 4./
145
+ (obj == 4).should == "Expected 3 to == 4."
147
146
  end
148
147
 
149
148
  def test_negative_fail_message
150
- obj = Matchy::Expectations::OperatorExpectation.new(3, true)
149
+ obj = Matchy::Expectations::OperatorExpectation.new(3, false)
151
150
 
152
- def obj.fail!(fail)
151
+ def obj.flunk(msg)
152
+ msg
153
153
  end
154
154
 
155
- obj == 3
156
-
157
- obj.send(:negative_failure_message, "==").should =~ /Expected 3 to not == 3./
155
+ (obj == 3).should == "Expected 3 to not == 3."
158
156
  end
159
157
  end
@@ -135,28 +135,28 @@ class TestTruthExpectations < Test::Unit::TestCase
135
135
  obj = equal(4)
136
136
  obj.matches?(5)
137
137
 
138
- obj.failure_message.should be "Expected 5 to equal 4."
138
+ obj.failure_message.should be("Expected 5 to equal 4.")
139
139
  end
140
140
 
141
141
  def test_equal_negative_fail_message
142
142
  obj = equal(5)
143
143
  obj.matches?(5)
144
144
 
145
- obj.negative_failure_message.should be "Expected 5 to not equal 5."
145
+ obj.negative_failure_message.should be("Expected 5 to not equal 5.")
146
146
  end
147
147
 
148
148
  def test_eql_fail_message
149
149
  obj = eql(4)
150
150
  obj.matches?(5)
151
151
 
152
- obj.failure_message.should be "Expected 5 to eql 4."
152
+ obj.failure_message.should be("Expected 5 to eql 4.")
153
153
  end
154
154
 
155
- def test_eql_negative_fail_message
155
+ def test_eql_negative_fail_message_for_eql
156
156
  obj = eql(5)
157
157
  obj.matches?(5)
158
158
 
159
- obj.negative_failure_message.should be "Expected 5 to not eql 5."
159
+ obj.negative_failure_message.should be("Expected 5 to not eql 5.")
160
160
  end
161
161
 
162
162
  def test_exist_fail_message
@@ -177,41 +177,82 @@ class TestTruthExpectations < Test::Unit::TestCase
177
177
  obj = be_close(3.0)
178
178
  obj.matches?(6.0)
179
179
 
180
- obj.failure_message.should be "Expected 6.0 to be close to 3.0 (delta: 0.3)."
180
+ obj.failure_message.should be("Expected 6.0 to be close to 3.0 (delta: 0.3).")
181
181
  end
182
182
 
183
183
  def test_be_close_negative_fail_message
184
184
  obj = be_close(5.0)
185
185
  obj.matches?(5.0)
186
186
 
187
- obj.negative_failure_message.should be "Expected 5.0 to not be close to 5.0 (delta: 0.3)."
187
+ obj.negative_failure_message.should be("Expected 5.0 to not be close to 5.0 (delta: 0.3).")
188
188
  end
189
189
 
190
190
  def test_be_fail_message
191
191
  obj = be(4)
192
192
  obj.matches?(5)
193
193
 
194
- obj.failure_message.should be "Expected 5 to be 4."
194
+ obj.failure_message.should be("Expected 5 to be 4.")
195
195
  end
196
196
 
197
197
  def test_be_negative_fail_message
198
198
  obj = be(5)
199
199
  obj.matches?(5)
200
200
 
201
- obj.negative_failure_message.should be "Expected 5 to not be 5."
201
+ obj.negative_failure_message.should be("Expected 5 to not be 5.")
202
202
  end
203
203
 
204
204
  def test_satisfy_fail_message
205
205
  obj = satisfy(lambda {|x| x == 4})
206
206
  obj.matches?(6)
207
207
 
208
- obj.failure_message.should be "Expected 6 to satisfy given block."
208
+ obj.failure_message.should be("Expected 6 to satisfy given block.")
209
209
  end
210
210
 
211
- def test_eql_negative_fail_message
211
+ def test_eql_negative_fail_message_for_matches
212
212
  obj = satisfy(lambda {|x| x == 4})
213
213
  obj.matches?(4)
214
214
 
215
- obj.negative_failure_message.should be "Expected 4 to not satisfy given block."
215
+ obj.negative_failure_message.should be("Expected 4 to not satisfy given block.")
216
216
  end
217
+
218
+ def test_kind_of
219
+ 3.should be_kind_of(Fixnum)
220
+ end
221
+
222
+ def test_kind_of_fail
223
+ lambda {
224
+ 3.should be_kind_of(Hash)
225
+ }.should raise_error(Test::Unit::AssertionFailedError)
226
+ end
227
+
228
+ def test_negative_kind_of
229
+ 3.should_not be_kind_of(Hash)
230
+ end
231
+
232
+ def test_negative_kind_of_fail
233
+ lambda {
234
+ 3.should_not be_kind_of(Fixnum)
235
+ }.should raise_error(Test::Unit::AssertionFailedError)
236
+ end
237
+
238
+ def test_respond_to
239
+ "foo".should respond_to(:length)
240
+ end
241
+
242
+ def test_respond_to_fail
243
+ lambda {
244
+ "foo".should respond_to(:nonexistant_method)
245
+ }.should raise_error(Test::Unit::AssertionFailedError)
246
+ end
247
+
248
+ def test_negative_respond_to
249
+ "foo".should_not respond_to(:nonexistant_method)
250
+ end
251
+
252
+ def test_negative_respond_to_fail
253
+ lambda {
254
+ "foo".should_not respond_to(:length)
255
+ }.should raise_error(Test::Unit::AssertionFailedError)
256
+ end
257
+
217
258
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jeremymcanally-matchy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy McAnally