0.0
No commit activity in last 3 years
No release in over 3 years
This gem adds an assert random assertion that will allow the testing of random sequences. This is handy for game development and anyone working with random numbers with out a way of testing them.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

assert-random¶ ↑

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…

  • Identical results

  • Sequences of numbers (eg… 1 2 3 4 or… 10 20 30 etc)

Usage(Will pass):

def test_something
   assert_random do
      rand(1000)
   end
end

Usage(Will fail):

def test_something_else
   assert_random do
      1000
   end
end

Tolerance¶ ↑

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.

Tolerance Usage:

def test_tolerance
   assert_random :tolerance => 5 do
      rand(10)
   end
end

The above example will pass even if the same number comes up 4 times.

Iterations¶ ↑

You can now set the amount of times that the passed block will iterate. This is handy when testing numbers from different ranges. It is used like so. Warning: assert random will throw exceptions if you pass 1. There is no point in only iterating once.

def test_iterations
   assert_random :iterations => 2 do
      rand(10)
   end
end

Copyright © 2010 Stewart Matheson. See LICENSE for details.