assert_statistically 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ v0.0.1 (2007-07-17)
2
+ * Birthday!
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Zachary Holt
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,28 @@
1
+ Please see the MIT-LICENSE file for copyright and distribution information.
2
+
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.
5
+
6
+ assert_statistically( :min => 3, :max => 6, :of => 10 ) do
7
+ some_method
8
+ end
9
+
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?).
@@ -0,0 +1,47 @@
1
+ module Test
2
+ module Unit
3
+ module Assertions
4
+ def assert_statistically( options={}, &blk )
5
+ options = { :message => options.to_s } unless options.is_a?( Hash )
6
+ of = options[:of].to_i > 0 ? options[:of].to_i : 1
7
+
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
15
+ final_message = ''
16
+ passes = 0
17
+
18
+ 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
34
+
35
+ if min > 0
36
+ if max > 0
37
+ raise AssertionFailedError.new( final_message ) if passes < min || passes > max
38
+ else
39
+ raise AssertionFailedError.new( final_message ) if passes < min
40
+ end
41
+ else
42
+ raise AssertionFailedError.new( final_message ) if passes > max
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
data/test/all_tests.rb ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/local/bin/ruby
2
+ dir = File.expand_path( File.dirname( __FILE__ ) )
3
+
4
+ Dir[File.join( File.join( File.dirname( __FILE__ ), 'unit' ), "*_test.rb" )].each { |f| require f }
@@ -0,0 +1,176 @@
1
+ #!/usr/local/bin/ruby
2
+ dir = File.expand_path( File.dirname( __FILE__ ) )
3
+
4
+ require 'test/unit'
5
+ require "#{File.expand_path( File.dirname( __FILE__ ) )}/test_setup"
6
+
7
+ class AssertStatisticallyTest < Test::Unit::TestCase
8
+ def setup
9
+ $counter = 0
10
+ end
11
+ def teardown
12
+ end
13
+
14
+ def test_assert_statistically_block
15
+ assert_statistically( :min => 10, :of => 10 ) do
16
+ $counter += 1
17
+ true
18
+ end
19
+ assert_equal 10, $counter
20
+ end
21
+
22
+ def test_assert_statistically_block_sleep
23
+ t1 = Time.now
24
+ assert_statistically( :of => 5, :sleep => 0.5 ) do
25
+ $counter += 1
26
+ true
27
+ end
28
+ t2 = Time.now
29
+ assert_in_delta 2.5, t2.to_f - t1.to_f, 0.2
30
+ assert_equal 5, $counter
31
+
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
37
+ true
38
+ end
39
+ t2 = Time.now
40
+ assert_in_delta 6, t2.to_f - t1.to_f, 0.2
41
+ assert_equal 3, $counter
42
+ end
43
+
44
+ def test_assert_statistically_block_defaults
45
+ assert_statistically do
46
+ $counter += 1
47
+ end
48
+ assert_equal 1, $counter
49
+ end
50
+
51
+ def test_assert_statistically_block_min
52
+ assert_statistically( :min => 100 ) do
53
+ $counter += 1
54
+ end
55
+ assert_equal 1, $counter
56
+
57
+
58
+
59
+ $counter = 0
60
+ begin
61
+ assert_statistically( :min => 2, :of => 5 ) do
62
+ $counter += 1
63
+ $counter < 3
64
+ end
65
+ rescue
66
+ flunk "Should not have failed"
67
+ end
68
+ assert_equal 5, $counter
69
+
70
+
71
+ $counter = 0
72
+ begin
73
+ assert_statistically( :min => 2, :of => 5 ) do
74
+ $counter += 1
75
+ $counter < 2
76
+ end
77
+ rescue
78
+ else
79
+ flunk "Should have failed"
80
+ end
81
+ assert_equal 5, $counter
82
+ end
83
+
84
+
85
+ def test_assert_statistically_block_max
86
+ assert_statistically( :max => 100 ) do
87
+ $counter += 1
88
+ end
89
+ assert_equal 1, $counter
90
+
91
+
92
+ $counter = 0
93
+ begin
94
+ assert_statistically( :max => 3, :of => 5 ) do
95
+ $counter += 1
96
+ $counter < 3
97
+ end
98
+ rescue
99
+ flunk "Should not have failed"
100
+ end
101
+ assert_equal 5, $counter
102
+
103
+
104
+ $counter = 0
105
+ begin
106
+ assert_statistically( :max => 2, :of => 5 ) do
107
+ $counter += 1
108
+ $counter < 4
109
+ end
110
+ rescue
111
+ else
112
+ flunk "Should have failed"
113
+ end
114
+ assert_equal 5, $counter
115
+ end
116
+
117
+
118
+ def test_assert_statistically_block_min_and_max
119
+ assert_statistically( :max => 100, :min => 100 ) do
120
+ $counter += 1
121
+ end
122
+ assert_equal 1, $counter
123
+
124
+
125
+ $counter = 0
126
+ begin
127
+ assert_statistically( :min => 2, :max => 3, :of => 5 ) do
128
+ $counter += 1
129
+ $counter == 1
130
+ end
131
+ rescue
132
+ else
133
+ flunk "Should have failed"
134
+ end
135
+ assert_equal 5, $counter
136
+
137
+
138
+ $counter = 0
139
+ begin
140
+ assert_statistically( :min => 2, :max => 3, :of => 5 ) do
141
+ $counter += 1
142
+ $counter < 5
143
+ end
144
+ rescue
145
+ else
146
+ flunk "Should have failed"
147
+ end
148
+ assert_equal 5, $counter
149
+
150
+ $counter = 0
151
+ begin
152
+ assert_statistically( :min => 2, :max => 3, :of => 5 ) do
153
+ $counter += 1
154
+ $counter < 3
155
+ end
156
+ rescue
157
+ flunk "Should not have failed"
158
+ end
159
+ assert_equal 5, $counter
160
+ end
161
+
162
+
163
+
164
+ def test_assert_statistically_block_failure
165
+ begin
166
+ assert_statistically( :of => 2 ) do
167
+ false
168
+ end
169
+ rescue
170
+ assert_equal 'Test::Unit::AssertionFailedError', $!.class.name
171
+ assert_equal "assert_block (1) failed\nassert_block (2) failed\n", $!.message
172
+ else
173
+ flunk "Was supposed to raise a Test::Unit::AssertionFailedError"
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,4 @@
1
+ #!/usr/local/bin/ruby
2
+
3
+ dir = File.expand_path( File::dirname( __FILE__ ) )
4
+ require "#{dir}/../../lib/assert_statistically.rb"
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: assert_statistically
5
+ 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:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ 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
36
+ - README
37
+ - MIT-LICENSE
38
+ - CHANGELOG
39
+ test_files:
40
+ - test/all_tests.rb
41
+ - test/unit/assert_statistically_test.rb
42
+ - test/unit/test_setup.rb
43
+ rdoc_options: []
44
+
45
+ extra_rdoc_files:
46
+ - README
47
+ - MIT-LICENSE
48
+ - CHANGELOG
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ requirements: []
54
+
55
+ dependencies: []
56
+