-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kotlin: Extracted AnyForSubtypeOfTests
- Loading branch information
Showing
3 changed files
with
76 additions
and
54 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
74 changes: 74 additions & 0 deletions
74
kotlin/src/test/kotlin/net/jqwik/kotlin/AnyForSubtypeOfTests.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,74 @@ | ||
package net.jqwik.kotlin | ||
|
||
import net.jqwik.api.Arbitraries | ||
import net.jqwik.api.Example | ||
import net.jqwik.api.ForAll | ||
import net.jqwik.api.Property | ||
import net.jqwik.api.PropertyDefaults | ||
import net.jqwik.api.Provide | ||
import net.jqwik.kotlin.api.anyForSubtypeOf | ||
import net.jqwik.kotlin.api.anyForType | ||
import net.jqwik.kotlin.api.frequency | ||
import net.jqwik.kotlin.api.frequencyOf | ||
import net.jqwik.testing.SuppressLogging | ||
import net.jqwik.testing.TestingSupport | ||
import org.assertj.core.api.Assertions.assertThat | ||
import java.util.* | ||
|
||
@PropertyDefaults(tries = 100) | ||
class AnyForSubtypeOfTests { | ||
|
||
sealed interface Interface | ||
class Implementation(val value: String) : Interface | ||
|
||
@Example | ||
fun `anyForSubtypeOf() returns type arbitrary for any implementations of given sealed interface`(@ForAll random: Random) { | ||
val subtypes = anyForSubtypeOf<Interface>() | ||
TestingSupport.checkAllGenerated(subtypes, random) { it is Implementation } | ||
} | ||
|
||
sealed class Parent | ||
class Child(val value: String) : Parent() | ||
|
||
@Example | ||
fun `anyForSubtypeOf() returns type arbitrary for any implementations of given sealed class`(@ForAll random: Random) { | ||
val subtypes = anyForSubtypeOf<Parent>() | ||
TestingSupport.checkAllGenerated(subtypes, random) { it is Child } | ||
} | ||
|
||
sealed class ParentWithRecursion | ||
class ChildWithCustomType(val customType: CustomType) : ParentWithRecursion() | ||
class CustomType(val value: String) | ||
|
||
@Example | ||
fun `anyForSubtypeOf() with arbitrary recursion`(@ForAll random: Random) { | ||
val subtypes = anyForSubtypeOf<ParentWithRecursion>(enableArbitraryRecursion = true) | ||
TestingSupport.checkAllGenerated(subtypes, random) { it is ChildWithCustomType } | ||
} | ||
|
||
sealed interface ParentInterface | ||
sealed interface ChildInterface : ParentInterface | ||
sealed class ChildClass : ParentInterface | ||
class ChildInterfaceImpl(val value: String) : ChildInterface | ||
class ChildClassImpl(val value: String) : ChildClass() | ||
|
||
@Provide | ||
fun parentInterface() = anyForSubtypeOf<ParentInterface>() | ||
|
||
@Property | ||
fun `anyForSubtypeOf() returns type arbitrary for any concrete subtype of a given sealed class or interface, even nested`( | ||
@ForAll( | ||
"parentInterface" | ||
) parent: ParentInterface | ||
) { | ||
assertThat(parent).matches { it is ChildInterfaceImpl || it is ChildClassImpl } | ||
} | ||
|
||
@Example | ||
fun `anyForSubtypeOf() with type arbitrary customization`(@ForAll random: Random) { | ||
val subtypes = anyForSubtypeOf<Interface> { | ||
provide<Implementation> { Arbitraries.of(Implementation("custom arbitrary")) } | ||
} | ||
TestingSupport.checkAllGenerated(subtypes, random) { it is Implementation && it.value == "custom arbitrary" } | ||
} | ||
} |
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