Skip to content

Commit

Permalink
Fixed issues 1435 Add before and after test listeners to property ba… (
Browse files Browse the repository at this point in the history
…#1452)

* Fixed issues 1435  Add before and after test listeners to property base tests.
#1435

* Issue 1435 Change the listeners list to be immutable.
#1435

Co-authored-by: Atara Shachar <ashachar@atlassian.com>
  • Loading branch information
ataronet and atarashachar committed May 23, 2020
1 parent 21a88b1 commit f4044cd
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 3 deletions.
14 changes: 11 additions & 3 deletions kotest-property/src/commonMain/kotlin/io/kotest/property/config.kt
Expand Up @@ -16,7 +16,8 @@ data class PropTest(
val minSuccess: Int = Int.MAX_VALUE,
val maxFailure: Int = 0,
val shrinkingMode: ShrinkingMode = ShrinkingMode.Bounded(1000),
val iterations: Int? = null
val iterations: Int? = null,
val listeners: List<PropTestListener> = listOf()
)

fun PropTest.toPropTestConfig() =
Expand All @@ -25,13 +26,20 @@ fun PropTest.toPropTestConfig() =
minSuccess = minSuccess,
maxFailure = maxFailure,
iterations = iterations,
shrinkingMode = shrinkingMode
shrinkingMode = shrinkingMode,
listeners = listeners
)

data class PropTestConfig(
val seed: Long? = null,
val minSuccess: Int = Int.MAX_VALUE,
val maxFailure: Int = 0,
val shrinkingMode: ShrinkingMode = ShrinkingMode.Bounded(1000),
val iterations: Int? = null
val iterations: Int? = null,
val listeners: List<PropTestListener> = listOf()
)

interface PropTestListener {
suspend fun beforeTest(): Unit = Unit
suspend fun afterTest(): Unit = Unit
}
Expand Up @@ -23,9 +23,11 @@ suspend fun <A> proptest(
.take(iterations)
.forEach { a ->
val shrinkfn = shrinkfn<A>(a, property, config.shrinkingMode)
config.listeners.forEach { it.beforeTest() }
test(context, config, shrinkfn, listOf(a.value), random.seed) {
context.property(a.value)
}
config.listeners.forEach { it.afterTest() }
}
context.checkMaxSuccess(config, random.seed)
return context
Expand All @@ -51,9 +53,11 @@ suspend fun <A, B> proptest(
.take(iterations)
.forEach { (a, b) ->
val shrinkfn = shrinkfn<A, B>(a, b, property, config.shrinkingMode)
config.listeners.forEach { it.beforeTest() }
test(context, config, shrinkfn, listOf(a.value, b.value), random.seed) {
context.property(a.value, b.value)
}
config.listeners.forEach { it.afterTest() }
}

context.checkMaxSuccess(config, random.seed)
Expand Down Expand Up @@ -81,9 +85,11 @@ suspend fun <A, B, C> proptest(
.forEach { (ab, c) ->
val (a, b) = ab
val shrinkfn = shrinkfn<A, B, C>(a, b, c, property, config.shrinkingMode)
config.listeners.forEach { it.beforeTest() }
test(context, config, shrinkfn, listOf(a.value, b.value, c.value), random.seed) {
context.property(a.value, b.value, c.value)
}
config.listeners.forEach { it.afterTest() }
}

context.checkMaxSuccess(config, random.seed)
Expand Down Expand Up @@ -114,9 +120,11 @@ suspend fun <A, B, C, D> proptest(
val (ab, c) = abc
val (a, b) = ab
val shrinkfn = shrinkfn(a, b, c, d, property, config.shrinkingMode)
config.listeners.forEach { it.beforeTest() }
test(context, config, shrinkfn, listOf(a.value, b.value, c.value, d.value), random.seed) {
context.property(a.value, b.value, c.value, d.value)
}
config.listeners.forEach { it.afterTest() }
}

context.checkMaxSuccess(config, random.seed)
Expand Down Expand Up @@ -156,9 +164,11 @@ suspend fun <A, B, C, D, E> proptest(
val (ab, c) = abc
val (a, b) = ab
val shrinkfn = shrinkfn(a, b, c, d, e, property, config.shrinkingMode)
config.listeners.forEach { it.beforeTest() }
test(context, config, shrinkfn, listOf(a.value, b.value, c.value, d.value, e.value), random.seed) {
context.property(a.value, b.value, c.value, d.value, e.value)
}
config.listeners.forEach { it.afterTest() }
}
context.checkMaxSuccess(config, random.seed)
return context
Expand Down Expand Up @@ -200,9 +210,11 @@ suspend fun <A, B, C, D, E, F> proptest(
val (ab, c) = abc
val (a, b) = ab
val shrinkfn = shrinkfn(a, b, c, d, e, f, property, config.shrinkingMode)
config.listeners.forEach { it.beforeTest() }
test(context, config, shrinkfn, listOf(a.value, b.value, c.value, d.value, e.value, f.value), random.seed) {
context.property(a.value, b.value, c.value, d.value, e.value, f.value)
}
config.listeners.forEach { it.afterTest() }
}
context.checkMaxSuccess(config, random.seed)
return context
Expand Down
@@ -0,0 +1,126 @@
package com.sksamuel.kotest.property

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.property.*
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.string

class PropListenersTest : FunSpec({
var previous = -1
var current = 0
var total = 0

beforeTest {
previous = -1
current = 0
total = 0
}

val propConfig = PropTestConfig(listeners = listOf(object : PropTestListener {
override suspend fun beforeTest() {
previous = current
++current
}

override suspend fun afterTest() {
++total
}
}))

test("checkAll calls listener for param A") {
val context = checkAll(10, propConfig, Arb.string()) {
current shouldBe (previous + 1)
total shouldBe previous
}
previous shouldBe 9
current shouldBe 10
total shouldBe 10
}

test("checkAll calls listener for params A, B") {
val context = checkAll(
10,
propConfig,
Arb.string(),
Arb.int()
) { _, _ ->
current shouldBe (previous + 1)
total shouldBe previous
}
previous shouldBe 9
current shouldBe 10
total shouldBe 10
}

test("checkAll calls listener for params A, B, C") {
val context = checkAll(
10,
propConfig,
Arb.string(),
Arb.string(),
Arb.int()
) { _, _, _ ->
current shouldBe (previous + 1)
total shouldBe previous
}
previous shouldBe 9
current shouldBe 10
total shouldBe 10
}

test("checkAll calls listener for params A, B, C, D") {
val context = checkAll(
10,
propConfig,
Arb.string(),
Arb.int(),
Arb.string(),
Arb.int()
) { _, _, _, _ ->
current shouldBe (previous + 1)
total shouldBe previous
}
previous shouldBe 9
current shouldBe 10
total shouldBe 10
}

test("checkAll calls listener for params A, B, C, D, E") {
val context = checkAll(
10,
propConfig,
Arb.string(),
Arb.int(),
Arb.string(),
Arb.string(),
Arb.int()
) { _, _, _, _, _ ->
current shouldBe (previous + 1)
total shouldBe previous
}
previous shouldBe 9
current shouldBe 10
total shouldBe 10
}

test("checkAll calls listener for params A, B, C, D, E, F") {
val context = checkAll(
10,
propConfig,
Arb.string(),
Arb.int(),
Arb.string(),
Arb.int(),
Arb.string(),
Arb.int()
) { _, _, _, _, _, _ ->
current shouldBe (previous + 1)
total shouldBe previous
}
previous shouldBe 9
current shouldBe 10
total shouldBe 10
}
})

0 comments on commit f4044cd

Please sign in to comment.