Class: ConsistentRandom::Testing

Inherits:
Object
  • Object
show all
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.

Examples:

ConsistentRandom.testing.rand(foo: 0.5).bytes(foo: "foobar").seed(123) do
  expect(ConsistentRandom.new("foo").rand).to eq(0.5)
  expect(ConsistentRandom.new("bar").rand).not_to eq(0.5)

  expect(ConsistentRandom.new("foo").bytes(12)).to eq("foobarfoobar")

  expect(ConsistentRandom.new("foo").seed).to eq(123)
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTesting

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

.currentConsistentRandom::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.

Returns:



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.

Yields:

  • block of code to execute with the test values.

Returns:

  • (ConsistentRandom::Testing, Object)

    If a block is specified, then the result of the block is returned. Otherwise the testing object is returned so that you can chain calls to set up more test values.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/consistent_random/testing.rb', line 65

def bytes(options, &block)
  options = validate_bytes(options)
  unless options
    raise ArgumentError.new("Argument must be a String or a Hash with String values")
  end

  @bytes_hash = options.default ? options.merge(@bytes_hash) : @bytes_hash.merge(options)
  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.

Parameters:

  • name (String)

    the name of the ConsistentRandom object

Returns:

  • (String, nil)

    the test value for bytes if there is a test value for the name



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.

Yields:

  • block of code to execute with the test values.

Returns:

  • (ConsistentRandom::Testing, Object)

    If a block is specified, then the result of the block is returned. Otherwise the testing object is returned so that you can chain calls to set up more test values.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/consistent_random/testing.rb', line 42

def rand(options, &block)
  options = validate_rand(options)
  unless options
    raise ArgumentError.new("Argument must be a Float between 0 and 1 or a Hash with Float values")
  end

  @rand_hash = options.default ? options.merge(@rand_hash) : @rand_hash.merge(options)
  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.

Parameters:

  • name (String)

    the name of the ConsistentRandom object

Returns:

  • (Float, nil)

    the test value for rand if there is a test value for the name



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.

Yields:

  • block of code to execute with the test values.

Returns:

  • (ConsistentRandom::Testing, Object)

    If a block is specified, then the result of the block is returned. Otherwise the testing object is returned so that you can chain calls to set up more test



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/consistent_random/testing.rb', line 88

def seed(options, &block)
  options = validate_seed(options)
  unless options
    raise ArgumentError.new("Argument must be an Integer or a Hash with Integer values")
  end

  @seed_hash = options.default ? options.merge(@seed_hash) : @seed_hash.merge(options)
  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.

Parameters:

  • name (String)

    the name of the ConsistentRandom object

Returns:

  • (Integer, nil)

    the test value for seed if there is a test value for the name



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.

Yields:

  • block of code to execute with the test values.

Returns:

  • (Object)

    the result of the block



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