Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes by create-pull-request action #343

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion convention-plugins/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ com-github-ben-manes-gradle-versions-plugin = "com.github.ben-manes:gradle-versi
org-jetbrains-kotlin-kotlin-bom = { module = "org.jetbrains.kotlin:kotlin-bom", version.ref = "org-jetbrains-kotlin" }
org-jetbrains-kotlin-kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "org-jetbrains-kotlin" }
org-jetbrains-kotlin-kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "org-jetbrains-kotlin" }
org-jmailen-gradle-kotlinter-gradle = "org.jmailen.gradle:kotlinter-gradle:4.4.1"
org-jmailen-gradle-kotlinter-gradle = "org.jmailen.gradle:kotlinter-gradle:4.5.0"

[plugins]
com-github-ben-manes-versions = "com.github.ben-manes.versions:0.51.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ interface ActionCannon<out D> {
suspend fun <R> fire(action: SuspendAction<D, R>): R

companion object {
operator fun <D> invoke(dispatcher: D, pipe: ActionPipe = ActionPipe): ActionCannon<D> =
DispatcherPipeCannon(dispatcher, pipe)
operator fun <D> invoke(dispatcher: D, pipe: ActionPipe = ActionPipe): ActionCannon<D> = DispatcherPipeCannon(dispatcher, pipe)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,26 @@ class GeneralSuspendActionDispatcherTest {
}

@Test
fun syntaxAllowsInterceptionOfActionExecutionIncludingReplacingResult() =
asyncSetup(object : GeneralSuspendActionDispatcherSyntax {
val expectedReplacedResult = 127
override val generalDispatcher = generalDispatcherSpy().apply { spyWillReturn(expectedReplacedResult) }
val action = DivideAction(6, 7)
val divideDispatcherSpy = SpyData<DivideAction, Int>()
val divideDispatcher: DivideActionDispatcher = { divideDispatcherSpy.spyFunction(it) }
}) exercise {
divideDispatcher.execute(action)
} verify { result ->
result.assertIsEqualTo(expectedReplacedResult)
generalDispatcher.spyReceivedValues
.assertIsEqualTo(listOf(action to divideDispatcher))
divideDispatcherSpy.spyReceivedValues
.assertIsEqualTo(emptyList<Any>())
}
fun syntaxAllowsInterceptionOfActionExecutionIncludingReplacingResult() = asyncSetup(object : GeneralSuspendActionDispatcherSyntax {
val expectedReplacedResult = 127
override val generalDispatcher = generalDispatcherSpy().apply { spyWillReturn(expectedReplacedResult) }
val action = DivideAction(6, 7)
val divideDispatcherSpy = SpyData<DivideAction, Int>()
val divideDispatcher: DivideActionDispatcher = { divideDispatcherSpy.spyFunction(it) }
}) exercise {
divideDispatcher.execute(action)
} verify { result ->
result.assertIsEqualTo(expectedReplacedResult)
generalDispatcher.spyReceivedValues
.assertIsEqualTo(listOf(action to divideDispatcher))
divideDispatcherSpy.spyReceivedValues
.assertIsEqualTo(emptyList<Any>())
}

private fun generalDispatcherSpy() = object :
GeneralSuspendActionDispatcher,
Spy<Pair<SuspendAction<*, *>, *>, Any> by SpyData() {
@Suppress("UNCHECKED_CAST")
override suspend fun <D, R> dispatch(action: SuspendAction<D, R>, dispatcher: D): R =
spyFunction(action to dispatcher) as R
override suspend fun <D, R> dispatch(action: SuspendAction<D, R>, dispatcher: D): R = spyFunction(action to dispatcher) as R
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class GeneralExecutableActionDispatcherTest {
GeneralExecutableActionDispatcher,
Spy<Pair<ExecutableAction<*, *>, *>, Any> by SpyData() {
@Suppress("UNCHECKED_CAST")
override fun <D, R> dispatch(action: ExecutableAction<D, R>, dispatcher: D): R =
spyFunction(action to dispatcher) as R
override fun <D, R> dispatch(action: ExecutableAction<D, R>, dispatcher: D): R = spyFunction(action to dispatcher) as R
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,16 @@ internal fun Any.chooseTestScope() = if (this is ScopeMint) testScope else mintS

val asyncSetup: TestTemplate<Unit> get() = AsyncMints.asyncSetup

fun <SC : Any> asyncTestTemplate(sharedSetup: suspend () -> SC, sharedTeardown: suspend (SC) -> Unit = {}) =
AsyncMints.asyncTestTemplate(sharedSetup, sharedTeardown)
fun <SC : Any> asyncTestTemplate(sharedSetup: suspend () -> SC, sharedTeardown: suspend (SC) -> Unit = {}) = AsyncMints.asyncTestTemplate(sharedSetup, sharedTeardown)

fun <SC : Any> asyncTestTemplate(beforeAll: suspend () -> SC) = AsyncMints.asyncTestTemplate(beforeAll = beforeAll)

fun asyncTestTemplate(sharedSetup: suspend () -> Unit, sharedTeardown: suspend () -> Unit) =
AsyncMints.asyncTestTemplate(sharedSetup) { sharedTeardown() }
fun asyncTestTemplate(sharedSetup: suspend () -> Unit, sharedTeardown: suspend () -> Unit) = AsyncMints.asyncTestTemplate(sharedSetup) { sharedTeardown() }

@JvmName("asyncTestTemplateSimple")
fun asyncTestTemplate(wrapper: suspend (suspend () -> Unit) -> Unit) = AsyncMints.asyncTestTemplateSimple(wrapper)

@JvmName("asyncTestTemplateSC")
fun <SC : Any> asyncTestTemplate(wrapper: suspend (TestFunc<SC>) -> Unit) =
AsyncMints.asyncTestTemplate(wrapper)
fun <SC : Any> asyncTestTemplate(wrapper: suspend (TestFunc<SC>) -> Unit) = AsyncMints.asyncTestTemplate(wrapper)

object AsyncMints : AsyncMintDispatcher, ReporterProvider by MintReporterConfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ package com.zegreatrob.testmints.async

class ExerciseAsync<C, R>(private val context: C, private val result: R) {
@Suppress("unused")
suspend infix fun <R2> verifyAsync(assertionFunctions: suspend C.(R) -> R2) =
context.assertionFunctions(result)
suspend infix fun <R2> verifyAsync(assertionFunctions: suspend C.(R) -> R2) = context.assertionFunctions(result)
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ class Setup<out C : Any, out SC : Any>(
return context
}

private suspend fun <R> performExercise(context: C, exerciseFunc: suspend C.() -> R) =
runCodeUnderTest(context, exerciseFunc)
.also {
if (context is ScopeMint) {
waitForJobsToFinish(context.exerciseScope)
}
private suspend fun <R> performExercise(context: C, exerciseFunc: suspend C.() -> R) = runCodeUnderTest(context, exerciseFunc)
.also {
if (context is ScopeMint) {
waitForJobsToFinish(context.exerciseScope)
}
}
}

private fun Throwable.wrapCause() = CancellationException("Test failure.", this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ package com.zegreatrob.testmints.async

class SetupAsync<C>(private val context: C) {
@Suppress("unused")
suspend infix fun <R> exerciseAsync(codeUnderTest: suspend C.() -> R) =
ExerciseAsync(context, context.codeUnderTest())
suspend infix fun <R> exerciseAsync(codeUnderTest: suspend C.() -> R) = ExerciseAsync(context, context.codeUnderTest())
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ class TestTemplate<out SC : Any>(
this.wrapper { sc1 -> wrapper(sc1, test) }
}

fun <SC2 : Any> extend(sharedSetup: suspend (SC) -> SC2, sharedTeardown: suspend (SC2) -> Unit = {}) =
extend { sc1, test ->
val sc2 = sharedSetup(sc1)
test(sc2)
sharedTeardown(sc2)
}
fun <SC2 : Any> extend(sharedSetup: suspend (SC) -> SC2, sharedTeardown: suspend (SC2) -> Unit = {}) = extend { sc1, test ->
val sc2 = sharedSetup(sc1)
test(sc2)
sharedTeardown(sc2)
}

fun extend(sharedSetup: suspend () -> Unit = {}, sharedTeardown: suspend () -> Unit = {}) = TestTemplate(reporterProvider) { test ->
wrapper {
Expand All @@ -45,15 +44,14 @@ class TestTemplate<out SC : Any>(
})
}

operator fun <C : Any> invoke(context: C, timeoutMs: Long = 60_000L, additionalActions: suspend C.() -> Unit = {}) =
Setup(
{ context },
context.chooseTestScope(),
additionalActions,
reporterProvider.reporter,
timeoutMs,
wrapper,
)
operator fun <C : Any> invoke(context: C, timeoutMs: Long = 60_000L, additionalActions: suspend C.() -> Unit = {}) = Setup(
{ context },
context.chooseTestScope(),
additionalActions,
reporterProvider.reporter,
timeoutMs,
wrapper,
)

operator fun invoke(timeoutMs: Long = 60_000L, additionalActions: suspend SC.() -> Unit = {}) = Setup(
{ it },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import com.zegreatrob.mindiff.stringDiff
import kotlin.test.assertEquals
import kotlin.test.assertTrue

fun <T> T?.assertIsEqualTo(expected: T, message: String? = null) =
assertEquals(expected, this, message.appendDiff(expected, this))
fun <T> T?.assertIsEqualTo(expected: T, message: String? = null) = assertEquals(expected, this, message.appendDiff(expected, this))

private fun <T> String?.appendDiff(expected: T, t1: T?): String =
"${this ?: ""}\n${stringDiff(expected.toString(), t1.toString())}\n"
private fun <T> String?.appendDiff(expected: T, t1: T?): String = "${this ?: ""}\n${stringDiff(expected.toString(), t1.toString())}\n"

fun <T> T?.assertIsNotEqualTo(expected: T, message: String? = null) {
assertTrue("${if (message == null) "" else "$message. "}Two values were unexpectedly identical: $expected") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ private fun diffDescription(index: Int, eDiff: String, aDiff: String) = listOf(
"A: $aDiff",
)

private fun String.diffRange(firstDiffIndex: Int, endOfString: Int) =
(firstDiffIndex until endOfString).let {
substring(it)
}
private fun String.diffRange(firstDiffIndex: Int, endOfString: Int) = (firstDiffIndex until endOfString).let {
substring(it)
}

private fun String.firstDiffIndex() = indexOf("x")
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,13 @@ private fun exceptionDescriptionMap(
teardownException: Throwable?,
templateTeardownException: Throwable?,
failure: Throwable?,
) =
mapOf(
"Failure" to failure,
"Teardown exception" to teardownException,
"Template teardown exception" to templateTeardownException,
)
.mapNotNull { (descriptor, exception) -> exception?.let { descriptor to exception } }
.toMap()
) = mapOf(
"Failure" to failure,
"Teardown exception" to teardownException,
"Template teardown exception" to templateTeardownException,
)
.mapNotNull { (descriptor, exception) -> exception?.let { descriptor to exception } }
.toMap()

private fun <SC : Any> checkedInvoke(wrapper: (TestFunc<SC>) -> Unit, test: TestFunc<SC>) = captureException {
var testWasInvoked = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ interface StandardMintDispatcher : ReporterProvider {
return testTemplate(wrapper = { it(lazy) })
}

fun testTemplateSimple(wrapper: (() -> Unit) -> Unit): TestTemplate<Unit> =
testTemplate(wrapper = { wrapper { it(Unit) } })
fun testTemplateSimple(wrapper: (() -> Unit) -> Unit): TestTemplate<Unit> = testTemplate(wrapper = { wrapper { it(Unit) } })
}

typealias ExerciseFunc<C, R> = C.() -> R
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ object StandardMints : StandardMintDispatcher, ReporterProvider by MintReporterC

val setup get() = StandardMints.setup

fun <SC : Any> testTemplate(sharedSetup: () -> SC, sharedTeardown: (SC) -> Unit = {}) =
StandardMints.testTemplate(sharedSetup, sharedTeardown)
fun <SC : Any> testTemplate(sharedSetup: () -> SC, sharedTeardown: (SC) -> Unit = {}) = StandardMints.testTemplate(sharedSetup, sharedTeardown)

fun <SC : Any> testTemplate(beforeAll: () -> SC) = StandardMints.testTemplate(beforeAll = beforeAll)

fun testTemplate(sharedSetup: () -> Unit, sharedTeardown: () -> Unit) =
StandardMints.testTemplate(sharedSetup, { sharedTeardown() })
fun testTemplate(sharedSetup: () -> Unit, sharedTeardown: () -> Unit) = StandardMints.testTemplate(sharedSetup, { sharedTeardown() })

@JvmName("testTemplateSimple")
fun testTemplate(wrapper: SimpleWrapper) = StandardMints.testTemplateSimple(wrapper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ class TestTemplate<SC : Any>(val reporterProvider: ReporterProvider, val wrapper
return TestTemplate(reporterProvider) { test -> wrapper { sc -> test(mergeContext(sc, lazy)) } }
}

operator fun <C : Any> invoke(contextProvider: (SC) -> C, additionalSetupActions: C.() -> Unit = {}) =
Setup(contextProvider, reporterProvider.reporter, additionalSetupActions, wrapper)
operator fun <C : Any> invoke(contextProvider: (SC) -> C, additionalSetupActions: C.() -> Unit = {}) = Setup(contextProvider, reporterProvider.reporter, additionalSetupActions, wrapper)

operator fun <C : Any> invoke(
context: C,
additionalSetupActions: C.() -> Unit = {},
) = Setup({ context }, reporterProvider.reporter, additionalSetupActions, wrapper)

operator fun invoke(additionalSetupActions: SC.() -> Unit = {}): Setup<SC, SC> =
Setup({ it }, reporterProvider.reporter, additionalSetupActions, wrapper)
operator fun invoke(additionalSetupActions: SC.() -> Unit = {}): Setup<SC, SC> = Setup({ it }, reporterProvider.reporter, additionalSetupActions, wrapper)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ data class MultiplyAction(val left: Int, val right: Int) {
}

interface ExampleActionDispatcher : MultiplyAction.Dispatcher {
override suspend fun handle(action: MultiplyAction): MultiplyAction.Result =
MultiplyAction.Result.Success(action.left * action.right)
override suspend fun handle(action: MultiplyAction): MultiplyAction.Result = MultiplyAction.Result.Success(action.left * action.right)
}

@ActionMint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ class ActionMintTest : ActionPipe {
val allExecutedActions = mutableListOf<Any?>()

val pipe = object : ActionPipe {
override suspend fun <D, R> execute(dispatcher: D, action: SuspendAction<D, R>): R =
action.execute(dispatcher)
.also { allExecutedActions.add((action as? ActionWrapper<*, *>)?.action) }
override suspend fun <D, R> execute(dispatcher: D, action: SuspendAction<D, R>): R = action.execute(dispatcher)
.also { allExecutedActions.add((action as? ActionWrapper<*, *>)?.action) }
}
val cannon = ActionCannon(dispatcher, pipe)
}) exercise {
Expand All @@ -156,9 +155,8 @@ class ActionMintTest : ActionPipe {
val allExecutedActions = mutableListOf<Any?>()

val pipe = object : ActionPipe {
override suspend fun <D, R> execute(dispatcher: D, action: SuspendAction<D, R>): R =
action.execute(dispatcher)
.also { allExecutedActions.add((action as? ActionWrapper<*, *>)?.action) }
override suspend fun <D, R> execute(dispatcher: D, action: SuspendAction<D, R>): R = action.execute(dispatcher)
.also { allExecutedActions.add((action as? ActionWrapper<*, *>)?.action) }
}
val cannon = ActionCannon(dispatcher, pipe)
}) exercise {
Expand All @@ -177,9 +175,8 @@ class ActionMintTest : ActionPipe {
val allExecutedActions = mutableListOf<Any?>()

val pipe = object : ActionPipe {
override suspend fun <D, R> execute(dispatcher: D, action: SuspendAction<D, R>): R =
action.execute(dispatcher)
.also { allExecutedActions.add((action as? ActionWrapper<*, *>)?.action) }
override suspend fun <D, R> execute(dispatcher: D, action: SuspendAction<D, R>): R = action.execute(dispatcher)
.also { allExecutedActions.add((action as? ActionWrapper<*, *>)?.action) }
}
val cannon = ActionCannon(dispatcher, pipe)
}) exercise {
Expand Down
Loading