Module: Pbt::Arbitrary::ArbitraryMethods
- Included in:
- Pbt
- Defined in:
- lib/pbt/arbitrary/arbitrary_methods.rb
Instance Method Summary collapse
-
#alphanumeric_char ⇒ Arbitrary<String>
For an alphanumeric character.
-
#alphanumeric_string(**kwargs) ⇒ Arbitrary<String>
For alphanumeric strings.
-
#array(arb, min: 0, max: 10, empty: true) ⇒ Arbitrary<T>
For arrays of values generated by ‘arb`.
-
#ascii_char ⇒ Arbitrary<String>
For an ascii character.
-
#ascii_string(**kwargs) ⇒ Arbitrary<String>
For ascii strings.
-
#boolean ⇒ Arbitrary<Boolean>
For booleans.
-
#char ⇒ Arbitrary<String>
For a single unicode character (including printable and non-printable).
-
#choose(range) ⇒ Arbitrary<Integer>
Picks a random integer in the given range.
-
#constant(val) ⇒ Arbitrary<Object>
For any constant values.
-
#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`.
-
#fixed_hash(hash) ⇒ Arbitrary<Hash<Object, T>>
For fixed hashes of values generated by ‘hash`.
-
#float ⇒ Arbitrary<Float>
For floats.
-
#future_date(base_date: Date.today, future_offset_days: 18250) ⇒ Arbitrary<Date>
For future dates between ‘base_date` and `base_date - future_offset_days`.
-
#future_time(base_time: Time.now, future_offset_seconds: 1576800000) ⇒ Arbitrary<Time>
For future times between ‘base_time` and `base_time + future_offset_seconds`.
-
#hash(*args, **kwargs) ⇒ Arbitrary<Hash<T,U>>
For hashes of any keys and values.
-
#hexa ⇒ Arbitrary<String>
For a lowercase hexadecimal character.
-
#hexa_string(**kwargs) ⇒ Arbitrary<String>
For lowercase hexadecimal stings.
-
#integer(min: -1000000,, max: 1000000) ⇒ Arbitrary<Integer>
For integers between min (included) and max (included).
-
#nat(max: nil) ⇒ Arbitrary<Integer>
For natural numbers (non-negative integers) between 0 (included) and max (included).
-
#nil ⇒ Arbitrary<nil>
For nil.
-
#one_of(*choices) ⇒ Arbitrary<Object>
Picks a random element from the given choices.
-
#past_date(base_date: Date.today, past_offset_days: -18250)) ⇒ Arbitrary<Date>
For past dates between ‘base_date - past_offset_days` and `base_date`.
-
#past_time(base_time: Time.now, past_offset_seconds: -1576800000)) ⇒ Arbitrary<Time>
For past times between ‘base_time + past_offset_seconds` and `base_time`.
-
#printable_ascii_char ⇒ Arbitrary<String>
For a printable ascii character.
-
#printable_ascii_string(**kwargs) ⇒ Arbitrary<String>
For printable ascii strings.
-
#printable_char ⇒ Arbitrary<String>
For a printable character.
-
#printable_string(**kwargs) ⇒ Arbitrary<String>
For printable strings.
-
#set(arb, min: 0, max: 10, empty: true) ⇒ Arbitrary<T>
For symbols.
-
#symbol(**kwargs) ⇒ Arbitrary<Symbol>
For symbols.
-
#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`.
-
#tuple(*arbs) ⇒ Arbitrary<Array<...T>>
For tuples of values generated by ‘arbs`.
Instance Method Details
#alphanumeric_char ⇒ Arbitrary<String>
For an alphanumeric character.
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.
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`.
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_char ⇒ Arbitrary<String>
For an ascii character.
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.
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 |
#boolean ⇒ Arbitrary<Boolean>
For booleans.
225 226 227 |
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 225 def boolean one_of(true, false) end |
#char ⇒ Arbitrary<String>
For a single unicode character (including printable and non-printable).
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.
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.
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`.
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`.
65 66 67 |
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 65 def fixed_hash(hash) FixedHashArbitrary.new(hash) end |
#float ⇒ Arbitrary<Float>
For floats.
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`.
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`.
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.
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 |
#hexa ⇒ Arbitrary<String>
For a lowercase hexadecimal character.
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.
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).
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).
31 32 33 |
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 31 def nat(max: nil) integer(min: 0, max: max) end |
#nil ⇒ Arbitrary<nil>
For nil.
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.
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`.
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`
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_char ⇒ Arbitrary<String>
For a printable ascii character.
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.
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_char ⇒ Arbitrary<String>
For a printable character.
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.
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.
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.
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`.
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`.
53 54 55 |
# File 'lib/pbt/arbitrary/arbitrary_methods.rb', line 53 def tuple(*arbs) TupleArbitrary.new(*arbs) end |