-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose Ranged- and SignedNumericArray API
- Loading branch information
Showing
23 changed files
with
1,495 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
core/src/commonMain/kotlin/tech/antibytes/kfixture/RangedNumericArrayFixture.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* ktlint-disable filename */ | ||
/* | ||
* Copyright (c) 2022 Matthias Geisler (bitPogo) / All rights reserved. | ||
* | ||
* Use of this source code is governed by Apache v2.0 | ||
*/ | ||
|
||
package tech.antibytes.kfixture | ||
|
||
@Throws(IllegalStateException::class) | ||
/** | ||
* Creates a value for a given type in given boundaries, excluding generics like List or Array. | ||
* @param FixtureType the type which is supposed to be created. | ||
* @param RangeType the type which is supposed to be created and is bounded to its boundaries. | ||
* @param from the lower boundary of the value. | ||
* @param to the upper boundary of the value. | ||
* @param size determines amount of items. | ||
* @param qualifier a optional qualifier for a special flavour of a type. | ||
* @throws IllegalStateException if the no matching Generator was found for the given type. | ||
*/ | ||
public inline fun <reified RangeType, reified FixtureType : RangeType?> PublicApi.Fixture.fixture( | ||
from: RangeType & Any, | ||
to: RangeType & Any, | ||
size: Int, | ||
qualifier: PublicApi.Qualifier? = null, | ||
): FixtureType { | ||
val returnNull = random.returnNull<FixtureType>() | ||
val id = resolveIdentifier<FixtureType>(qualifier) | ||
val generator = generators[id] | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
return when { | ||
generator !is PublicApi.RangedArrayGenerator<*, *> -> { | ||
throw IllegalStateException("Missing Generator for ClassID ($id).") | ||
} | ||
returnNull -> null as FixtureType | ||
else -> (generator as PublicApi.RangedArrayGenerator<Comparable<Any>, *>).generate( | ||
from = from as Comparable<Any>, | ||
to = to as Comparable<Any>, | ||
size = size, | ||
) as FixtureType | ||
} | ||
} | ||
|
||
@InternalAPI | ||
@PublishedApi | ||
internal inline fun <reified RangeType, reified FixtureType : RangeType?> PublicApi.Fixture.guardRangedReturn( | ||
qualifier: PublicApi.Qualifier? = null, | ||
resolve: PublicApi.RangedArrayGenerator<RangeType, *>.() -> FixtureType, | ||
): FixtureType where RangeType : Comparable<RangeType> { | ||
val returnNull = random.returnNull<FixtureType>() | ||
val id = resolveIdentifier<FixtureType>(qualifier) | ||
val generator = generators[id] | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
return when { | ||
generator !is PublicApi.RangedArrayGenerator<*, *> -> { | ||
throw IllegalStateException("Missing Generator for ClassID ($id).") | ||
} | ||
returnNull -> null as FixtureType | ||
else -> (generator as PublicApi.RangedArrayGenerator<RangeType, *>).resolve() | ||
} | ||
} | ||
|
||
@Throws(IllegalStateException::class) | ||
/** | ||
* Creates a value for a given type in a given range, excluding generics like List or Array. | ||
* @param FixtureType the type which is supposed to be created. | ||
* @param RangeType the type which is supposed to be created and is bounded to its boundaries. | ||
* @param ranges the lower boundary of the value. | ||
* @param size determines amount of items. | ||
* @param qualifier a optional qualifier for a special flavour of a type. | ||
* @throws IllegalStateException if the no matching Generator was found for the given type. | ||
*/ | ||
public inline fun <reified RangeType, reified FixtureType : RangeType?> PublicApi.Fixture.fixture( | ||
vararg ranges: ClosedRange<RangeType>, | ||
size: Int, | ||
qualifier: PublicApi.Qualifier? = null, | ||
): FixtureType where RangeType : Comparable<RangeType> = guardRangedReturn<RangeType, FixtureType>(qualifier) { | ||
this.generate( | ||
ranges = ranges, | ||
size = size, | ||
) as FixtureType | ||
} | ||
|
||
@Throws(IllegalStateException::class) | ||
/** | ||
* Creates a value for a given type in a given range, excluding generics like List or Array. | ||
* @param FixtureType the type which is supposed to be created. | ||
* @param RangeType the type which is supposed to be created and is bounded to its boundaries. | ||
* @param ranges the lower boundary of the value. | ||
* @param qualifier a optional qualifier for a special flavour of a type. | ||
* @throws IllegalStateException if the no matching Generator was found for the given type. | ||
*/ | ||
public inline fun <reified RangeType, reified FixtureType : RangeType?> PublicApi.Fixture.fixture( | ||
vararg ranges: ClosedRange<RangeType>, | ||
qualifier: PublicApi.Qualifier? = null, | ||
): FixtureType where RangeType : Comparable<RangeType> = guardRangedReturn<RangeType, FixtureType>(qualifier) { | ||
this.generate(ranges = ranges) as FixtureType | ||
} |
Oops, something went wrong.