Skip to content
Xavier Gouchet edited this page Aug 24, 2020 · 4 revisions

open class Forge

The base class to generate forgeries.

Constructors

<init>

Forge()

The base class to generate forgeries.

Properties

seed

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.

Functions

aBigInt

fun aBigInt(): Int

Return a strictly positive int, greater than #BIG_THRESHOLD

aBool

@JvmOverloads fun aBool(probability: Float= HALF_PROBABILITY):Boolean

Parameters

probability - the probability the boolean will be true (default 0.5f)

Return a boolean

aChar

@JvmOverloads fun aChar(min: Char= MIN_PRINTABLE, max:Char= MAX_UTF8):Char

Parameters

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

addFactory

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.

Parameters

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.

Parameters

T - the type the ForgeryFactory will be able to forge

clazz - the class of type T

forgeryFactory - the factory to be used

aDouble

@JvmOverloads fun aDouble(min: Double= -Double.MAX_VALUE, max:Double= Double.MAX_VALUE):Double

Parameters

min - the minimum value (inclusive), default = -Double#MAX_VALUE

max - the maximum value (exclusive), default = Double#MAX_VALUE

Exceptions

IllegalArgumentException - if min > max

Return a double between min and max

aFloat

@JvmOverloads fun aFloat(min: Float= -Float.MAX_VALUE, max:Float= Float.MAX_VALUE):Float

Parameters

min - the minimum value (inclusive), default = -Float#MAX_VALUE

max - the maximum value (exclusive), default = Float#MAX_VALUE

Exceptions

IllegalArgumentException - if min > max

Return a float between min and max

aGaussianDouble

@JvmOverloads fun aGaussianDouble(mean: Double= 0.0, standardDeviation:Double= 1.0):Double

Parameters

mean - the mean value of the distribution (default : 0.0)

standardDeviation - the standard deviation value of the distribution (default : 1.0)

Exceptions

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)

aGaussianFloat

@JvmOverloads fun aGaussianFloat(mean: Float= 0f, standardDeviation:Float= 1f):Float

Parameters

mean - the mean value of the distribution (default : 0.0f)

standardDeviation - the standard deviation value of the distribution (default : 1.0f)

Exceptions

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)

aGaussianInt

@JvmOverloads fun aGaussianInt(mean: Int= 0, standardDeviation:Int= DEFAULT_STDEV_INT):Int

Parameters

mean - the mean value of the distribution (default : 0)

standardDeviation - the standard deviation value of the distribution (default : 100)

Exceptions

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)

aGaussianLong

@JvmOverloads fun aGaussianLong(mean: Long= 0L, standardDeviation:Long= DEFAULT_STDEV_INT.toLong()):Long

Parameters

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)

aHugeInt

fun aHugeInt(): Int

Return a strictly positive int, greater than #HUGE_THRESHOLD

aKeyFrom

fun <K, V> aKeyFrom(map: Map<K, V>): K

Parameters

map - a non empty map

Return a key randomly picked in the map

aList

fun <T> aList(size: Int= -1, forging:Forge.() -> T): List<T>

Creates a random list.

Parameters

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

aLong

@JvmOverloads fun aLong(min: Long= Long.MIN_VALUE, max:Long= Long.MAX_VALUE):Long

Parameters

min - the minimum value (inclusive), default = Long.MIN_VALUE

max - the maximum value (exclusive), default = Long.MAX_VALUE

Exceptions

IllegalArgumentException - if min >= max

Return a long between min and max

aMap

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

Parameters

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

anAlphabeticalChar

@JvmOverloads fun anAlphabeticalChar(case: Case= Case.ANY):Char

Parameters

case - the case to use

Return an alpha character (from the roman alphabet), in the given case

anAlphabeticalString

@JvmOverloads fun anAlphabeticalString(case: Case= Case.LOWER, size:Int= -1):String

Parameters

case - the case to use

size - the size of the string (or -1 for a random sized String)

Return an alphabetical string

anAlphaNumericalChar

@JvmOverloads fun anAlphaNumericalChar(case: Case= Case.ANY):Char

Parameters

case - the case to use

Return an alphabetical or digit character, in the given case

anAlphaNumericalString

@JvmOverloads fun anAlphaNumericalString(case: Case= Case.LOWER, size:Int= -1):String

Parameters

case - the case to use

size - the size of the string (or -1 for a random sized String)

Return an alpha-numerical string

anAsciiChar

fun anAsciiChar(): Char

Return a Char within the standard ASCII printable characters

anAsciiString

@JvmOverloads fun anAsciiString(size: Int= -1):String

Parameters

size - the size of the string (or -1 for a random sized String)

Return a string containing only ASCII printable characters

aNegativeDouble

@JvmOverloads fun aNegativeDouble(strict: Boolean= true):Double

Parameters

strict - if true, then it will return a non 0 int (default : true)

Return a negative double

aNegativeFloat

@JvmOverloads fun aNegativeFloat(strict: Boolean= true):Float

Parameters

strict - if true, then it will return a non 0 int (default : true)

Return a negative float

aNegativeInt

@JvmOverloads fun aNegativeInt(strict: Boolean= true):Int

Parameters

strict - if true, then it will return a non 0 int (default : true)

Return a negative int

aNegativeLong

@JvmOverloads fun aNegativeLong(strict: Boolean= true):Long

Parameters

strict - if true, then it will return a non 0 long (default : true)

Return a negative long

anElementFrom

fun <T> anElementFrom(set: Set<T>): T

Parameters

set - a non empty Set

Return an element “randomly” picked in the set

fun <T> anElementFrom(list: List<T>): T

Parameters

list - a non empty List

Return an element “randomly” picked in the list

fun <T> anElementFrom(vararg array: T): T

Parameters

array - a non empty Array

Return an element “randomly” picked in the array

fun anElementFrom(array: BooleanArray): Boolean

Parameters

array - a non empty BooleanArray

Return an element “randomly” picked in the array

fun anElementFrom(array: CharArray): Char

Parameters

array - a non empty CharArray

Return an element “randomly” picked in the array

fun anElementFrom(array: IntArray): Int

Parameters

array - a non empty IntArray

Return an element “randomly” picked in the array

fun anElementFrom(array: LongArray): Long

Parameters

array - a non empty LongArray

Return an element “randomly” picked in the array

fun anElementFrom(array: FloatArray): Float

Parameters

array - a non empty FloatArray

Return an element “randomly” picked in the array

fun anElementFrom(array: DoubleArray): Double

Parameters

array - a non empty DoubleArray

Return an element “randomly” picked in the array

anEntryFrom

fun <K, V> anEntryFrom(map: Map<K, V>): Entry<K, V>

Parameters

map - a non empty Map

Return an element “randomly” picked in the set

anExtendedAsciiChar

fun anExtendedAsciiChar(): Char

Return a Char within the extended ASCII printable characters

anExtendedAsciiString

@JvmOverloads fun anExtendedAsciiString(size: Int= -1):String

Parameters

size - the size of the string (or -1 for a random sized String)

Return a string containing only extended ASCII printable characters

anHexadecimalChar

@JvmOverloads fun anHexadecimalChar(case: Case= Case.LOWER):Char

Parameters

case - the case to use

Return a digit (0 to F)

anHexadecimalString

@JvmOverloads fun anHexadecimalString(case: Case= Case.LOWER, size:Int= -1):String

Parameters

case - the case to use

size - the size of the string (or -1 for a random sized String)

Return an hexadecimal string

anInt

@JvmOverloads fun anInt(min: Int= Int.MIN_VALUE, max:Int= Int.MAX_VALUE):Int

Parameters

min - the minimum value (inclusive), default = Int#MIN_VALUE

max - the maximum value (exclusive), default = Int#MAX_VALUE

Exceptions

IllegalArgumentException - if min >= max

Return an int between min and max

aNullable

fun <reified T : Any> aNullable(): T?

Return either a value forged by an existing factory, or null (with 50% probability)

See Also

Forge.addFactory

fun <reified T : Any> aNullable(probability: Float): T?

Parameters

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

Forge.addFactory

@JvmOverloads fun <T : Any> aNullable(clazz: Class<T>, probability: Float = HALF_PROBABILITY): T?

Parameters

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

Forge.addFactory

fun <T : Any> aNullable(probability: Float= HALF_PROBABILITY, forging:Forge.() -> T): T?

Parameters

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)

aNumericalChar

fun aNumericalChar(): Char

Return a numerical (0 to 9)

aNumericalString

@JvmOverloads fun aNumericalString(size: Int= -1):String

Parameters

size - the size of the string (or -1 for a random sized String)

Return a numerical string

aPositiveDouble

@JvmOverloads fun aPositiveDouble(strict: Boolean= false):Double

Parameters

strict - if true, then it will return a non 0 int (default : false)

Return a positive double

aPositiveFloat

@JvmOverloads fun aPositiveFloat(strict: Boolean= false):Float

Parameters

strict - if true, then it will return a non 0 int (default : false)

Return a positive float

aPositiveInt

@JvmOverloads fun aPositiveInt(strict: Boolean= false):Int

Parameters

strict - if true, then it will return a non 0 int (default : false)

Return a positive int

aPositiveLong

@JvmOverloads fun aPositiveLong(strict: Boolean= false):Long

Parameters

strict - if true, then it will return a non 0 long (default : false)

Return a positive long

aSequence

fun <T> aSequence(size: Int= -1, forging:Forge.() -> T): Sequence<T>

Creates a random sequence.

Parameters

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

aSmallInt

fun aSmallInt(): Int

Return a strictly positive int, less than #SMALL_THRESHOLD

aString

@JvmOverloads fun aString(size: Int= -1, forging:Forge.() -> Char= { aChar() }):String

Creates a random String.

Parameters

size - the size of the String, or -1 for a random size

forging - a lambda generating chars that will fill the String

aStringMatching

fun aStringMatching(regex: String): String fun aStringMatching(regex: Regex): String

Parameters

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

aSubListOf

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.

Parameters

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

aSubSetOf

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.

Parameters

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.

aSubStringOf

fun aSubStringOf(string: String, outputSize: Int= -1):String

Parameters

string - the string from which a substring will be taken

Return a random sub string

fun aSubStringOf(forging: Forge.() -> String): String

Parameters

forging - a lambda generating a String from which a substring will be taken

Return a random sub string

aTinyInt

fun aTinyInt(): Int

Return a strictly positive int, less than #TINY_THRESHOLD

aValueFrom

fun <K, V> aValueFrom(map: Map<K, V>): V

Parameters

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

Parameters

enumClass - an Enum class

exclude - a list of enum constants to exclude from the values

Return an element “randomly” picked in the enum values

aWhitespaceChar

fun aWhitespaceChar(): Char

Return a whitespace character

aWhitespaceString

@JvmOverloads fun aWhitespaceString(size: Int= -1):String

Parameters

size - the size of the string (or -1 for a random sized String)

Return a string containing only whitespaces

getForgery

fun <reified T : Any> getForgery(): T

Parameters

T - the type of the instance to be forged

Exceptions

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

Parameters

T - the type of the instance to be forged

clazz - the class of type T

Exceptions

IllegalArgumentException - if no compatible factory exists

Return a new instance of type T, randomly forged with available factories

randomizeCase

fun randomizeCase(string: String): String

Randomizes the case of a String.

Parameters

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.

Parameters

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

shuffle

fun <T> shuffle(list: List<T>): List<T>

Shuffles the order if the elements in a list (like shuffling a deck of card).

Parameters

list - the list to shuffle

Return a new list with the same elements as the input, but in a random order

Companion Object Functions

seed

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.

Clone this wiki locally