assert-random 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,16 +1,39 @@
1
1
  = assert-random
2
2
 
3
- Adds an assert_random assertion to test/unit
4
-
5
- == Note on Patches/Pull Requests
6
-
7
- * Fork the project.
8
- * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a
10
- future version unintentionally.
11
- * Commit, do not mess with rakefile, version, or history.
12
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
- * Send me a pull request. Bonus points for topic branches.
3
+ Assert random is a simple gem that extends the test/unit assertions to include an assert-random test. Assert random works like any other assertion and accepts a block of code. Assert random checks this block of code records the results of the output. The out put is then checked for the following...
4
+
5
+ * Identical results
6
+ * Sequences of numbers (eg... 1 2 3 4 or... 10 20 30 etc)
7
+
8
+ Usage(Will pass):
9
+
10
+ def test_something
11
+ assert_random do
12
+ rand(1000)
13
+ end
14
+ end
15
+
16
+ Usage(Will fail):
17
+
18
+ def test_something_else
19
+ assert_random do
20
+ 1000
21
+ end
22
+ end
23
+
24
+ == Tolerance
25
+
26
+ Random numbers and results are hard to test. If your generating with in a small range of numbers say 1 to 10 then you can't guarantee that the same number will not come up twice. This is despite the generator working correctly. For cases like this assert-random supports the tolerance option. Tolerance allows the test to tolerate more than one instance of a number from a narrow range.
27
+
28
+ Tolerance Usage:
29
+
30
+ def test_tolerance
31
+ assert_random :tolerance => 5 do
32
+ rand(10)
33
+ end
34
+ end
35
+
36
+ The above example will pass even if the same number comes up 4 times.
14
37
 
15
38
  == Copyright
16
39
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.1
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{assert-random}
8
- s.version = "0.2.1"
8
+ s.version = "0.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Stewart Matheson"]
data/lib/assert-random.rb CHANGED
@@ -1,26 +1,35 @@
1
1
  require 'test/unit'
2
-
3
2
  module AssertRandom
4
3
  module AssertionMixins
5
4
 
6
- def assert_random
5
+
6
+ def assert_random(options = {})
7
7
  _wrap_assertion do
8
+ assert_block("assert needs to be called with a block.") { block_given? }
9
+ configuration = { :tolerance => 1 }
10
+ configuration.update(options) if options.is_a?(Hash)
11
+
8
12
  results = Array.new
9
13
  10.times do
10
14
  results.push yield
11
15
  end
12
- assert_block("2 or more values match") { !same_values? results }
16
+
17
+ assert_block("#{configuration[:tolerance]} or more values match") { !same_values? results, configuration[:tolerance] }
13
18
  assert_block("Results are in a sequence") { !in_sequence? results }
14
19
  end
15
20
  end
16
21
 
17
22
 
18
23
  private
19
- def same_values?(results)
24
+ def same_values?(results, tolerance)
25
+ same_value_counter = 0
20
26
  results.each do
21
27
  last_result = results.shift
22
28
  results.each do |r|
23
- return true if(last_result == r)
29
+ if(last_result == r)
30
+ same_value_counter += 1
31
+ return true if same_value_counter > tolerance
32
+ end
24
33
  end
25
34
  end
26
35
  false
@@ -62,5 +62,42 @@ class TestAssertRandom < Test::Unit::TestCase
62
62
  end
63
63
  end
64
64
  end
65
+
66
+
67
+ def test_tolerance_with_random
68
+ assert_nothing_raised do
69
+ assert_random :tolerance => 5 do
70
+ rand(1000)
71
+ end
72
+ end
73
+ end
74
+
75
+
76
+ def test_tolerance_with_fixed_number_set
77
+ numbers = Array.new
78
+
79
+ 2.times do
80
+ numbers.push 10
81
+ end
82
+
83
+ 8.times do
84
+ numbers.push rand(9)
85
+ end
86
+
87
+ i = -1
88
+ assert_raise Test::Unit::AssertionFailedError do
89
+ assert_random :tolerance => 2 do
90
+ i =+ 1
91
+ numbers[i]
92
+ end
93
+ end
94
+ end
65
95
 
96
+ def test_must_pass_block
97
+ assert_raise Test::Unit::AssertionFailedError do
98
+ assert_random
99
+ end
100
+ end
101
+
102
+
66
103
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assert-random
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 1
10
- version: 0.2.1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Stewart Matheson