-
-
Notifications
You must be signed in to change notification settings - Fork 3
Forge
open class Forge
The base class to generate forgeries.
Forge()
The base class to generate forgeries.
var seed:
Long
The seed that ensure reproducibility of anything generated by this Forge.
@JvmStatic fun seed():
Long
Creates a "random" seed based on the system clock.
fun aBigInt():
Int
Return a strictly positive int, greater than #BIG_THRESHOLD
@JvmOverloads fun aBool(probability:
Float
= HALF_PROBABILITY):
Boolean
probability
- the probability the boolean will be true (default 0.5f)
Return a boolean
@JvmOverloads fun aChar(min:
Char
= MIN_PRINTABLE, max:
Char
= MAX_UTF8):
Char
min
- the min char code to use (inclusive, default = 0x20 == space)
max
- the max char code to use (exclusive, default = 0xD800)
Return a Char within the given range
fun <reified T :
Any
> addFactory(forgeryFactory:
ForgeryFactory
<T>):
Unit
Adds a factory to the forge. This is the best way to extend a forge and provides means to create custom forgeries.
T
- the type the ForgeryFactory will be able to forge
forgeryFactory
- the factory to be usedfun <T :
Any
> addFactory(clazz:
Class
<T>, forgeryFactory:
ForgeryFactory
<T>):
Unit
Adds a factory to the forge. This is the best way to extend a forge and provides means to create custom forgeries.
T
- the type the ForgeryFactory will be able to forge
clazz
- the class of type T
forgeryFactory
- the factory to be used
@JvmOverloads fun aDouble(min:
Double
= -Double.MAX_VALUE, max:
Double
= Double.MAX_VALUE):
Double
min
- the minimum value (inclusive), default = -Double#MAX_VALUE
max
- the maximum value (exclusive), default = Double#MAX_VALUE
IllegalArgumentException
- if min > max
Return a double between min and max
@JvmOverloads fun aFloat(min:
Float
= -Float.MAX_VALUE, max:
Float
= Float.MAX_VALUE):
Float
min
- the minimum value (inclusive), default = -Float#MAX_VALUE
max
- the maximum value (exclusive), default = Float#MAX_VALUE
IllegalArgumentException
- if min > max
Return a float between min and max
@JvmOverloads fun aGaussianDouble(mean:
Double
= 0.0, standardDeviation:
Double
= 1.0):
Double
mean
- the mean value of the distribution (default : 0.0)
standardDeviation
- the standard deviation value of the distribution (default : 1.0)
IllegalArgumentException
- if the mean is outside of the range -1.8446743E19..1.8446743E19
(to avoid distribution imprecision); or if the standard deviation is negative
Return a double picked from a gaussian distribution (aka bell curve)
@JvmOverloads fun aGaussianFloat(mean:
Float
= 0f, standardDeviation:
Float
= 1f):
Float
mean
- the mean value of the distribution (default : 0.0f)
standardDeviation
- the standard deviation value of the distribution (default : 1.0f)
IllegalArgumentException
- if the mean is outside of the range -1.8446743E19..1.8446743E19
(to avoid distribution imprecision); or if the standard deviation is negative
Return a float picked from a gaussian distribution (aka bell curve)
@JvmOverloads fun aGaussianInt(mean:
Int
= 0, standardDeviation:
Int
= DEFAULT_STDEV_INT):
Int
mean
- the mean value of the distribution (default : 0)
standardDeviation
- the standard deviation value of the distribution (default : 100)
IllegalArgumentException
- if the mean is outside of the range -46340..46340
(to avoid distribution imprecision); or if the standard deviation is negative
Return an int picked from a gaussian distribution (aka bell curve)
@JvmOverloads fun aGaussianLong(mean:
Long
= 0L, standardDeviation:
Long
= DEFAULT_STDEV_INT.toLong()):
Long
mean
- the mean value of the distribution (default : 0)
standardDeviation
- the standard deviation value of the distribution (default : 100)
Return an long picked from a gaussian distribution (aka bell curve)
fun aHugeInt():
Int
Return a strictly positive int, greater than #HUGE_THRESHOLD
fun <K, V> aKeyFrom(map:
Map
<K, V>): K
map
- a non empty map
Return a key randomly picked in the map
fun <T> aList(size:
Int
= -1, forging:
Forge
.() -> T):
List
<T>
Creates a random list.
size
- the size of the list, or -1 for a random size
forging
- a lambda generating values that will fill the list
T
- The type of elements in the list
@JvmOverloads fun aLong(min:
Long
= Long.MIN_VALUE, max:
Long
= Long.MAX_VALUE):
Long
min
- the minimum value (inclusive), default = Long.MIN_VALUE
max
- the maximum value (exclusive), default = Long.MAX_VALUE
IllegalArgumentException
- if min >= max
Return a long between min and max
fun <K, V> aMap(size:
Int
= -1, forging:
Forge
.() ->
Pair
<K, V>):
Map
<K, V>
Returns a map with elements generated from the given lambda.
Note that the resulting map size might be smaller than the requested one if the forging lambda generates conflicting keys
size
- the size of the map, or -1 for a random size
forging
- a lambda generating a pair of key-value that will fill the map
K
- The type of keys in the map
V
- The type of values in the map
@JvmOverloads fun anAlphabeticalChar(case:
Case
= Case.ANY):
Char
case
- the case to use
Return an alpha character (from the roman alphabet), in the given case
@JvmOverloads fun anAlphabeticalString(case:
Case
= Case.LOWER, size:
Int
= -1):
String
case
- the case to use
size
- the size of the string (or -1 for a random sized String)
Return an alphabetical string
@JvmOverloads fun anAlphaNumericalChar(case:
Case
= Case.ANY):
Char
case
- the case to use
Return an alphabetical or digit character, in the given case
@JvmOverloads fun anAlphaNumericalString(case:
Case
= Case.LOWER, size:
Int
= -1):
String
case
- the case to use
size
- the size of the string (or -1 for a random sized String)
Return an alpha-numerical string
fun anAsciiChar():
Char
Return a Char within the standard ASCII printable characters
@JvmOverloads fun anAsciiString(size:
Int
= -1):
String
size
- the size of the string (or -1 for a random sized String)
Return a string containing only ASCII printable characters
@JvmOverloads fun aNegativeDouble(strict:
Boolean
= true):
Double
strict
- if true, then it will return a non 0 int (default : true)
Return a negative double
@JvmOverloads fun aNegativeFloat(strict:
Boolean
= true):
Float
strict
- if true, then it will return a non 0 int (default : true)
Return a negative float
@JvmOverloads fun aNegativeInt(strict:
Boolean
= true):
Int
strict
- if true, then it will return a non 0 int (default : true)
Return a negative int
@JvmOverloads fun aNegativeLong(strict:
Boolean
= true):
Long
strict
- if true, then it will return a non 0 long (default : true)
Return a negative long
fun <T> anElementFrom(set:
Set
<T>): T
set
- a non empty Set
Return an element “randomly” picked in the set
fun <T> anElementFrom(list:
List
<T>): T
list
- a non empty List
Return an element “randomly” picked in the list
fun <T> anElementFrom(vararg array: T): T
array
- a non empty Array
Return an element “randomly” picked in the array
fun anElementFrom(array:
BooleanArray
):
Boolean
array
- a non empty BooleanArray
Return an element “randomly” picked in the array
fun anElementFrom(array:
CharArray
):
Char
array
- a non empty CharArray
Return an element “randomly” picked in the array
fun anElementFrom(array:
IntArray
):
Int
array
- a non empty IntArray
Return an element “randomly” picked in the array
fun anElementFrom(array:
LongArray
):
Long
array
- a non empty LongArray
Return an element “randomly” picked in the array
fun anElementFrom(array:
FloatArray
):
Float
array
- a non empty FloatArray
Return an element “randomly” picked in the array
fun anElementFrom(array:
DoubleArray
):
Double
array
- a non empty DoubleArray
Return an element “randomly” picked in the array
fun <K, V> anEntryFrom(map:
Map
<K, V>):
Entry
<K, V>
map
- a non empty Map
Return an element “randomly” picked in the set
fun anExtendedAsciiChar():
Char
Return a Char within the extended ASCII printable characters
@JvmOverloads fun anExtendedAsciiString(size:
Int
= -1):
String
size
- the size of the string (or -1 for a random sized String)
Return a string containing only extended ASCII printable characters
@JvmOverloads fun anHexadecimalChar(case:
Case
= Case.LOWER):
Char
case
- the case to use
Return a digit (0 to F)
@JvmOverloads fun anHexadecimalString(case:
Case
= Case.LOWER, size:
Int
= -1):
String
case
- the case to use
size
- the size of the string (or -1 for a random sized String)
Return an hexadecimal string
@JvmOverloads fun anInt(min:
Int
= Int.MIN_VALUE, max:
Int
= Int.MAX_VALUE):
Int
min
- the minimum value (inclusive), default = Int#MIN_VALUE
max
- the maximum value (exclusive), default = Int#MAX_VALUE
IllegalArgumentException
- if min >= max
Return an int between min and max
fun <reified T :
Any
> aNullable(): T?
Return either a value forged by an existing factory, or null (with 50% probability)
See Also
fun <reified T :
Any
> aNullable(probability:
Float
): T?
probability
- the probability the result will be null (default 0.5f)
Return either a value forged by an existing factory, or null (with the given probability)
See Also
@JvmOverloads fun <T :
Any
> aNullable(clazz:
Class
<T>, probability:
Float
= HALF_PROBABILITY): T?
probability
- the probability the result will be null (default 0.5f)
clazz
- the class of the data to forge
Return either a value forged by an existing factory, or null (with the given probability)
See Also
fun <T :
Any
> aNullable(probability:
Float
= HALF_PROBABILITY, forging:
Forge
.() -> T): T?
probability
- the probability the result will be null (default 0.5f)
forging
- the lambda to forge a non null value
Return either a value forged by the lambda, or null (with the given probability)
fun aNumericalChar():
Char
Return a numerical (0 to 9)
@JvmOverloads fun aNumericalString(size:
Int
= -1):
String
size
- the size of the string (or -1 for a random sized String)
Return a numerical string
@JvmOverloads fun aPositiveDouble(strict:
Boolean
= false):
Double
strict
- if true, then it will return a non 0 int (default : false)
Return a positive double
@JvmOverloads fun aPositiveFloat(strict:
Boolean
= false):
Float
strict
- if true, then it will return a non 0 int (default : false)
Return a positive float
@JvmOverloads fun aPositiveInt(strict:
Boolean
= false):
Int
strict
- if true, then it will return a non 0 int (default : false)
Return a positive int
@JvmOverloads fun aPositiveLong(strict:
Boolean
= false):
Long
strict
- if true, then it will return a non 0 long (default : false)
Return a positive long
fun <T> aSequence(size:
Int
= -1, forging:
Forge
.() -> T):
Sequence
<T>
Creates a random sequence.
size
- the size of the sequence, or -1 for a random size
forging
- a lambda generating values that will fill the list
T
- The type of elements in the list
fun aSmallInt():
Int
Return a strictly positive int, less than #SMALL_THRESHOLD
@JvmOverloads fun aString(size:
Int
= -1, forging:
Forge
.() ->
Char
= { aChar() }):
String
Creates a random String.
size
- the size of the String, or -1 for a random size
forging
- a lambda generating chars that will fill the String
fun aStringMatching(regex:
String
):
String
fun aStringMatching(regex:
Regex
):
String
regex
-
a regular expression to drive the generation.
Note that parsing the regex can take some time depending on the regex complexity. Also not all regex feature are supported.
Return a String matching the given regular expression
fun <T> aSubListOf(list:
List
<T>, size:
Int
= -1):
List
<T>
Creates a sub list of the given list, with random elements selected from the input. The order of items in the result is not changed.
list
- the list to choose from
size
- the size of the result sublist. If the input set is smaller than the given size,
the result will have the size of the input set. If set to -1 (default) a random size is picked.
T
- The type of elements in the list
Return a non null list, with elements picked at random in the input, without duplicates. Note that if the input list contains duplicates, some might appear in the output. The order in the output matches the input order
fun <T> aSubSetOf(set:
Set
<T>, size:
Int
= -1):
Set
<T>
Creates a sub set of the given set, with random elements selected from the input.
set
- the set to choose from
size
- the size of the result subset. If the input set is smaller than the given size,
the result will have the size of the input set. If set to -1 (default) a random size is picked.
T
- The type of elements in the set
Return a non null set, with elements picked at random in the input, without duplicates.
fun aSubStringOf(string:
String
, outputSize:
Int
= -1):
String
string
- the string from which a substring will be taken
Return a random sub string
fun aSubStringOf(forging:
Forge
.() ->
String
):
String
forging
- a lambda generating a String from which a substring will be taken
Return a random sub string
fun aTinyInt():
Int
Return a strictly positive int, less than #TINY_THRESHOLD
fun <K, V> aValueFrom(map:
Map
<K, V>): V
map
- a non empty map
Return a key randomly picked in the map
@JvmOverloads fun <E :
Enum
<E>> aValueFrom(enumClass:
Class
<E>, exclude:
Collection
<E> = emptyList()): E
enumClass
- an Enum class
exclude
- a list of enum constants to exclude from the values
Return an element “randomly” picked in the enum values
fun aWhitespaceChar():
Char
Return a whitespace character
@JvmOverloads fun aWhitespaceString(size:
Int
= -1):
String
size
- the size of the string (or -1 for a random sized String)
Return a string containing only whitespaces
open fun equals(other:
Any
?):
Boolean
fun <reified T :
Any
> getForgery(): T
T
- the type of the instance to be forged
IllegalArgumentException
- if no compatible factory exists
Return a new instance of type T, randomly forged with available factories
fun <T :
Any
> getForgery(clazz:
Class
<T>): T
T
- the type of the instance to be forged
clazz
- the class of type T
IllegalArgumentException
- if no compatible factory exists
Return a new instance of type T, randomly forged with available factories
open fun hashCode():
Int
fun randomizeCase(string:
String
):
String
Randomizes the case of a String.
string
- the string to randomize
Return a new String with the same content, but any letter in it has a random case
fun randomizeCase(forging:
Forge
.() ->
String
):
String
Randomizes the case of a String.
forging
- a lambda generating a String that will be randomized
Return a new String with the same content, but any letter in it has a random case
fun <T> shuffle(list:
List
<T>):
List
<T>
Shuffles the order if the elements in a list (like shuffling a deck of card).
list
- the list to shuffle
Return a new list with the same elements as the input, but in a random order
open fun toString():
String
var seed:
Long
The seed that ensure reproducibility of anything generated by this Forge.
@JvmStatic fun seed():
Long
Creates a "random" seed based on the system clock.
Xavier F. Gouchet – @xgouchet
Distributed under the MIT license. See LICENSE.md for more information.
https://github.com/xgouchet/Elymr
- Home
- Getting Started
- Core Module
- Integrations
- Reference (core)
- Reference (junit4)
- Reference (junit5)
- Reference (spek)