Class: ConsistentRandom::Testing
- Inherits:
-
Object
- Object
- ConsistentRandom::Testing
- Defined in:
- lib/consistent_random/testing.rb
Overview
This class returns an object that can be used to generate deterministic values for use in testing.
Class Method Summary collapse
-
.current ⇒ ConsistentRandom::Testing?
private
Get the testing object if any for the current block.
Instance Method Summary collapse
-
#bytes(options) { ... } ⇒ ConsistentRandom::Testing, Object
Set the random bytes returned by ConsistentRandom#bytes.
-
#bytes_for(name) ⇒ String?
private
Get the test value for bytes.
-
#initialize ⇒ Testing
constructor
A new instance of Testing.
-
#rand(options) { ... } ⇒ ConsistentRandom::Testing, Object
Set the random value returned by ConsistentRandom#rand.
-
#rand_for(name) ⇒ Float?
private
Get the test value for rand.
-
#seed(options) { ... } ⇒ ConsistentRandom::Testing, Object
Set the seed value returned by ConsistentRandom#seed.
-
#seed_for(name) ⇒ Integer?
private
Get the test value for seed.
-
#use { ... } ⇒ Object
Use the test values within a block of code.
Constructor Details
#initialize ⇒ Testing
Returns a new instance of Testing.
27 28 29 30 31 |
# File 'lib/consistent_random/testing.rb', line 27 def initialize @rand_hash = {} @bytes_hash = {} @seed_hash = {} end |
Class Method Details
.current ⇒ ConsistentRandom::Testing?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the testing object if any for the current block.
22 23 24 |
# File 'lib/consistent_random/testing.rb', line 22 def current Thread.current[:consistent_random_testing] end |
Instance Method Details
#bytes(options) { ... } ⇒ ConsistentRandom::Testing, Object
Set the random bytes returned by ConsistentRandom#bytes.
param options [String, Hash<String, String>] the value to return for bytes. If a String is given
then it will be used as the value for all calls to bytes. If a Hash is given then the value
will only be returned for ConsitentRandom objects with the names specified in the keys.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/consistent_random/testing.rb', line 65 def bytes(, &block) = validate_bytes() unless raise ArgumentError.new("Argument must be a String or a Hash with String values") end @bytes_hash = .default ? .merge(@bytes_hash) : @bytes_hash.merge() if block use(&block) else self end end |
#bytes_for(name) ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the test value for bytes.
130 131 132 |
# File 'lib/consistent_random/testing.rb', line 130 def bytes_for(name) @bytes_hash[name] end |
#rand(options) { ... } ⇒ ConsistentRandom::Testing, Object
Set the random value returned by ConsistentRandom#rand.
param options [Float, Hash<String, Float>] the value to return for rand. If a Float is given
then it will be used as the value for all calls to rand. If a Hash is given then the value
will only be returned for ConsitentRandom objects with the names specified in the keys.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/consistent_random/testing.rb', line 42 def rand(, &block) = validate_rand() unless raise ArgumentError.new("Argument must be a Float between 0 and 1 or a Hash with Float values") end @rand_hash = .default ? .merge(@rand_hash) : @rand_hash.merge() if block use(&block) else self end end |
#rand_for(name) ⇒ Float?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the test value for rand.
121 122 123 |
# File 'lib/consistent_random/testing.rb', line 121 def rand_for(name) @rand_hash[name] end |
#seed(options) { ... } ⇒ ConsistentRandom::Testing, Object
Set the seed value returned by ConsistentRandom#seed.
param options [Integer, Hash<String, Integer>] the value to return for seed. If an Integer is given
then it will be used as the value for all calls to seed. If a Hash is given then the value
will only be returned for ConsitentRandom objects with the names specified in the keys.
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/consistent_random/testing.rb', line 88 def seed(, &block) = validate_seed() unless raise ArgumentError.new("Argument must be an Integer or a Hash with Integer values") end @seed_hash = .default ? .merge(@seed_hash) : @seed_hash.merge() if block use(&block) else self end end |
#seed_for(name) ⇒ Integer?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the test value for seed.
139 140 141 |
# File 'lib/consistent_random/testing.rb', line 139 def seed_for(name) @seed_hash[name] end |
#use { ... } ⇒ Object
Use the test values within a block of code.
106 107 108 109 110 111 112 113 114 |
# File 'lib/consistent_random/testing.rb', line 106 def use(&block) save_val = Thread.current[:consistent_random_testing] begin Thread.current[:consistent_random_testing] = self yield ensure Thread.current[:consistent_random_testing] = save_val end end |