assert_statistically 0.1.1 → 0.2.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.
data/CHANGELOG CHANGED
@@ -1,2 +1,23 @@
1
- v0.0.1 (2007-07-17)
1
+ v0.2.0 (2008-02-19)
2
+ * Please note that 0.2.0 is incompatible with 0.1.1. Notably, there is no longer
3
+ a :sleep option.
4
+ * Added some documentation to the code.
5
+ * Can now pass a :before Proc and an :after Proc. They will be passed the iteration number
6
+ and the number of successes thus far. (This is how to restore the removed :sleep option.)
7
+ * Removed the :sleep option
8
+ * Changed the default message from 'assert_block (%d) failed' to
9
+ 'Block returned false on iteration %d'
10
+ * Instead of an internal assert_block, the block is now called and the truth of the result is
11
+ used to determine success or failure.
12
+ * Now the test block can take two or zero parameters. If two, the first one is the iteration number,
13
+ and the second one is the number of successes thus far.
14
+ * Changed the global $counter to the instance @counter in the unit tests.
15
+ * If min is not set, it now defaults to 1.
16
+ * If max is not set, it now defaults to the value of "of."
17
+ * Added a :short_circuit option (defaulting to true) that will break out of the loop as soon as
18
+ the conditions are met or are impossible to meet.
19
+ * Removed shebangs.
20
+ * Changed original version from 0.0.1 to 0.1.1 to match the gemspec.
21
+
22
+ v0.1.1 (2007-07-17)
2
23
  * Birthday!
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2007 Zachary Holt
1
+ Copyright (c) 2007,2008 Zachary Holt
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README CHANGED
@@ -1,28 +1,8 @@
1
1
  Please see the MIT-LICENSE file for copyright and distribution information.
2
2
 
3
3
  assert_statistically is an addition to Test::Unit:TestCase that gives you a method
4
- called :assert_statistically, that wraps :assert_block, with some extra functionality.
4
+ called :assert_statistically, that works like :assert_block, with some extra functionality.
5
5
 
6
- assert_statistically( :min => 3, :max => 6, :of => 10 ) do
7
- some_method
8
- end
6
+ Please see the code's documentation for more information.
9
7
 
10
- The above snippet will run the block (which simply calls :some_method) 10 times. If
11
- it passes (as defined by assert_block) no fewer than 3 times and no more than 6 times,
12
- then the test "passes."
13
-
14
- :of defaults to 1
15
- :min defaults to :of
16
- :max defaults to 0 (0 is equal to no maximum)
17
-
18
- assert_statistically( :of => 10, :sleep => 3 ) do
19
- some_method
20
- end
21
-
22
- The above snippet will run the block 10 times, sleeping 3 second between each run.
23
-
24
- Note that if the :sleep key "points" to something that responds to :call (e.g.,
25
- a Proc object), that something will have :call invoked on it, with a single parameter:
26
- a Hash containing values for the keys :min, :max, :index (the current iteration), and
27
- :passes (the number of times the test has passed thus far). The result from that
28
- call will be passed to :sleep (so make it a number, eh?).
8
+ Please note that 0.2.0 is incompatible with 0.1.1. Notably, there is no longer a :sleep option.
@@ -1,46 +1,109 @@
1
1
  module Test
2
2
  module Unit
3
3
  module Assertions
4
- def assert_statistically( options={}, &blk )
4
+ ASSERT_STATISTICALLY_DEFAULT_FAILURE_MESSAGE = 'The block returned false on iteration %d'
5
+
6
+ # This method is used to determine whether we can short circuit and quit the
7
+ # iterations early. It is called once before the "before" block (if any), and
8
+ # once before the "after" block (if any). This means that if we can short circuit
9
+ # after the test block is called, but before the after block is called, we will.
10
+ # I.E., the after block will not be invoked in such a case.
11
+ def can_short_circuit?( range, completed, total, successes, short_circuit=true)
12
+ short_circuit && #first, be sure we can short circuit
13
+ (
14
+ # Are there enough successes, and is it impossible to get too many?
15
+ ( range.include?( successes ) && range.include?( total - completed + successes ) ) ||
16
+
17
+ # Are there too many successes?
18
+ successes > range.last ||
19
+
20
+ # Are there too few successes, and is it impossible to have enough?
21
+ total - completed + successes < range.first
22
+ )
23
+ end
24
+
25
+ # This is the main method of this library.
26
+ # It takes a hash of options and a block.
27
+ # Based on the options, the block will be run some number of times, possibly zero.
28
+ # If the criteria are met, assert_statistically is in a passing state.
29
+ # If assert_statistically enters a passing state and determines that it can never leave,
30
+ # it will be default short_circuit and run no more iterations. You can override this
31
+ # behavior by passing :short_circuit => false in the options Hash.
32
+ # Other keys uses in the options Hash
33
+ #
34
+ # :message => The message to report on failure. Takes a %d string format directive,
35
+ # which is populated with the failing iteration number.
36
+ # :of => The maximum number of times the test will be run (default = 1)
37
+ # :min => The minimum number of times the test needs to pass (default = 1)
38
+ # :max => The maximum number of times the test can pass (defaults to the value of of)
39
+ # :short_circuit => A flag determining whether to short circuit (defaults to true)
40
+ # :before => An optional Proc that will be called before each iteration
41
+ # :after => An optional Proc that will be called after each iteration
42
+ def assert_statistically( options={}, &test_block )
43
+ # If options is not a Hash, make it a hash, assuming that options.to_s is the message
5
44
  options = { :message => options.to_s } unless options.is_a?( Hash )
45
+
46
+ # The number of times to call the block; default is 1
6
47
  of = options[:of].to_i > 0 ? options[:of].to_i : 1
7
48
 
8
- max = options[:max].to_i > 0 ? options[:max].to_i : 0
9
- min = options[:min].to_i > 0 ? options[:min].to_i : 0
10
- min = of if min == 0 && max == 0 || min > of
11
- max = 0 if max > 0 && max < min
12
- sleep_time = options[:sleep]
13
-
14
- message = options[:message].to_s.empty? ? 'assert_block (%d) failed' : options[:message].to_s
49
+ # Set min ; default is 1 (when :min is not present)
50
+ # Note that if you pass a :min <= 0, the assertion will always pass,
51
+ # unless max is less than min
52
+ # Note that :min => nil is the same as :min => 0
53
+ # Note that !defined( :min ) is the same as :min => 1
54
+ min = options.has_key?( :min ) && options[:min].respond_to?( :to_i ) ? options[:min].to_i : 1
55
+
56
+ # Set max ; default is :of
57
+ # Note that you can pass a :max > :of, in which case
58
+ # :min will be the only determinant of success
59
+ # (since you can't have more than :of succcesses in :of attempts)
60
+ max = options.has_key?( :max ) && options[:max].respond_to?( :to_i ) ? options[:max].to_i : of
61
+
62
+ # Fail if min is greater than max
63
+ raise AssertionFailedError.new( "Impossible range: [#{min}..#{max}]" ) if min > max
64
+
65
+ # Set short_circuit ; default is true
66
+ short_circuit = options.has_key?( :short_circuit ) && !options[:short_circuit] ? false : true
67
+
68
+ # Create the range within which the number of successful runs must fall
69
+ successful_range = Range.new( min, max )
70
+
71
+ # Set up the before and after blocks
72
+ before = options.has_key?( :before ) && options[:before].respond_to?( :call ) ? options[:before] : false
73
+ after = options.has_key?( :after ) && options[:after].respond_to?( :call ) ? options[:after] : false
74
+
75
+ # Set up the failure message
76
+ message = options[:message].to_s.empty? ? ASSERT_STATISTICALLY_DEFAULT_FAILURE_MESSAGE : options[:message].to_s
77
+
15
78
  final_message = ''
16
- passes = 0
17
-
79
+ successes = 0
80
+
18
81
  1.upto( of ) do |i|
19
- begin
20
- assert_block nil, &blk
21
- passes += 1
22
- rescue AssertionFailedError => e
23
- final_message << "#{message % i}\n"
24
- end
25
-
26
- unless sleep_time.nil?
27
- if sleep_time.respond_to?( :call )
28
- sleep sleep_time.call( :index => i, :passes => passes, :of => of, :min => min, :max => max )
29
- elsif sleep_time.respond_to?( :to_f )
30
- sleep sleep_time.to_f
31
- end
32
- end
33
- end
82
+ # Check for short circuit eligibility (1 of 2)
83
+ break if can_short_circuit?( successful_range, i - 1, of, successes, short_circuit )
34
84
 
35
- if min > 0
36
- if max > 0
37
- raise AssertionFailedError.new( final_message ) if passes < min || passes > max
85
+ # Call any before block
86
+ before.call( i, successes ) if before
87
+
88
+ # Call the test block
89
+ if test_block.call( i, successes )
90
+ successes += 1
38
91
  else
39
- raise AssertionFailedError.new( final_message ) if passes < min
92
+ final_message << "#{message % i}\n"
40
93
  end
41
- else
42
- raise AssertionFailedError.new( final_message ) if passes > max
94
+
95
+ # Check for short circuit eligibility (2 of 2)
96
+ break if can_short_circuit?( successful_range, i, of, successes, short_circuit )
97
+
98
+ # Call any after block
99
+ after.call( i, successes ) if after
43
100
  end
101
+
102
+ # Fail if the number of successes is not in the successful range
103
+ raise AssertionFailedError.new( final_message ) unless successful_range.include?( successes )
104
+
105
+ # Pass
106
+ true
44
107
  end
45
108
  end
46
109
  end
data/test/all_tests.rb CHANGED
@@ -1,4 +1,3 @@
1
- #!/usr/local/bin/ruby
2
1
  dir = File.expand_path( File.dirname( __FILE__ ) )
3
2
 
4
3
  Dir[File.join( File.join( File.dirname( __FILE__ ), 'unit' ), "*_test.rb" )].each { |f| require f }
@@ -1,4 +1,3 @@
1
- #!/usr/local/bin/ruby
2
1
  dir = File.expand_path( File.dirname( __FILE__ ) )
3
2
 
4
3
  require 'test/unit'
@@ -6,169 +5,483 @@ require "#{File.expand_path( File.dirname( __FILE__ ) )}/test_setup"
6
5
 
7
6
  class AssertStatisticallyTest < Test::Unit::TestCase
8
7
  def setup
9
- $counter = 0
8
+ @counter = 0
10
9
  end
11
10
  def teardown
12
11
  end
13
12
 
14
- def test_assert_statistically_block
13
+ def test_with_short_circuit_without_min_without_max_pass
14
+ # short_circuit is true by default
15
+ assert_statistically( :of => 10 ) do
16
+ @counter += 1
17
+ true
18
+ end
19
+ assert_equal 1, @counter, "Should have gone through the assertions 1 time"
20
+ end
21
+
22
+ def test_with_short_circuit_without_min_without_max_fail
23
+ # short_circuit is true by default
24
+ begin
25
+ assert_statistically( :of => 10 ) do
26
+ @counter += 1
27
+ false
28
+ end
29
+ rescue Test::Unit::AssertionFailedError
30
+ else
31
+ flunk "Was supposed to fail because it did not have any successes"
32
+ end
33
+ assert_equal 10, @counter, "Should have gone through the assertions 10 times"
34
+ end
35
+
36
+ def test_with_short_circuit_without_min_with_max_pass
37
+ # short_circuit is true by default
38
+ assert_statistically( :of => 10, :max => 3 ) do
39
+ @counter += 1
40
+ @counter == 6
41
+ end
42
+
43
+ assert_equal 8, @counter, "Should have gone through the assertions 8 times, because we have to know that we won't exceed the max"
44
+ end
45
+
46
+ def test_with_short_circuit_without_min_with_max_fail
47
+ # short_circuit is true by default
48
+ begin
49
+ assert_statistically( :of => 10, :max => 3 ) do
50
+ @counter += 1
51
+ true
52
+ end
53
+ rescue Test::Unit::AssertionFailedError
54
+ else
55
+ flunk "Was supposed to fail because it had too many successes"
56
+ end
57
+
58
+ assert_equal 4, @counter, "Should have gone through the assertions 4 times"
59
+ end
60
+
61
+ def test_with_short_circuit_with_min_without_max_pass
62
+ # short_circuit is true by default
63
+ assert_statistically( :of => 10, :min => 4 ) do
64
+ @counter += 1
65
+ true
66
+ end
67
+
68
+ assert_equal 4, @counter, "Should have gone through the assertions 4 times"
69
+ end
70
+
71
+ def test_with_short_circuit_with_min_without_max_fail
72
+ # short_circuit is true by default
73
+ begin
74
+ assert_statistically( :of => 10, :min => 4 ) do
75
+ @counter += 1
76
+ false
77
+ end
78
+ rescue Test::Unit::AssertionFailedError
79
+ else
80
+ flunk "Was supposed to fail because it did not have enough successes"
81
+ end
82
+
83
+ assert_equal 7, @counter, "Should have gone through the assertions 7 times"
84
+ end
85
+
86
+ def test_with_short_circuit_with_min_with_max_pass
87
+ # short_circuit is true by default
88
+ assert_statistically( :of => 10, :min => 2, :max => 4 ) do
89
+ @counter += 1
90
+ [5,6,7].include?( @counter )
91
+ end
92
+
93
+ assert_equal 9, @counter, "Should have gone through the assertions 9 times"
94
+ end
95
+
96
+ def test_with_short_circuit_with_min_with_max_fail_too_few
97
+ # short_circuit is true by default
98
+ begin
99
+ assert_statistically( :of => 10, :min => 2, :max => 4 ) do
100
+ @counter += 1
101
+ 5 == @counter
102
+ end
103
+ rescue Test::Unit::AssertionFailedError
104
+ else
105
+ flunk "Was supposed to fail because it only had 1 success"
106
+ end
107
+
108
+ assert_equal 10, @counter, "Should have gone through the assertions 10 times"
109
+ end
110
+
111
+ def test_with_short_circuit_with_min_with_max_fail_too_many
112
+ # short_circuit is true by default
113
+ begin
114
+ assert_statistically( :of => 10, :min => 2, :max => 4 ) do
115
+ @counter += 1
116
+ true
117
+ end
118
+ rescue Test::Unit::AssertionFailedError
119
+ else
120
+ flunk "Was supposed to fail because it had 5 successes"
121
+ end
122
+
123
+ assert_equal 5, @counter, "Should have gone through the assertions 5 times"
124
+ end
125
+
126
+
127
+
128
+ def test_without_short_circuit_without_min_without_max_pass
129
+ # short_circuit is true by default
130
+ assert_statistically( :short_circuit => false, :of => 10 ) do
131
+ @counter += 1
132
+ true
133
+ end
134
+
135
+ assert_equal 10, @counter, "Should have gone through the assertions 10 times"
136
+ end
137
+
138
+ def test_without_short_circuit_without_min_without_max_fail
139
+ # short_circuit is true by default
140
+ begin
141
+ assert_statistically( :short_circuit => false, :of => 10 ) do
142
+ @counter += 1
143
+ false
144
+ end
145
+ rescue Test::Unit::AssertionFailedError
146
+ else
147
+ flunk "Was supposed to fail because it did not have any successes"
148
+ end
149
+
150
+ assert_equal 10, @counter, "Should have gone through the assertions 10 time"
151
+ end
152
+
153
+ def test_without_short_circuit_without_min_with_max_pass
154
+ # short_circuit is true by default
155
+ assert_statistically( :short_circuit => false, :of => 10, :max => 3 ) do
156
+ @counter += 1
157
+ @counter == 8
158
+ end
159
+
160
+ assert_equal 10, @counter, "Should have gone through the assertions 10 times"
161
+ end
162
+
163
+ def test_without_short_circuit_without_min_with_max_fail
164
+ # short_circuit is true by default
165
+ begin
166
+ assert_statistically( :short_circuit => false, :of => 10, :max => 3 ) do
167
+ @counter += 1
168
+ true
169
+ end
170
+ rescue Test::Unit::AssertionFailedError
171
+ else
172
+ flunk "Was supposed to fail because it had too many successes"
173
+ end
174
+
175
+ assert_equal 10, @counter, "Should have gone through the assertions 10 times"
176
+ end
177
+
178
+ def test_without_short_circuit_with_min_without_max_pass
179
+ # short_circuit is true by default
180
+ assert_statistically( :short_circuit => false, :of => 10, :min => 4 ) do
181
+ @counter += 1
182
+ true
183
+ end
184
+
185
+ assert_equal 10, @counter, "Should have gone through the assertions 10 times"
186
+ end
187
+
188
+ def test_without_short_circuit_with_min_without_max_fail
189
+ # short_circuit is true by default
190
+ begin
191
+ assert_statistically( :short_circuit => false, :of => 10, :min => 4 ) do
192
+ @counter += 1
193
+ false
194
+ end
195
+ rescue Test::Unit::AssertionFailedError
196
+ else
197
+ flunk "Was supposed to fail because it did not have enough successes"
198
+ end
199
+
200
+ assert_equal 10, @counter, "Should have gone through the assertions 10 times"
201
+ end
202
+
203
+ def test_without_short_circuit_with_min_with_max_pass
204
+ # short_circuit is true by default
205
+ assert_statistically( :short_circuit => false, :of => 10, :min => 2, :max => 4 ) do
206
+ @counter += 1
207
+ [5,6,7].include?( @counter )
208
+ end
209
+
210
+ assert_equal 10, @counter, "Should have gone through the assertions 10 times"
211
+ end
212
+
213
+ def test_without_short_circuit_with_min_with_max_fail_too_few
214
+ # short_circuit is true by default
215
+ begin
216
+ assert_statistically( :short_circuit => false, :of => 10, :min => 2, :max => 4 ) do
217
+ @counter += 1
218
+ 5 == @counter
219
+ end
220
+ rescue Test::Unit::AssertionFailedError
221
+ else
222
+ flunk "Was supposed to fail because it only had 1 success"
223
+ end
224
+
225
+ assert_equal 10, @counter, "Should have gone through the assertions 10 times"
226
+ end
227
+
228
+ def test_without_short_circuit_with_min_with_max_fail_too_many
229
+ # short_circuit is true by default
230
+ begin
231
+ assert_statistically( :short_circuit => false, :of => 10, :min => 2, :max => 4 ) do
232
+ @counter += 1
233
+ true
234
+ end
235
+ rescue Test::Unit::AssertionFailedError
236
+ else
237
+ flunk "Was supposed to fail because it had 5 successes"
238
+ end
239
+
240
+ assert_equal 10, @counter, "Should have gone through the assertions 10 times"
241
+ end
242
+
243
+ def test_block
15
244
  assert_statistically( :min => 10, :of => 10 ) do
16
- $counter += 1
245
+ @counter += 1
17
246
  true
18
247
  end
19
- assert_equal 10, $counter
248
+ assert_equal 10, @counter
249
+ end
250
+
251
+ def test_block_parameters
252
+ # Note that you can take the iteration we're on and the number of successes so
253
+ # far as parameters to the block
254
+ assert_statistically( :short_circuit => false, :of => 100 ) do |i,successes|
255
+ assert_equal successes, i / 2
256
+ i % 2 == 1
257
+ end
20
258
  end
21
259
 
22
- def test_assert_statistically_block_sleep
260
+ def test_sleep
23
261
  t1 = Time.now
24
- assert_statistically( :of => 5, :sleep => 0.5 ) do
25
- $counter += 1
262
+ assert_statistically( :short_circuit => false, :of => 5, :after => lambda { sleep 0.5 } ) do
263
+ @counter += 1
26
264
  true
27
265
  end
28
266
  t2 = Time.now
29
267
  assert_in_delta 2.5, t2.to_f - t1.to_f, 0.2
30
- assert_equal 5, $counter
268
+ assert_equal 5, @counter
269
+ end
31
270
 
32
- $counter = 0
33
- sleep_lambda = lambda { |o| $counter }
34
- t1 = Time.now
35
- assert_statistically( :of => 3, :sleep => sleep_lambda ) do
36
- $counter += 1
271
+ def test_after_lambda
272
+ success = true
273
+ assert_statistically( :short_circuit => false, :of => 10, :after => lambda { @counter += 1 } ) do |i, successes|
274
+ # @counter should lag the iteration, because we invoked an after Proc
275
+ success &= ( @counter == i - 1 )
37
276
  true
38
277
  end
39
- t2 = Time.now
40
- assert_in_delta 6, t2.to_f - t1.to_f, 0.2
41
- assert_equal 3, $counter
278
+ assert success
279
+ assert_equal 10, @counter
280
+ end
281
+
282
+ def test_before_lambda
283
+ success = true
284
+ assert_statistically( :short_circuit => false, :of => 10, :before => lambda { @counter += 1 } ) do |i, successes|
285
+ # @counter should equal the iteration, because we invoked a before Proc
286
+ success &= ( @counter == i )
287
+ true
288
+ end
289
+ assert success
290
+ assert_equal 10, @counter
42
291
  end
43
292
 
44
- def test_assert_statistically_block_defaults
293
+ def test_block_defaults
45
294
  assert_statistically do
46
- $counter += 1
295
+ @counter += 1
47
296
  end
48
- assert_equal 1, $counter
297
+ assert_equal 1, @counter
49
298
  end
50
-
51
- def test_assert_statistically_block_min
52
- assert_statistically( :min => 100 ) do
53
- $counter += 1
299
+
300
+
301
+ def test_min_defaults_to_one
302
+ assert_statistically( :of => 10 ) do
303
+ @counter += 1
304
+ true
305
+ end
306
+ assert_equal 1, @counter
307
+ end
308
+
309
+ def test_min_can_be_set_to_zero
310
+ assert_statistically( :of => 10, :min => 0 ) do
311
+ @counter += 1
312
+ true
54
313
  end
55
- assert_equal 1, $counter
314
+ assert_equal 0, @counter
315
+ end
56
316
 
317
+ def test_min_too_big_raises_exception
318
+ begin
319
+ assert_statistically( :min => 100, :of => 3 ) do
320
+ @counter += 1
321
+ end
322
+ rescue Test::Unit::AssertionFailedError => e
323
+ assert_match //, e.message
324
+ else
325
+ flunk "Should have raised an exception"
326
+ end
327
+ end
57
328
 
58
329
 
59
- $counter = 0
330
+ def test_block_min
60
331
  begin
61
332
  assert_statistically( :min => 2, :of => 5 ) do
62
- $counter += 1
63
- $counter < 3
333
+ @counter += 1
334
+ @counter < 3
64
335
  end
65
- rescue
336
+ rescue Test::Unit::AssertionFailedError
66
337
  flunk "Should not have failed"
67
338
  end
68
- assert_equal 5, $counter
339
+ assert_equal 2, @counter
69
340
 
70
341
 
71
- $counter = 0
342
+ @counter = 0
72
343
  begin
73
344
  assert_statistically( :min => 2, :of => 5 ) do
74
- $counter += 1
75
- $counter < 2
345
+ @counter += 1
346
+ @counter < 2
76
347
  end
77
- rescue
348
+ rescue Test::Unit::AssertionFailedError
78
349
  else
79
350
  flunk "Should have failed"
80
351
  end
81
- assert_equal 5, $counter
352
+ assert_equal 5, @counter
82
353
  end
83
354
 
84
355
 
85
- def test_assert_statistically_block_max
86
- assert_statistically( :max => 100 ) do
87
- $counter += 1
356
+ def test_block_max_defaults_to_of
357
+ assert_statistically( :max => 100, :of => 4, :short_circuit => false ) do
358
+ @counter += 1
88
359
  end
89
- assert_equal 1, $counter
360
+ assert_equal 4, @counter
361
+ end
90
362
 
363
+ def test_max_too_small_raises_exception
364
+ begin
365
+ assert_statistically( :max => -1, :of => 4 ) do
366
+ @counter += 1
367
+ end
368
+ rescue Test::Unit::AssertionFailedError
369
+ else
370
+ flunk "Should have failed"
371
+ end
372
+ end
91
373
 
92
- $counter = 0
374
+ def test_max
375
+ @counter = 0
93
376
  begin
94
377
  assert_statistically( :max => 3, :of => 5 ) do
95
- $counter += 1
96
- $counter < 3
378
+ @counter += 1
379
+ @counter < 3
97
380
  end
98
- rescue
381
+ rescue Test::Unit::AssertionFailedError
99
382
  flunk "Should not have failed"
100
383
  end
101
- assert_equal 5, $counter
384
+ assert_equal 4, @counter
102
385
 
103
386
 
104
- $counter = 0
387
+ @counter = 0
105
388
  begin
106
389
  assert_statistically( :max => 2, :of => 5 ) do
107
- $counter += 1
108
- $counter < 4
390
+ @counter += 1
391
+ @counter < 4
109
392
  end
110
- rescue
393
+ rescue Test::Unit::AssertionFailedError
111
394
  else
112
395
  flunk "Should have failed"
113
396
  end
114
- assert_equal 5, $counter
397
+ assert_equal 3, @counter
115
398
  end
116
399
 
117
-
118
- def test_assert_statistically_block_min_and_max
119
- assert_statistically( :max => 100, :min => 100 ) do
120
- $counter += 1
400
+ def test_block_min_and_max
401
+ begin
402
+ assert_statistically( :max => 100, :min => 100 ) do
403
+ @counter += 1
404
+ end
405
+ rescue Test::Unit::AssertionFailedError
406
+ else
407
+ flunk "Should have failed because the min is higher than of"
121
408
  end
122
- assert_equal 1, $counter
409
+ assert_equal 0, @counter
123
410
 
124
411
 
125
- $counter = 0
412
+ @counter = 0
126
413
  begin
127
414
  assert_statistically( :min => 2, :max => 3, :of => 5 ) do
128
- $counter += 1
129
- $counter == 1
415
+ @counter += 1
416
+ @counter == 1
130
417
  end
131
- rescue
418
+ rescue Test::Unit::AssertionFailedError
132
419
  else
133
- flunk "Should have failed"
420
+ flunk "Should have failed because @counter is one only once"
134
421
  end
135
- assert_equal 5, $counter
422
+ assert_equal 5, @counter
136
423
 
137
424
 
138
- $counter = 0
425
+ @counter = 0
139
426
  begin
140
427
  assert_statistically( :min => 2, :max => 3, :of => 5 ) do
141
- $counter += 1
142
- $counter < 5
428
+ @counter += 1
429
+ @counter < 5
143
430
  end
144
- rescue
431
+ rescue Test::Unit::AssertionFailedError
145
432
  else
146
- flunk "Should have failed"
433
+ flunk "Should have failed because it passes 4 times, which is greater than the max"
147
434
  end
148
- assert_equal 5, $counter
435
+ assert_equal 4, @counter
149
436
 
150
- $counter = 0
437
+ @counter = 0
151
438
  begin
152
439
  assert_statistically( :min => 2, :max => 3, :of => 5 ) do
153
- $counter += 1
154
- $counter < 3
440
+ @counter += 1
441
+ @counter < 3
155
442
  end
156
- rescue
443
+ rescue Test::Unit::AssertionFailedError
157
444
  flunk "Should not have failed"
158
445
  end
159
- assert_equal 5, $counter
446
+ assert_equal 4, @counter
160
447
  end
161
448
 
449
+ def test_block_failure_message
450
+ begin
451
+ assert_statistically( :of => 2, :min => 2 ) do
452
+ @counter += 1
453
+ @counter == 1
454
+ end
455
+ rescue Test::Unit::AssertionFailedError
456
+ assert_equal 'Test::Unit::AssertionFailedError', $!.class.name
457
+ assert_equal "The block returned false on iteration 2\n", $!.message
458
+ else
459
+ flunk "Was supposed to raise a Test::Unit::AssertionFailedError"
460
+ end
461
+
162
462
 
163
-
164
- def test_assert_statistically_block_failure
463
+ @counter = 0
464
+ begin
465
+ assert_statistically( :of => 2, :min => 2, :message => 'Number %d failed' ) do
466
+ @counter += 1
467
+ @counter == 1
468
+ end
469
+ rescue Test::Unit::AssertionFailedError
470
+ assert_equal 'Test::Unit::AssertionFailedError', $!.class.name
471
+ assert_equal "Number 2 failed\n", $!.message
472
+ else
473
+ flunk "Was supposed to raise a Test::Unit::AssertionFailedError"
474
+ end
475
+ end
476
+
477
+ def test_block_failure
165
478
  begin
166
479
  assert_statistically( :of => 2 ) do
167
480
  false
168
481
  end
169
- rescue
482
+ rescue Test::Unit::AssertionFailedError
170
483
  assert_equal 'Test::Unit::AssertionFailedError', $!.class.name
171
- assert_equal "assert_block (1) failed\nassert_block (2) failed\n", $!.message
484
+ assert_equal "The block returned false on iteration 1\nThe block returned false on iteration 2\n", $!.message
172
485
  else
173
486
  flunk "Was supposed to raise a Test::Unit::AssertionFailedError"
174
487
  end
@@ -1,4 +1,2 @@
1
- #!/usr/local/bin/ruby
2
-
3
1
  dir = File.expand_path( File::dirname( __FILE__ ) )
4
2
  require "#{dir}/../../lib/assert_statistically.rb"
metadata CHANGED
@@ -1,56 +1,63 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
3
- specification_version: 1
4
2
  name: assert_statistically
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
7
- date: 2007-07-17 00:00:00 -07:00
8
- summary: Unit::Test::Assertions addition that allows you to run assert_blocks many times in one pass
9
- require_paths:
10
- - lib
11
- email: z@wzph.com
12
- homepage:
13
- rubyforge_project: statistically
14
- description: assert_statistically is an addition to Unit::Test::Assertions that wraps assert_block. You can set a number of times to run the block, a minimum and maximum number of times that the block must pass, a message for failure, and an amount of time to sleep between each run of the block. Alternately, you can pass a Proc object to be called to determine the amount of time to sleep between each block. The Proc object will be passed a single Hash containing all the relevant information.
15
- autorequire: assert_statistically
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: false
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 0.2.0
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
- - Zachary Holt
31
- files:
32
- - lib/assert_statistically.rb
33
- - test/all_tests.rb
34
- - test/unit/assert_statistically_test.rb
35
- - test/unit/test_setup.rb
7
+ - Zach Holt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-02-19 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: assert_statistically is an addition to Unit::Test::Assertions that functions like assert_block. You can set a number of times to run the block, a minimum and maximum number of times that the block must pass, a message for failure, and a Proc to run before and after each run of the block.
17
+ email: z@wzph.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
36
23
  - README
37
24
  - MIT-LICENSE
38
25
  - CHANGELOG
39
- test_files:
26
+ files:
27
+ - lib/assert_statistically.rb
40
28
  - test/all_tests.rb
41
29
  - test/unit/assert_statistically_test.rb
42
30
  - test/unit/test_setup.rb
43
- rdoc_options: []
44
-
45
- extra_rdoc_files:
46
31
  - README
47
32
  - MIT-LICENSE
48
33
  - CHANGELOG
49
- executables: []
50
-
51
- extensions: []
34
+ has_rdoc: true
35
+ homepage: http://statistically.rubyforge.org
36
+ post_install_message:
37
+ rdoc_options: []
52
38
 
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
53
  requirements: []
54
54
 
55
- dependencies: []
56
-
55
+ rubyforge_project: statistically
56
+ rubygems_version: 1.0.1
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: Unit::Test::Assertions addition that allows you to run assert_blocks many times in one pass
60
+ test_files:
61
+ - test/all_tests.rb
62
+ - test/unit/assert_statistically_test.rb
63
+ - test/unit/test_setup.rb