aboutsummaryrefslogtreecommitdiffstats
path: root/spec/support/random.rb
blob: 0ebc2ee5e50a455532007930c2e696b69f4f53b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module Support
  module Random

    PRETTY_LARGE_INT = 1 << 30 

    def random_hex
      SecureRandom.hex
    end

    def random_element from
      from[random_int(from.size)]
    end

    def random_int max_plus_one=PRETTY_LARGE_INT
      (random_number * max_plus_one).to_i
    end

    def random_number
      SecureRandom.random_number
    end

    def random_string
      SecureRandom.urlsafe_base64
    end

    def very_random(veryness=3, joiner: '-')
      raise ArgumentError, 'not very random' unless veryness > 1
      veryness.times.map{ SecureRandom.uuid }.join(joiner)
    end

  end
end

RSpec.configure do | c |
  c.include Support::Random
end