Module: Pbt::Arbitrary::ArbitraryMethods

Included in:
Pbt
Defined in:
lib/pbt/arbitrary/arbitrary_methods.rb

Instance Method Summary collapse

Instance Method Details

#alphanumeric_charArbitrary<String>

For an alphanumeric character.

Returns:



114
115
116
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 114

def alphanumeric_char
  one_of(*ALPHANUMERIC_CHARS)
end

#alphanumeric_string(**kwargs) ⇒ Arbitrary<String>

For alphanumeric strings.

Parameters:

  • kwargs (Hash)

    Options for ArrayArbitrary. See ‘.array` for more information.

Returns:

See Also:

  • Pbt.array


123
124
125
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 123

def alphanumeric_string(**kwargs)
  array(alphanumeric_char, **kwargs).map(STRING_MAPPER, STRING_UNMAPPER)
end

#array(arb, min: 0, max: 10, empty: true) ⇒ Arbitrary<T>

For arrays of values generated by ‘arb`.

Parameters:

  • arb (Arbitrary<T>)

    Arbitrary used to generate the values of the array.

  • min (Integer) (defaults to: 0)

    Lower limit of the generated array size.

  • max (Integer) (defaults to: 10)

    Upper limit of the generated array size.

  • empty (Boolean) (defaults to: true)

    Whether the array can be empty or not.

Returns:

Raises:

  • (ArgumentError)


42
43
44
45
46
47
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 42

def array(arb, min: 0, max: 10, empty: true)
  raise ArgumentError if min < 0
  min = 1 if min.zero? && !empty

  ArrayArbitrary.new(arb, min, max)
end

#ascii_charArbitrary<String>

For an ascii character.

Returns:



130
131
132
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 130

def ascii_char
  one_of(*ASCII_CHARS)
end

#ascii_string(**kwargs) ⇒ Arbitrary<String>

For ascii strings.

Parameters:

  • kwargs (Hash)

    Options for ArrayArbitrary. See ‘.array` for more information.

Returns:

See Also:

  • Pbt.array


139
140
141
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 139

def ascii_string(**kwargs)
  array(ascii_char, **kwargs).map(STRING_MAPPER, STRING_UNMAPPER)
end

#booleanArbitrary<Boolean>

For booleans.

Returns:



225
226
227
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 225

def boolean
  one_of(true, false)
end

#charArbitrary<String>

For a single unicode character (including printable and non-printable).

Returns:



107
108
109
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 107

def char
  choose(CHAR_RANGE).map(CHAR_MAPPER, CHAR_UNMAPPER)
end

#choose(range) ⇒ Arbitrary<Integer>

Picks a random integer in the given range.

Parameters:

  • range (Range<Integer>)

    Range of integers to choose from.

Returns:

See Also:

  • Pbt.one_of


74
75
76
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 74

def choose(range)
  ChooseArbitrary.new(range)
end

#constant(val) ⇒ Arbitrary<Object>

For any constant values. It’s useful when you want to use a constant value that behaves like an arbitrary.

Examples:

Pbt.constant(42).generate(Random.new) # => 42

Parameters:

  • val (Object)

Returns:



237
238
239
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 237

def constant(val)
  ConstantArbitrary.new(val)
end

#date(base_date: Date.today, past_offset_days: -18250,, future_offset_days: 18250) ⇒ Arbitrary<Date>

For dates between ‘base_date + past_offset_days` and `base_date + future_offset_days`.

Parameters:

  • base_date (Date) (defaults to: Date.today)

    Base date for the generated dates.

  • past_offset_days (Integer) (defaults to: -18250,)

    Offset days for the past. Default is -18250 (about 50 years).

  • future_offset_days (Integer) (defaults to: 18250)

    Offset days for the future. Default is 18250 (about 50 years).

Returns:



254
255
256
257
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 254

def date(base_date: Date.today, past_offset_days: -18250, future_offset_days: 18250)
  offset_arb = integer(min: past_offset_days, max: future_offset_days)
  offset_arb.map(DATE_MAPPER.call(base_date), DATE_UNMAPPER.call(base_date))
end

#fixed_hash(hash) ⇒ Arbitrary<Hash<Object, T>>

For fixed hashes of values generated by ‘hash`.

Examples:

arb = Pbt.fixed_hash(x: Pbt.integer, y: Pbt.integer)
arb.generate(Random.new) # => {x: -450108, y: 42}

Parameters:

  • hash (Hash<Object, Arbitrary<T>>)

    Hash with any keys and arbitraries as values.

Returns:



65
66
67
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 65

def fixed_hash(hash)
  FixedHashArbitrary.new(hash)
end

#floatArbitrary<Float>

For floats.

Returns:



187
188
189
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 187

def float
  tuple(integer, integer).map(FLOAT_MAPPER, FLOAT_UNMAPPER)
end

#future_date(base_date: Date.today, future_offset_days: 18250) ⇒ Arbitrary<Date>

For future dates between ‘base_date` and `base_date - future_offset_days`.

Parameters:

  • base_date (Date) (defaults to: Date.today)

    Base date for the generated dates.

  • future_offset_days (Integer) (defaults to: 18250)

    Offset days for the future. Default is 18250 (about 50 years).

Returns:



273
274
275
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 273

def future_date(base_date: Date.today, future_offset_days: 18250)
  date(base_date: base_date, past_offset_days: 0, future_offset_days: future_offset_days)
end

#future_time(base_time: Time.now, future_offset_seconds: 1576800000) ⇒ Arbitrary<Time>

For future times between ‘base_time` and `base_time + future_offset_seconds`.

Parameters:

  • base_time (Date) (defaults to: Time.now)

    Base time for the generated times.

  • future_offset_seconds (Integer) (defaults to: 1576800000)

    Offset seconds for the future. Default is 1576800000 (about 50 years).

Returns:



302
303
304
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 302

def future_time(base_time: Time.now, future_offset_seconds: 1576800000)
  time(base_time: base_time, past_offset_seconds: 0, future_offset_seconds: future_offset_seconds)
end

#hash(*args, **kwargs) ⇒ Arbitrary<Hash<T,U>>

For hashes of any keys and values. If you want to call ‘Object#hash` for `Pbt`, call this method without arguments.

Examples:

hash_generator = Pbt.hash(Pbt.symbol, Pbt.integer)
hash_generator.generate(Random.new) # => {:buo=>466214, :cwftzvglq=>331431, :wweccnzg=>-848867}

Parameters:

  • args (Array<Arbitrary<T,U>>)

    Arbitraries to generate Hash. First one is for key and second is for value.

  • kwargs (Hash)

    Options for ArrayArbitrary. See ‘.array` for more information.

Returns:

See Also:

  • Pbt.array


213
214
215
216
217
218
219
220
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 213

def hash(*args, **kwargs)
  if args.size == 2
    key_arbitrary, value_arbitrary = args
    array(tuple(key_arbitrary, value_arbitrary), **kwargs).map(HASH_MAPPER, HASH_UNMAPPER)
  else
    super # call `Object#hash`
  end
end

#hexaArbitrary<String>

For a lowercase hexadecimal character.

Returns:



91
92
93
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 91

def hexa
  one_of(*HEXA_CHARS)
end

#hexa_string(**kwargs) ⇒ Arbitrary<String>

For lowercase hexadecimal stings.

Parameters:

  • kwargs (Hash)

    Options for ArrayArbitrary. See ‘.array` for more information.

Returns:

See Also:

  • Pbt.array


100
101
102
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 100

def hexa_string(**kwargs)
  array(hexa, **kwargs).map(STRING_MAPPER, STRING_UNMAPPER)
end

#integer(min: -1000000,, max: 1000000) ⇒ Arbitrary<Integer>

For integers between min (included) and max (included).

Parameters:

  • min (Integer) (defaults to: -1000000,)

    Lower limit for the generated integers (included).

  • max (Integer) (defaults to: 1000000)

    Upper limit for the generated integers (included).

Returns:



23
24
25
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 23

def integer(min: -1000000, max: 1000000)
  IntegerArbitrary.new(min, max)
end

#nat(max: nil) ⇒ Arbitrary<Integer>

For natural numbers (non-negative integers) between 0 (included) and max (included).

Parameters:

  • max (Integer) (defaults to: nil)

    Upper limit for the generated integers (included).

Returns:



31
32
33
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 31

def nat(max: nil)
  integer(min: 0, max: max)
end

#nilArbitrary<nil>

For nil.

Returns:



244
245
246
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 244

def nil
  constant(nil)
end

#one_of(*choices) ⇒ Arbitrary<Object>

Picks a random element from the given choices. The choices can be of any type.

Parameters:

  • choices (Array<Object>)

    Array of choices.

Returns:

See Also:

  • Pbt.one_of


84
85
86
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 84

def one_of(*choices)
  OneOfArbitrary.new(choices)
end

#past_date(base_date: Date.today, past_offset_days: -18250)) ⇒ Arbitrary<Date>

For past dates between ‘base_date - past_offset_days` and `base_date`.

Parameters:

  • base_date (Date) (defaults to: Date.today)

    Base date for the generated dates.

  • past_offset_days (Integer) (defaults to: -18250))

    Offset days for the past. Default is -18250 (about 50 years).

Returns:



264
265
266
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 264

def past_date(base_date: Date.today, past_offset_days: -18250)
  date(base_date: base_date, past_offset_days: past_offset_days, future_offset_days: 0)
end

#past_time(base_time: Time.now, past_offset_seconds: -1576800000)) ⇒ Arbitrary<Time>

For past times between ‘base_time + past_offset_seconds` and `base_time`

Parameters:

  • base_time (Date) (defaults to: Time.now)

    Base time for the generated times.

  • past_offset_seconds (Integer) (defaults to: -1576800000))

    Offset seconds for the past. Default is -1576800000 (about 50 years).

Returns:



293
294
295
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 293

def past_time(base_time: Time.now, past_offset_seconds: -1576800000)
  time(base_time: base_time, past_offset_seconds: past_offset_seconds, future_offset_seconds: 0)
end

#printable_ascii_charArbitrary<String>

For a printable ascii character.

Returns:



146
147
148
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 146

def printable_ascii_char
  one_of(*PRINTABLE_ASCII_CHARS)
end

#printable_ascii_string(**kwargs) ⇒ Arbitrary<String>

For printable ascii strings.

Parameters:

  • kwargs (Hash)

    Options for ArrayArbitrary. See ‘.array` for more information.

Returns:

See Also:

  • Pbt.array


155
156
157
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 155

def printable_ascii_string(**kwargs)
  array(printable_ascii_char, **kwargs).map(STRING_MAPPER, STRING_UNMAPPER)
end

#printable_charArbitrary<String>

For a printable character.

Returns:



162
163
164
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 162

def printable_char
  one_of(*PRINTABLE_CHARS)
end

#printable_string(**kwargs) ⇒ Arbitrary<String>

For printable strings.

Parameters:

  • kwargs (Hash)

    Options for ArrayArbitrary. See ‘.array` for more information.

Returns:

See Also:

  • Pbt.array


171
172
173
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 171

def printable_string(**kwargs)
  array(printable_char, **kwargs).map(STRING_MAPPER, STRING_UNMAPPER)
end

#set(arb, min: 0, max: 10, empty: true) ⇒ Arbitrary<T>

For symbols.

Parameters:

  • arb (Arbitrary<T>)

    Arbitrary used to generate the values of the array.

  • min (Integer) (defaults to: 0)

    Lower limit of the generated set size.

  • max (Integer) (defaults to: 10)

    Upper limit of the generated set size.

  • empty (Boolean) (defaults to: true)

    Whether the array can be empty or not.

Returns:



198
199
200
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 198

def set(arb, min: 0, max: 10, empty: true)
  array(arb, min: min, max: max, empty: empty).map(SET_MAPPER, SET_UNMAPPER)
end

#symbol(**kwargs) ⇒ Arbitrary<Symbol>

For symbols.

Parameters:

  • kwargs (Hash)

    Options for ArrayArbitrary. See ‘.array` for more information.

Returns:

See Also:

  • Pbt.array


180
181
182
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 180

def symbol(**kwargs)
  array(one_of(*SYMBOL_SAFE_CHARS), empty: false, **kwargs).map(SYMBOL_MAPPER, SYMBOL_UNMAPPER)
end

#time(base_time: Time.now, past_offset_seconds: -1576800000,, future_offset_seconds: 1576800000) ⇒ Arbitrary<Time>

For times between ‘base_time + past_offset_seconds` and `base_time + future_offset_seconds`.

Parameters:

  • base_time (Date) (defaults to: Time.now)

    Base time for the generated times.

  • past_offset_seconds (Integer) (defaults to: -1576800000,)

    Offset seconds for the past. Default is -1576800000 (about 50 years).

  • future_offset_seconds (Integer) (defaults to: 1576800000)

    Offset seconds for the future. Default is 1576800000 (about 50 years).

Returns:



283
284
285
286
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 283

def time(base_time: Time.now, past_offset_seconds: -1576800000, future_offset_seconds: 1576800000)
  offset_arb = integer(min: past_offset_seconds, max: future_offset_seconds)
  offset_arb.map(TIME_MAPPER.call(base_time), TIME_UNMAPPER.call(base_time))
end

#tuple(*arbs) ⇒ Arbitrary<Array<...T>>

For tuples of values generated by ‘arbs`.

Parameters:

  • arbs (Array<Arbitrary<...T>>)

    Arbitraries used to generate the values of the tuple.

Returns:



53
54
55
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 53

def tuple(*arbs)
  TupleArbitrary.new(*arbs)
end