Module: Gen::Test

Defined in:
lib/gen/test.rb,
lib/gen/test/version.rb

Constant Summary collapse

LOWER_BOUND =
ENV.fetch('TEST_LOWER_BOUND', 10).to_i
UPPER_BOUND =
ENV.fetch('TEST_UPPER_BOUND', 20).to_i
VERSION =
"0.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
# File 'lib/gen/test.rb', line 9

def self.included(base)
  base.include(Gen)
end

Instance Method Details

#for_all(*generators, &block) ⇒ Object



13
14
15
# File 'lib/gen/test.rb', line 13

def for_all(*generators, &block)
  for_n([LOWER_BOUND, UPPER_BOUND], *generators, &block)
end

#for_any(*generators, &block) ⇒ Object Also known as: for_one



17
18
19
# File 'lib/gen/test.rb', line 17

def for_any(*generators, &block)
  for_n(1, *generators, &block)
end

#for_each(*collections) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gen/test.rb', line 33

def for_each(*collections)
  unless collections.empty?
    size = collections.first.size
    raise "All collections must be the same size" unless collections.all? { |c| c.size == size }
    zipped = collections.first.zip(*collections.drop(1))
    if block_given?
      zipped.each do |generators|
        yield(*generators.map { |g| Gen.generate(g) })
      end
    else
      raise 'A block is expected with a property definition'
    end
  end
end

#for_n(n, *generators) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/gen/test.rb', line 22

def for_n(n, *generators)
  if block_given?
    n = Faker::Number.between(n.first, n.last) if n.respond_to?(:first) and n.respond_to?(:last)
    n.times do
      yield(*generators.map(&Gen.method(:generate)))
    end
  else
    raise 'A block is expected with a property definition'
  end
end