Class: RandomHash

Inherits:
Hash
  • Object
show all
Extended by:
RushCheck::Arbitrary
Includes:
RushCheck::Coarbitrary
Defined in:
lib/rushcheck/hash.rb

Overview

RandomHash is a subclass of Hash which provides a random generator of hash. Programmer can make a subclass of RandomHash to make user defined random generator of Hash.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RushCheck::Arbitrary

arbitrary

Class Method Details

.arbitraryObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rushcheck/hash.rb', line 26

def self.arbitrary
  RushCheck::Gen.new do |n, r|
    h = {}
    r2 = r
    @@pat.keys.each do |k|
      r1, r2 = r2.split
      h[k] = @@pat[k].arbitrary.value(n, r1)
    end
    h
  end
end

.set_pattern(pat) ⇒ Object

class method set_pattern takes a hash object of random pattern. For example, the following pattern

pat = { 'key1' => Integer, 'key2' => String }

means that an arbitrary hash is randomly generated which has two keys (say ‘key1’ and ‘key2’) and has indicated random object as its values.



21
22
23
24
# File 'lib/rushcheck/hash.rb', line 21

def self.set_pattern(pat)
  @@pat = pat
  self
end

Instance Method Details

#coarbitrary(g) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/rushcheck/hash.rb', line 38

def coarbitrary(g)
  r = g.variant(0)
  values.each do |c|
    r = c.coarbitrary(r.variant(1))
  end
  r
end