rubyunit 0.1.9 → 0.2.10

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c3e1a28adb333f17a61ff08881e22893ad74d765
4
- data.tar.gz: 4dfe9ddcb22f1548a9f0455e311ec7c8519e9dd4
3
+ metadata.gz: d35ebf25087d43280b2ab9582e47ceedb6c74053
4
+ data.tar.gz: a4a28b7a31bd3972ee4629e839ff044a5937bed0
5
5
  SHA512:
6
- metadata.gz: c810ced52681035b09c75486551c58f6cf9d995104be9d67c596beb46eeaf6eb15fc3ce0ff20be5c9b120dd9dc6984918a026af02d2e3e4998397ecb8d96f6d4
7
- data.tar.gz: e5fc0d25fe211f90efec5e600ea3f230af74bf617dc9703807a770d6eb533c5326151b3ffc4c06d02fa7e9c1c5aacc00f65b4ef14947796be24f95d224e8a8cb
6
+ metadata.gz: 55530d6d309f9bdf6d2f0fb3fa3dc40ab7050081a3e824d371d2f17e1e0f71ef0e1bb7e490fadb3fc97385a5e4080cfc3ea6c53d55f36bc06ddeaad5ca9b5383
7
+ data.tar.gz: 401b7ca8726f793ff13d5483617059ba04507677ed1c90837f6cfd279aceae1d8ec5887b252ccf3a9cfa42614a7a6516a4edb80fb13d69b35cb9330ee4de7113
@@ -0,0 +1,549 @@
1
+
2
+ module RubyUnit
3
+ #
4
+ # All classes derrived from the RubyUnit::TestCase will automatically be run by
5
+ # the test runner.
6
+ # * Test methods must currently be named ending in 'Test'
7
+ # * Data methods must currently be named ending in 'Data'
8
+ # All data methods must return an array of arrays as param lists to passed to
9
+ # the corresponding test method.
10
+ # * Exceptions raised in this class are generally caught by the RubyUnit::Runner
11
+ #
12
+ # MyTest < RubyUnit::TestCase
13
+ # def simpleData
14
+ # [
15
+ # [ 1, 3],
16
+ # ['string', nil],
17
+ # ],
18
+ # end
19
+ #
20
+ # def simpleTest param1, param2
21
+ # # run assertions
22
+ # end
23
+ # end
24
+ #
25
+ module Assertions
26
+ # Tracks the total number of assertions made during the tests
27
+ @@assertions = 0
28
+
29
+ #
30
+ # Fail the test. This is used when some conditioned outside the test warrants
31
+ # a test failure.
32
+ # * This is likely an indication of something unexpected or missing functionality
33
+ # * raises RubyUnit::AssertionFailure
34
+ #
35
+ # message::
36
+ # The message provided to be reported for a failure
37
+ #
38
+ # data::
39
+ # The data associated with assertion
40
+ #
41
+ # fail "I wasn't expecting the moon to fall into Lake Michigan" # => fail
42
+ #
43
+ def fail message = nil, data = {}
44
+ build_message 'Failing test', message, data
45
+ end
46
+
47
+ #
48
+ # Assert that a test condition is true.
49
+ # * raises RubyUnit::AssertionFailure if _value_ is false or nil
50
+ #
51
+ # value::
52
+ # The value that is being checked by the assertion
53
+ #
54
+ # message::
55
+ # The message provided to be reported for a failure
56
+ #
57
+ # assert false, "This will fail" # => fail
58
+ #
59
+ def assert value, message = nil
60
+ __assert value, 'Failed to assert that value is false or nil', message, {:value=>value}
61
+ end
62
+
63
+ #
64
+ # Assert that a test condition is false.
65
+ # * raises RubyUnit::AssertionFailure unless _value_ is false or nil
66
+ #
67
+ # value::
68
+ # The value that is being checked by the assertion
69
+ #
70
+ # message::
71
+ # The message provided to be reported for a failure
72
+ #
73
+ # assertNot true, "This will fail" # => fail
74
+ #
75
+ def assertNot value, message = nil
76
+ __reject value, 'Value should NOT be false or nil', message, {:value=>value}
77
+ end
78
+
79
+ #
80
+ # Assert that two values are equal.
81
+ # * raises RubyUnit::AssertionFailure unless _expected_ equals _actual_
82
+ #
83
+ # expected::
84
+ # The value that is forbidden by the assertion
85
+ #
86
+ # actual::
87
+ # The value that is being checked by the assertion
88
+ #
89
+ # message::
90
+ # The message provided to be reported for a failure
91
+ #
92
+ # assertEqual 42, 24, "This will fail" # => fail
93
+ #
94
+ def assertEqual expected, actual, message = nil
95
+ __assert (expected == actual), 'Failed to assert that values are equal', message, {:expected=>expected, :actual=>actual}
96
+ end
97
+
98
+ #
99
+ # Assert that two values are NOT equal.
100
+ # * raises RubyUnit::AssertionFailure if _illegal_ equals _actual_
101
+ #
102
+ # illegal::
103
+ # The value that is not allowed by the assertion
104
+ #
105
+ # actual::
106
+ # The value that is being checked by the assertion
107
+ #
108
+ # message::
109
+ # The message provided to be reported for a failure
110
+ #
111
+ # assertNotEqual 3.14, 3.14, "This will fail" # => fail
112
+ #
113
+ def assertNotEqual illegal, actual, message = nil
114
+ __reject (illegal == actual), 'Values should NOT be equal', message, {:illegal=>illegal, :actual=>actual}
115
+ end
116
+
117
+ #
118
+ # Assert that a value matches a Regexp pattern.
119
+ # * raises RubyUnit::AssertionFailure unless _value_ matches _pattern_
120
+ #
121
+ # pattern::
122
+ # A Regexp pattern expected by the assertion
123
+ #
124
+ # value::
125
+ # The value that is being checked for the assertion
126
+ #
127
+ # message::
128
+ # The message provided to be reported for a failure
129
+ #
130
+ # assertMatch /^Hello/, 'Goodbye!', "This will fail" # => fail
131
+ #
132
+ def assertMatch pattern, value, message = nil
133
+ __assert (value =~ pattern), 'Failed to assert value matches pattern', message, {:pattern=>pattern, :value=>value}
134
+ end
135
+
136
+ #
137
+ # Assert that a value does not match a Regexp pattern.
138
+ # * raises RubyUnit::AssertionFailure if _value_ matches _pattern_
139
+ #
140
+ # pattern::
141
+ # A Regexp pattern excluded by the assertion
142
+ #
143
+ # value::
144
+ # The value that is being checked for the assertion
145
+ #
146
+ # message::
147
+ # The message provided to be reported for a failure
148
+ #
149
+ # assertMatch /^Good/, 'Goodbye!', "This will fail" # => fail
150
+ #
151
+ def assertNotMatch exclusion, value, message = nil
152
+ __reject (value =~ exclusion), 'Value should NOT match exclusion', message, {:exclusion=>exclusion, :value=>value}
153
+ end
154
+
155
+ #
156
+ # Assert that two objects are the same object
157
+ # * raises RubyUnit::AssertionFailure unless _expected_ and _actual_ are
158
+ # the same object.
159
+ #
160
+ # expected::
161
+ # The expected object
162
+ #
163
+ # actual::
164
+ # The object that is being checked against _expected_
165
+ #
166
+ # message::
167
+ # The message provided to be reported for a failure
168
+ #
169
+ # assertSame '42', 42, 'Not even close.' # => fail
170
+ #
171
+ def assertSame expected, actual, message = nil
172
+ __assert (expected.equal? actual), 'Failed to assert objects are the same', message, {:expected=>expected, :actual=>actual}
173
+ end
174
+
175
+ #
176
+ # Assert that two objects are not the same object
177
+ # * raises RubyUnit::AssertionFailure if _illegal_ and _actual_ are the
178
+ # same object.
179
+ #
180
+ # illegal::
181
+ # The expected that it shouldn't be
182
+ #
183
+ # actual::
184
+ # The object that is being checked against _illegal_
185
+ #
186
+ # message::
187
+ # The message provided to be reported for a failure
188
+ #
189
+ # assertNotSame value, value, 'Imagine that!' # => fail
190
+ #
191
+ def assertNotSame illegal, actual, message = nil
192
+ __reject (illegal.equal? actual), 'Objects shoul NOT be the same', message, {:illegal=>illegal, :actual=>actual}
193
+ end
194
+
195
+ #
196
+ # Assert that an object is an instance of the specified class or one of
197
+ # its descendents.
198
+ # * raises RubyUnit::AssertionFailure unless _object_ is an instance of
199
+ # _klass_ or one of its descendents.
200
+ #
201
+ # klass::
202
+ # The class that is expected
203
+ #
204
+ # object::
205
+ # The object that will be checked against _klass_
206
+ #
207
+ # message::
208
+ # The message provided to be reported for a failure
209
+ #
210
+ # assertKindOf String, 25, 'Nope, try again.' # => fail
211
+ #
212
+ def assertKindOf klass, object, message = nil
213
+ __assert (object.is_a? klass), 'Failed to assert object heritage', message, {:klass=>klass, :object=>object}
214
+ end
215
+
216
+ alias_method :assertIsA, :assertKindOf
217
+
218
+ #
219
+ # Assert that an object is not an instance of the specified class or one of
220
+ # its descendents.
221
+ # * raises RubyUnit::AssertionFailure if _object_ is an instance of _exclusion_ or
222
+ # one of its descendents.
223
+ #
224
+ # exclusion::
225
+ # The class that is excluded
226
+ #
227
+ # object::
228
+ # The object that will be checked against _klass_
229
+ #
230
+ # message::
231
+ # The message provided to be reported for a failure
232
+ #
233
+ # assertNotKindOf Numeric, 25, 'Nope, try again.' # => fail
234
+ #
235
+ def assertNotKindOf exclusion, object, message = nil
236
+ __reject (object.is_a? exclusion), 'Object should not be a descendent', message, {:exclusion=>exclusion, :object=>object}
237
+ end
238
+
239
+ [:assertNotIsA, :assertIsNotA].each do |method|
240
+ alias_method method, :assertNotKindOf
241
+ end
242
+
243
+ #
244
+ # Assert that an object is an instance of a specified class
245
+ # * raises RubyUnit::AssertionFailure unless _object_ is an instance of _klass_.
246
+ #
247
+ # klass::
248
+ # The class that is expected
249
+ #
250
+ # object::
251
+ # The object that will be checked against _klass_
252
+ #
253
+ # message::
254
+ # The message provided to be reported for a failure
255
+ #
256
+ # assertInstanceOf Integer, '25', 'So close, but... No.' # => fail
257
+ #
258
+ def assertInstanceOf klass, object, message = nil
259
+ __assert (object.instance_of? klass), 'Failed to assert object instance', message, {:klass=>klass, :object=>object}
260
+ end
261
+
262
+ #
263
+ # Assert that an object is an instance of a specified class
264
+ # * raises RubyUnit::AssertionFailure unless _object_ is an instance of _klass_.
265
+ #
266
+ # exclusion::
267
+ # The class that is expected
268
+ #
269
+ # object::
270
+ # The object that will be checked against _klass_
271
+ #
272
+ # message::
273
+ # The message provided to be reported for a failure
274
+ #
275
+ # assertNotInstanceOf Integer, 25, 'So close, but... No.' # => fail
276
+ #
277
+ def assertNotInstanceOf exclusion, object, message = nil
278
+ __reject (object.instance_of? exclusion), 'Object should NOT be this instance', message, {:exclusion=>exclusion, :object=>object}
279
+ end
280
+
281
+ #
282
+ # Assert that a constant is defined correctly in the correct class
283
+ # * raises RubyUnit::AssertionFailure unless the constant is defined in
284
+ # the specified class and it is the correct type and value
285
+ #
286
+ # expected::
287
+ # The value that is expected for the constant
288
+ #
289
+ # klass::
290
+ # The class where the constant should be defined
291
+ #
292
+ # konstant::
293
+ # The name of the constant
294
+ #
295
+ # message::
296
+ # The message provided to be reported for a failure
297
+ #
298
+ # assertConst 42, Numbers, 'TWENTYFOUR', 'So dyslexic.' # => fail
299
+ #
300
+ def assertConst expected, klass, konstant, message = nil
301
+ __wrap_assertion do
302
+ assertConstDefined klass, konstant, message
303
+ value = klass.const_get konstant
304
+ assertIsA expected.class, value, message
305
+ assertEqual expected, value, message
306
+ end
307
+ end
308
+
309
+ #
310
+ # Assert that a constant is defined in the specified class
311
+ # * raises RubyUnit::AssertionFailure unless the constant is defined in
312
+ # the specified class
313
+ #
314
+ # klass::
315
+ # The class where the constant should be defined
316
+ #
317
+ # konstant::
318
+ # The name of the constant
319
+ #
320
+ # message::
321
+ # The message provided to be reported for a failure
322
+ #
323
+ # assertConstDefined Numbers, 'FORTYTWO', 'Mystery.' # => ??
324
+ #
325
+ def assertConstDefined klass, konstant, message = nil
326
+ __assert (klass.const_defined? konstant), 'Failed to assert constant is defined', message, {:klass=>klass, :konstant=>konstant}
327
+ end
328
+
329
+ #
330
+ # Assert that a constant is not defined in the specified class
331
+ # * raises RubyUnit::AssertionFailure if the constant is defined in
332
+ # the specified class
333
+ #
334
+ # klass::
335
+ # The class where the constant should not be defined
336
+ #
337
+ # konstant::
338
+ # The name of the constant
339
+ #
340
+ # message::
341
+ # The message provided to be reported for a failure
342
+ #
343
+ # assertConstNotDefined Numbers, 'TWENTYFOUR', 'Mystery.' # => ??
344
+ #
345
+ def assertConstNotDefined klass, konstant, message = nil
346
+ __reject (klass.const_defined? konstant), 'Constant should not be defined', message, {:klass=>klass, :konstant=>konstant}
347
+ end
348
+
349
+ #
350
+ # Assert that no exception is raised.
351
+ # * raises RubyUnit::AssertionFailure if any exception is raised
352
+ #
353
+ # message::
354
+ # The message provided to be reported for a failure
355
+ #
356
+ # &block::
357
+ # The code block that is executed
358
+ #
359
+ # assertNothingRaised 'Not expecting an exception!' do
360
+ # # do something
361
+ # end
362
+ #
363
+ def assertNothingRaised message = nil, &block
364
+ __wrap_assertion do
365
+ begin
366
+ yield
367
+ rescue Exception => e
368
+ build_message 'Exception should NOT be raised', message, {:exception=>e.message}
369
+ end
370
+ end
371
+ end
372
+
373
+ #
374
+ # Assert that a specified exception message is raised.
375
+ # * raises RubyUnit::AssertionFailure unless the correct Exception message is raised
376
+ #
377
+ # pattern::
378
+ # The String or Regexp that will be used to validate the Exception message
379
+ #
380
+ # message::
381
+ # The message provided to be reported for a failure
382
+ #
383
+ # &block::
384
+ # The code block that is expected to throw the Exception
385
+ #
386
+ # assertRaiseMessage /^Invalid/, 'Expecting an exception!' do
387
+ # # do something
388
+ # end
389
+ #
390
+ def assertRaiseMessage pattern, message = nil, &block
391
+ assertRaiseExpected Exception, pattern, message, &block
392
+ end
393
+
394
+ #
395
+ # Assert that a specified exception type is raised.
396
+ # * raises RubyUnit::AssertionFailure unless the correct Exception type is raised
397
+ #
398
+ # e::
399
+ # The Exception class that is expected.
400
+ #
401
+ # message::
402
+ # The message provided to be reported for a failure
403
+ #
404
+ # &block::
405
+ # The code block that is expected to throw the Exception
406
+ #
407
+ # assertRaiseKindOf StandardError, 'Expecting an exception!' do # => fail
408
+ # # do something
409
+ # end
410
+ #
411
+ def assertRaiseKindOf e, message = nil, &block
412
+ assertRaiseExpected e, '', message, &block
413
+ end
414
+
415
+ #
416
+ # Assert that a specified exception is raised.
417
+ # * raises RubyUnit::AssertionFailure unless the correct Exception is raised
418
+ #
419
+ # exception::
420
+ # The Exception class that is expected.
421
+ #
422
+ # pattern::
423
+ # The String or Regexp that will be used to validate the Exception message
424
+ #
425
+ # message::
426
+ # The message provided to be reported for a failure
427
+ #
428
+ # &block::
429
+ # The code block that is expected to throw the Exception
430
+ #
431
+ # assertRaiseExpected StandardError, /^Invalid/, 'Expecting an exception!' do
432
+ # raise StandardError, 'Invalid Retroincabulator'
433
+ # end
434
+ #
435
+ def assertRaiseExpected exception, pattern, message = nil, &block
436
+ __validate_exception pattern, exception
437
+ __wrap_assertion do
438
+ begin
439
+ yield
440
+ build_message 'Expected exception was not raised', message, {:exception=>exception, :pattern=>pattern}
441
+ rescue exception => e
442
+ assertEqual pattern, e.message if pattern.is_a? String and pattern.length > 0
443
+ assertMatch pattern, e.message if pattern.is_a? Regexp
444
+ end
445
+ end
446
+ end
447
+
448
+ private
449
+ #
450
+ # Builds the message that will be used with the assertion
451
+ # * raises RubyUnit::AssertionFailure
452
+ # * raises ArgumentError unless error is a String
453
+ # * raises ArgumentError unless message is nil or a String
454
+ # * raises ArgumentError unless data is a Hash
455
+ #
456
+ # error::
457
+ # The assertion description
458
+ #
459
+ # message::
460
+ # The message provided by the test for the assertion
461
+ #
462
+ # data::
463
+ # The data associated with assertion failure
464
+ #
465
+ # build_message 'Failing Test', message, {'expected' => expected, 'actual' => actual }
466
+ #
467
+ def build_message error, message, data = {} # :nodoc:
468
+ raise ArgumentError, 'Error description must be a String' unless error.is_a? String
469
+ raise ArgumentError, 'Failure message must be String' unless message.nil? or message.is_a? String
470
+ raise ArgumentError, 'Failure data must be a Hash' unless data.is_a? Hash
471
+
472
+ error_message = error
473
+ error_message << "\n#{message}" if not message.nil?
474
+ data.each do |index, value|
475
+ error_message << "\n#{index}:\n\t#{value.inspect}"
476
+ end
477
+ raise AssertionFailure, error_message
478
+ end
479
+
480
+ #
481
+ # The assertion wrapper is responsible for doing everything that must be done
482
+ # on each assertion.
483
+ # * keep track of the total number of assertions
484
+ #
485
+ # &block::
486
+ # The assertion which is being wrapped
487
+ #
488
+ def __wrap_assertion &block # :nodoc:
489
+ @@assertions += 1
490
+ yield
491
+ end
492
+
493
+ #
494
+ # Internally validate that an assertion not false or nil
495
+ # * raises RubyUnit::AssertionFailure unless _value_ is true
496
+ #
497
+ # value::
498
+ # The value to be asserted
499
+ #
500
+ # error::
501
+ # The error associated with the assertion being checked
502
+ #
503
+ # message::
504
+ # The message provided to be reported for a failure
505
+ #
506
+ # data::
507
+ # The data associated with assertion
508
+ #
509
+ # __assert value, 'Failed to assert value is true', message, {:value=>value}
510
+ #
511
+ def __assert value, error, message, data # :nodoc:
512
+ __wrap_assertion do
513
+ build_message error, message, data unless value
514
+ end
515
+ end
516
+
517
+ #
518
+ # Internally validate that an assertion is false or nil
519
+ # * raises RubyUnit::AssertionFailure unless _value_ is false or nil
520
+ #
521
+ # value::
522
+ # The value to be asserted
523
+ #
524
+ # error::
525
+ # The error associated with the assertion being checked
526
+ #
527
+ # message::
528
+ # The message provided to be reported for a failure
529
+ #
530
+ # data::
531
+ # The data associated with assertion
532
+ #
533
+ # __reject value, 'Failed to assert value is not true', message, {:value=>value}
534
+ #
535
+ def __reject value, error, message, data # :nodoc:
536
+ __assert (not value), error, message, data
537
+ end
538
+
539
+ #
540
+ # Validate the parameters for exception assertions
541
+ # * raises ArgumentError if _pattern_ is not a String or Regexp
542
+ # * raises ArgumentError unless _e_ is a descendent of the Exception class
543
+ #
544
+ def __validate_exception pattern, e = Exception # :nodoc:
545
+ raise ArgumentError, "Exception message must be a Regexp or String" unless pattern.is_a? Regexp or pattern.is_a? String
546
+ raise ArgumentError, "Exception must be a subclass of Exception" unless e < Exception
547
+ end
548
+ end
549
+ end
@@ -3,8 +3,6 @@ require_relative 'AssertionFailure'
3
3
 
4
4
  module RubyUnit
5
5
  #
6
- # = RubyUnit::Runner
7
- #
8
6
  # This is the test runner.
9
7
  # Done, and done.
10
8
  #
@@ -1,8 +1,7 @@
1
1
  require_relative 'AssertionFailure'
2
+ require_relative 'Assertions'
2
3
 
3
4
  module RubyUnit
4
- #
5
- # = RubyUnit::TestCase
6
5
  #
7
6
  # All classes derrived from the RubyUnit::TestCase will automatically be run by
8
7
  # the test runner.
@@ -26,37 +25,6 @@ module RubyUnit
26
25
  # end
27
26
  #
28
27
  class TestCase
29
- private
30
- #
31
- # Builds the message that will be used with the assertion
32
- # * raises RubyUnit::AssertionFailure
33
- # * raises ArgumentError unless error is a String
34
- # * raises ArgumentError unless message is nil or a String
35
- # * raises ArgumentError unless data is a Hash
36
- #
37
- # error::
38
- # The assertion failure
39
- #
40
- # message::
41
- # The message provided by the test for the assertion
42
- #
43
- # data::
44
- # The data associated with assertion failure
45
- #
46
- # build_message 'Failing Test', message, {'expected' => expected, 'actual' => actual }
47
- #
48
- def build_message error, message, data = {} # :nodoc:
49
- raise ArgumentError, 'Error message must be a String' unless error.is_a? String
50
- raise ArgumentError, 'Failure message must be String' unless message.nil? or message.is_a? String
51
- raise ArgumentError, 'Failure data must be a Hash' unless data.is_a? Hash
52
-
53
- error_message = error
54
- error_message << "\n#{message}" if not message.nil?
55
- data.each do |index, value|
56
- error_message << "\n%10s: #{value.inspect}" % index
57
- end
58
- raise AssertionFailure, error_message
59
- end
60
28
 
61
29
  public
62
30
 
@@ -80,372 +48,10 @@ module RubyUnit
80
48
  def teardown
81
49
  end
82
50
 
83
- private
84
- #
85
- # The assertion wrapper is responsible for doing everything that must be done
86
- # on each assertion.
87
- # * keep track of the total number of assertions
88
- #
89
- # &block::
90
- # The assertion which is being wrapped
91
- #
92
- def __assertion &block # :nodoc:
93
- @@assertions += 1
94
- yield
95
- end
96
-
97
51
  protected
52
+ include Assertions
98
53
 
99
- #
100
- # Fail the test. This is used when some conditioned outside the test warrants
101
- # a test failure.
102
- # * This is likely an indication of something unexpected or missing functionality
103
- # * raises RubyUnit::AssertionFailure
104
- #
105
- # message::
106
- # The message provided to be reported for a failure
107
- #
108
- # fail "I wasn't expecting the moon to fall into Lake Michigan" # => fail
109
- #
110
- def fail message = nil
111
- __assertion do
112
- build_message 'Failing test', message
113
- end
114
- end
115
-
116
- #
117
- # Assert that a test condition is true.
118
- # * raises RubyUnit::AssertionFailure if _value_ is false or nil
119
- #
120
- # value::
121
- # The value that is being checked by the assertion
122
- #
123
- # message::
124
- # The message provided to be reported for a failure
125
- #
126
- # assert false, "This will fail" # => fail
127
- #
128
- def assert value, message = nil
129
- __assertion do
130
- build_message 'Failed to assert that value is false or nil', message, {:value=>value} unless value
131
- end
132
- end
133
-
134
- #
135
- # Assert that a test condition is false.
136
- # * raises RubyUnit::AssertionFailure unless _value_ is false or nil
137
- #
138
- # value::
139
- # The value that is being checked by the assertion
140
- #
141
- # message::
142
- # The message provided to be reported for a failure
143
- #
144
- # assertNot true, "This will fail" # => fail
145
- #
146
- def assertNot value, message = nil
147
- __assertion do
148
- build_message 'Value should NOT be false or nil', message, {:value=>value} if value
149
- end
150
- end
151
-
152
- #
153
- # Assert that two values are equal.
154
- # * raises RubyUnit::AssertionFailure unless _expected_ equals _actual_
155
- #
156
- # expected::
157
- # The value that is forbidden by the assertion
158
- #
159
- # actual::
160
- # The value that is being checked by the assertion
161
- #
162
- # message::
163
- # The message provided to be reported for a failure
164
- #
165
- # assertEqual 42, 24, "This will fail" # => fail
166
- #
167
- def assertEqual expected, actual, message = nil
168
- __assertion do
169
- build_message 'Failed to assert that values are equal', message, {:expected=>expected, :actual=>actual} unless expected == actual
170
- end
171
- end
172
-
173
- #
174
- # Assert that two values are NOT equal.
175
- # * raises RubyUnit::AssertionFailure if _illegal_ equals _actual_
176
- #
177
- # illegal::
178
- # The value that is not allowed by the assertion
179
- #
180
- # actual::
181
- # The value that is being checked by the assertion
182
- #
183
- # message::
184
- # The message provided to be reported for a failure
185
- #
186
- # assertNotEqual 3.14, 3.14, "This will fail" # => fail
187
- #
188
- def assertNotEqual illegal, actual, message = nil
189
- __assertion do
190
- build_message 'Values should NOT be equal', message, {:illegal=>illegal, :actual=>actual} if illegal == actual
191
- end
192
- end
193
-
194
- #
195
- # Assert that a value matches a Regexp pattern.
196
- # * raises RubyUnit::AssertionFailure unless _value_ matches _pattern_
197
- #
198
- # pattern::
199
- # A Regexp pattern expected by the assertion
200
- #
201
- # value::
202
- # The value that is being checked for the assertion
203
- #
204
- # message::
205
- # The message provided to be reported for a failure
206
- #
207
- # assertMatch /^Hello/, 'Goodbye!', "This will fail" # => fail
208
- #
209
- def assertMatch pattern, value, message = nil
210
- __assertion do
211
- build_message 'Failed to assert value matches regular expression', message, {:pattern=>pattern, :value=>value} unless value =~ pattern
212
- end
213
- end
214
-
215
- #
216
- # Assert that a specified exception is raised.
217
- # * raises RubyUnit::AssertionFailure unless the correct Exception is raised
218
- # * raises ArgumentError unless _exception_ is a descendent of the Exception class
219
- # * raises ArgumentError if _pattern_ is not a String or Regexp
220
- # * raises ArgumentError if _message_ is not a String or nil
221
- #
222
- # exception::
223
- # The Exception class that is expected.
224
- #
225
- # pattern::
226
- # The String or Regexp that will be used to validate the Exception message
227
- #
228
- # message::
229
- # The message provided to be reported for a failure
230
- #
231
- # &block::
232
- # The code block that is expected to throw the Exception
233
- #
234
- # assertRaiseExpected Exception, /^Invalid/, 'Expecting an exception!' do
235
- # raise 'Invalid Retroincabulator'
236
- # end
237
- #
238
- def assertRaiseExpected exception, pattern, message = nil, &block
239
- raise ArgumentError, "Exception must be a subclass of Exception" unless exception < Exception
240
- raise ArgumentError, "exception message must be a Regexp or String" unless pattern.is_a? Regexp or pattern.is_a? String
241
- raise ArguemntError, "message must be a String or nil" unless message.is_a? String or message.nil?
242
-
243
- __assertion do
244
- begin
245
- yield
246
- build_message 'Expected exception was not raised', message, {:exception=>exception, :pattern=>pattern}
247
- rescue exception => e
248
- assertEqual pattern, e.message if pattern.is_a? String
249
- assertMatch pattern, e.message if pattern.is_a? Regexp
250
- end
251
- end
252
- end
253
-
254
- #
255
- # Assert that no exception is raised.
256
- # * raises RubyUnit::AssertionFailure if any exception is raised
257
- #
258
- # message::
259
- # The message provided to be reported for a failure
260
- #
261
- # &block::
262
- # The code block that is executed
263
- #
264
- # assertNothingRaised 'Not expecting an exception!' do
265
- # # do something
266
- # end
267
- #
268
- def assertNothingRaised message = nil, &block
269
- __assertion do
270
- begin
271
- yield
272
- rescue Exception => e
273
- build_message 'Exception was thrown but not expected', message, {:exception=>e.message}
274
- end
275
- end
276
- end
277
-
278
- #
279
- # Assert that an object is an instance of the specified class or one of
280
- # its descendents.
281
- # * raises RubyUnit::AssertionFailure unless _object_ is an instance of
282
- # _klass_ or one of its descendents.
283
- #
284
- # klass::
285
- # The class that is expected
286
- #
287
- # object::
288
- # The object that will be checked against _klass_
289
- #
290
- # message::
291
- # The message provided to be reported for a failure
292
- #
293
- # assertIsA String, 25, 'Nope, try again.' # => fail
294
- #
295
- def assertIsA klass, object, message = nil
296
- __assertion do
297
- build_message 'Failed to assert object heritage', message, {:klass=>klass, :object=>object} unless object.is_a? klass
298
- end
299
- end
300
-
301
- #
302
- # Assert that an object is an instance of a specified class
303
- # * raises RubyUnit::AssertionFailure unless _object_ is an instance of _klass_.
304
- #
305
- # klass::
306
- # The class that is expected
307
- #
308
- # object::
309
- # The object that will be checked against _klass_
310
- #
311
- # message::
312
- # The message provided to be reported for a failure
313
- #
314
- # assertInstanceOf Integer, '25', 'So close, but... No.' # => fail
315
- #
316
- def assertInstanceOf klass, object, message = nil
317
- __assertion do
318
- build_message 'Failed to assert correct instance', message, {:klass=>klass, :object=>object} unless object.instance_of? klass
319
- end
320
- end
321
-
322
- #
323
- # Assert that two objects are the same object
324
- # * raises RubyUnit::AssertionFailure unless _expected_ and _actual_ are
325
- # the same object.
326
- #
327
- # expected::
328
- # The expected object
329
- #
330
- # actual::
331
- # The object that is being checked against _expected_
332
- #
333
- # message::
334
- # The message provided to be reported for a failure
335
- #
336
- # assertSame '42', 42, 'Not even close.' # => fail
337
- #
338
- def assertSame expected, actual, message = nil
339
- __assertion do
340
- build_message 'Failed to assert object are the same', message, {:expected=>expected, :actual=>actual} unless expected.equal? actual
341
- end
342
- end
343
-
344
- #
345
- # Assert that two objects are not the same object
346
- # * raises RubyUnit::AssertionFailure if _illegal_ and _actual_ are the
347
- # same object.
348
- #
349
- # illegal::
350
- # The expected that it shouldn't be
351
- #
352
- # actual::
353
- # The object that is being checked against _illegal_
354
- #
355
- # message::
356
- # The message provided to be reported for a failure
357
- #
358
- # assertNotSame value, value, 'Imagine that!' # => fail
359
- #
360
- def assertNotSame illegal, actual, message = nil
361
- __assertion do
362
- build_message 'Failed to assert objects are NOT the same', message, {:illegal=>illegal, :actual=>actual} if illegal.equal? actual
363
- end
364
- end
365
-
366
- #
367
- # Assert that a constant is defined correctly in the correct class
368
- # * raises RubyUnit::AssertionFailure unless the constant is defined in
369
- # the specified class and it is the correct type and value
370
- # * raises ArgumentError if _konstant_ is not a string
371
- #
372
- # expected::
373
- # The value that is expected for the constant
374
- #
375
- # klass::
376
- # The class where the constant should be defined
377
- #
378
- # konstant::
379
- # The name of the constant
380
- #
381
- # message::
382
- # The message provided to be reported for a failure
383
- #
384
- # assertConst 42, Numbers, 'TWENTYFOUR', 'So dyslexic.' # => fail
385
- #
386
- def assertConst expected, klass, konstant, message = nil
387
- raise ArgumentError, 'Constant name must be given as a String' unless konstant.is_a? String
388
- __assertion do
389
- assertConstDefined klass, konstant, message
390
- value = klass.const_get konstant
391
- assertIsA expected.class, value, message
392
- assertEqual expected, value, message
393
- end
394
- end
395
-
396
- #
397
- # Assert that a constant is defined in the specified class
398
- # * raises RubyUnit::AssertionFailure unless the constant is defined in
399
- # the specified class
400
- # * raises ArgumentError if _konstant_ is not a string
401
- #
402
- # klass::
403
- # The class where the constant should be defined
404
- #
405
- # konstant::
406
- # The name of the constant
407
- #
408
- # message::
409
- # The message provided to be reported for a failure
410
- #
411
- # assertConstDefined Numbers, 'FORTYTWO', 'Mystery.' # => ??
412
- #
413
- def assertConstDefined klass, konstant, message = nil
414
- raise ArgumentError, 'Constant name must be given as a String' unless konstant.is_a? String
415
- __assertion do
416
- build_message 'Failed to assert constant is defined', message, {:klass=>klass, :konstant=>konstant} unless klass.const_defined? konstant
417
- end
418
- end
419
-
420
- #
421
- # Assert that a constant is not defined in the specified class
422
- # * raises RubyUnit::AssertionFailure if the constant is defined in
423
- # the specified class
424
- # * raises ArgumentError if _konstant_ is not a string
425
- #
426
- # klass::
427
- # The class where the constant should not be defined
428
- #
429
- # konstant::
430
- # The name of the constant
431
- #
432
- # message::
433
- # The message provided to be reported for a failure
434
- #
435
- # assertConstNotDefined Numbers, 'TWENTYFOUR', 'Mystery.' # => ??
436
- #
437
- def assertConstNotDefined klass, konstant, message = nil
438
- raise ArgumentError, 'Constant name must be given as a String' unless konstant.is_a? String
439
- __assertion do
440
- build_message 'Constant should not be defined', message, {:klass=>klass, :konstant=>konstant} if klass.const_defined? konstant
441
- end
442
- end
443
-
444
54
  class << self
445
- private
446
- # Tracks the total number of assertions made during the tests
447
- @@assertions = 0
448
-
449
55
  public
450
56
  #
451
57
  # Gets a list of all the descendents of the RubyUnit::TestCase class.
data/lib/RubyUnit.rb CHANGED
@@ -1,12 +1,11 @@
1
1
  #
2
- # = RubyUnit
3
2
  # A Simple Unit Test Framework for Ruby
4
3
  #
5
4
  # The RubyModule is the root object for all RubyUnit modules and classes.
6
5
  #
7
6
  module RubyUnit
8
7
  # Current RubyUnit version
9
- VERSION = '0.1.9'
8
+ VERSION = '0.2.10'
10
9
 
11
10
  #
12
11
  # RubyUnit::GemInfo contains data and functionality needed by the gem builder
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyunit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Clower
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-14 00:00:00.000000000 Z
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Unit testing and test-driven development are crucial parts ofthe software
14
14
  development life cycle. This tool is intended tomake development and testing in
@@ -28,6 +28,7 @@ files:
28
28
  - example/TestSet.rb
29
29
  - lib/RubyUnit.rb
30
30
  - lib/RubyUnit/AssertionFailure.rb
31
+ - lib/RubyUnit/Assertions.rb
31
32
  - lib/RubyUnit/Runner.rb
32
33
  - lib/RubyUnit/TestCase.rb
33
34
  - tests/TEST_GemInfo.rb