Skip to content

Commit a321396

Browse files
committed
Remove suspend modifier from PartScope.
It leads to a false-positive report in intelliJ complaining about calling suspend functions even though runtime worked.
1 parent f270036 commit a321396

File tree

3 files changed

+32
-42
lines changed

3 files changed

+32
-42
lines changed

aockt-test/api/aockt-test.api

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ public abstract class io/github/jadarma/aockt/test/AdventSpec : io/kotest/core/s
1414
public final fun dispatcherAffinity ()Ljava/lang/Boolean;
1515
public final fun getSolution ()Lio/github/jadarma/aockt/core/Solution;
1616
public final fun isolationMode ()Lio/kotest/core/spec/IsolationMode;
17-
public final fun partOne-51bEbmg (ZZLio/github/jadarma/aockt/test/ExecMode;Lkotlin/time/Duration;Lkotlin/jvm/functions/Function2;)V
18-
public static synthetic fun partOne-51bEbmg$default (Lio/github/jadarma/aockt/test/AdventSpec;ZZLio/github/jadarma/aockt/test/ExecMode;Lkotlin/time/Duration;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
19-
public final fun partTwo-51bEbmg (ZZLio/github/jadarma/aockt/test/ExecMode;Lkotlin/time/Duration;Lkotlin/jvm/functions/Function2;)V
20-
public static synthetic fun partTwo-51bEbmg$default (Lio/github/jadarma/aockt/test/AdventSpec;ZZLio/github/jadarma/aockt/test/ExecMode;Lkotlin/time/Duration;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
17+
public final fun partOne-51bEbmg (ZZLio/github/jadarma/aockt/test/ExecMode;Lkotlin/time/Duration;Lkotlin/jvm/functions/Function1;)V
18+
public static synthetic fun partOne-51bEbmg$default (Lio/github/jadarma/aockt/test/AdventSpec;ZZLio/github/jadarma/aockt/test/ExecMode;Lkotlin/time/Duration;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
19+
public final fun partTwo-51bEbmg (ZZLio/github/jadarma/aockt/test/ExecMode;Lkotlin/time/Duration;Lkotlin/jvm/functions/Function1;)V
20+
public static synthetic fun partTwo-51bEbmg$default (Lio/github/jadarma/aockt/test/AdventSpec;ZZLio/github/jadarma/aockt/test/ExecMode;Lkotlin/time/Duration;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
2121
public final fun testCaseOrder ()Lio/kotest/core/test/TestCaseOrder;
2222
public final fun threads ()Ljava/lang/Integer;
2323
}
2424

2525
public abstract interface class io/github/jadarma/aockt/test/AdventSpec$PartScope {
26-
public abstract fun shouldAllOutput (Ljava/lang/Iterable;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
27-
public abstract fun shouldOutput (Ljava/lang/String;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
26+
public abstract fun shouldAllOutput (Ljava/lang/Iterable;Ljava/lang/Object;)V
27+
public abstract fun shouldOutput (Ljava/lang/String;Ljava/lang/Object;)V
2828
}
2929

3030
public final class io/github/jadarma/aockt/test/AocKtExtension : kotlin/coroutines/AbstractCoroutineContextElement, io/kotest/core/extensions/DisplayNameFormatterExtension, io/kotest/core/extensions/TestCaseExtension {

aockt-test/src/main/kotlin/AdventSpec.kt

+16-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import io.github.jadarma.aockt.test.internal.id
1919
import io.github.jadarma.aockt.test.internal.partFunction
2020
import io.github.jadarma.aockt.test.internal.solutionToPart
2121
import io.kotest.assertions.failure
22+
import io.kotest.assertions.throwables.shouldNotThrowAny
2223
import io.kotest.assertions.withClue
2324
import io.kotest.common.ExperimentalKotest
2425
import io.kotest.core.concurrency.CoroutineDispatcherFactory
@@ -122,7 +123,7 @@ public abstract class AdventSpec<T : Solution>(
122123
* @receiver The example puzzle input.
123124
* @param expected The correct answer to the puzzle for the given input.
124125
*/
125-
public suspend infix fun String.shouldOutput(expected: Any)
126+
public infix fun String.shouldOutput(expected: Any)
126127

127128
/**
128129
* For each of the values given creates a new test that asserts that when given as input, it gets the correct
@@ -134,7 +135,7 @@ public abstract class AdventSpec<T : Solution>(
134135
* @receiver The example puzzle inputs.
135136
* @param expected The correct answer to the puzzle for all given inputs.
136137
*/
137-
public suspend infix fun Iterable<String>.shouldAllOutput(expected: Any)
138+
public infix fun Iterable<String>.shouldAllOutput(expected: Any)
138139
}
139140

140141
/**
@@ -160,7 +161,7 @@ public abstract class AdventSpec<T : Solution>(
160161
expensive: Boolean,
161162
executionMode: ExecMode?,
162163
efficiencyBenchmark: Duration?,
163-
examples: (suspend PartScope.() -> Unit)?,
164+
examples: (PartScope.() -> Unit)?,
164165
) {
165166
if (efficiencyBenchmark != null && !efficiencyBenchmark.isPositive()) {
166167
throw ConfigurationException("Efficiency benchmark must be a positive value, but was: $efficiencyBenchmark")
@@ -190,7 +191,16 @@ public abstract class AdventSpec<T : Solution>(
190191

191192
if (examples != null) {
192193
context("Validates the examples").config(enabled = execMode != ExecMode.SkipExamples) {
193-
AdventSpecPartScope(partFunction, this).examples()
194+
AdventSpecPartScope().apply(examples).forEachIndexed { index, input, expected ->
195+
test("Example #${index + 1}") {
196+
withClue("Expected answer '$expected' for input: ${input.preview()}") {
197+
val answer = shouldNotThrowAny {
198+
partFunction(input.toString()).toString()
199+
}
200+
answer shouldBe expected
201+
}
202+
}
203+
}
194204
}
195205
}
196206

@@ -267,7 +277,7 @@ public abstract class AdventSpec<T : Solution>(
267277
expensive: Boolean = false,
268278
executionMode: ExecMode? = null,
269279
efficiencyBenchmark: Duration? = null,
270-
test: (suspend PartScope.() -> Unit)? = null,
280+
test: (PartScope.() -> Unit)? = null,
271281
): Unit = partTest(One, enabled, expensive, executionMode, efficiencyBenchmark, test)
272282

273283
/**
@@ -291,6 +301,6 @@ public abstract class AdventSpec<T : Solution>(
291301
expensive: Boolean = false,
292302
executionMode: ExecMode? = null,
293303
efficiencyBenchmark: Duration? = null,
294-
test: (suspend PartScope.() -> Unit)? = null,
304+
test: (PartScope.() -> Unit)? = null,
295305
): Unit = partTest(Two, enabled, expensive, executionMode, efficiencyBenchmark, test)
296306
}
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,21 @@
11
package io.github.jadarma.aockt.test.internal
22

33
import io.github.jadarma.aockt.test.AdventSpec
4-
import io.kotest.assertions.throwables.shouldNotThrowAny
5-
import io.kotest.assertions.withClue
6-
import io.kotest.core.spec.style.scopes.ContainerScope
7-
import io.kotest.core.spec.style.scopes.FunSpecContainerScope
8-
import io.kotest.matchers.shouldBe
94

10-
/**
11-
* A [ContainerScope] specialized for testing the outputs of a specific function against predetermined inputs.
12-
*
13-
* @param implementation The function to be tested.
14-
* @param context The parent context in which to append these test cases.
15-
*/
16-
internal class AdventSpecPartScope(
17-
private val implementation: (String) -> Any,
18-
private val context: FunSpecContainerScope,
19-
) : AdventSpec.PartScope, ContainerScope by context {
5+
/** A simple part scope implementation that builds a list of example inputs. */
6+
internal class AdventSpecPartScope : AdventSpec.PartScope {
207

21-
/** Incrementing counter for the example test cases. */
22-
private var exampleNumber: Int = 0
8+
private val examples = mutableListOf<Pair<PuzzleInput, String>>()
239

24-
override suspend infix fun String.shouldOutput(expected: Any) {
25-
exampleNumber++
26-
val input = PuzzleInput(this)
27-
val preview = input.preview()
28-
context.test("Example #$exampleNumber") {
29-
withClue("Expected answer '$expected' for input: $preview") {
30-
val answer = shouldNotThrowAny {
31-
this@AdventSpecPartScope.implementation(input.toString()).toString()
32-
}
33-
answer shouldBe expected.toString()
34-
}
35-
}
10+
override infix fun String.shouldOutput(expected: Any) {
11+
examples.add(PuzzleInput(this) to expected.toString())
3612
}
3713

38-
override suspend infix fun Iterable<String>.shouldAllOutput(expected: Any) {
14+
override infix fun Iterable<String>.shouldAllOutput(expected: Any) {
3915
forEach { it shouldOutput expected }
4016
}
17+
18+
/** Applies the [block] function for each registered example. */
19+
inline fun forEachIndexed(block: (Int, PuzzleInput, String) -> Unit) =
20+
examples.forEachIndexed { index, example -> block(index, example.first, example.second) }
4121
}

0 commit comments

Comments
 (0)