Skip to content

Commit 2b9429a

Browse files
committed
README fixes and LinesSource
Enables JUnit parameterised tests.
1 parent d270940 commit 2b9429a

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ Here's the overview of your daily routine while solving AoC puzzles:
4040
1. Open `./calendar/dayX` directory for the day you're solving.
4141
2. Paste your input for part1 into `part1.txt`.
4242
3. Open `DayX.kt` file and implement your solution in `part1` method and return your answer.
43-
4. Check your solution for part1 by running `./gradlew test --tests='dayX.DayX#part1'`
44-
5. Paste your input for part2 into `part2.txt`.
43+
4. Check your solution for part1 by running `./gradlew test --tests='dayX.DayX.part1'`
44+
5. Paste your input for part2 into `part2.txt` (only if it's different frm `part1`, otherwise `part1.txt` will be used.
4545
6. Open `DayX.kt` file and implement your solution in `part2` method and return your answer.
46-
7. Check your solution for part2 by running `./gradlew test --tests='dayX.DayX#part2'`
46+
7. Check your solution for part2 by running `./gradlew test --tests='dayX.DayX.part2'`
4747
8. Check both your solutions by running `./gradlew test --tests='dayX.DayX'`
4848
9. Submit your answers to [AoC](https://adventofcode.com)
4949
10. Clean up inputs by running `./gradlew cleanInputs`

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ repositories {
1313

1414
dependencies {
1515
implementation(kotlin("test-junit5"))
16+
implementation("org.junit.jupiter:junit-jupiter-params")
1617
}
1718

1819
sourceSets {

src/main/kotlin/Day.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import org.junit.jupiter.api.Test
2+
import org.junit.jupiter.api.extension.ExtensionContext
3+
import org.junit.jupiter.params.ParameterizedTest
4+
import org.junit.jupiter.params.provider.Arguments
5+
import org.junit.jupiter.params.provider.ArgumentsProvider
26
import java.awt.Toolkit
37
import java.awt.datatransfer.StringSelection
48
import java.io.File
59
import java.util.*
10+
import java.util.stream.Stream
611
import kotlin.time.ExperimentalTime
712
import kotlin.time.measureTimedValue
813

@@ -42,9 +47,36 @@ abstract class Day {
4247
Toolkit.getDefaultToolkit().systemClipboard.setContents(StringSelection(data), null)
4348
}
4449

50+
4551
@Test
4652
fun part1(): Unit = solve("part1.txt", null, ::part1)
4753

4854
@Test
4955
fun part2(): Unit = solve("part2.txt", "part1.txt", ::part2)
56+
57+
/**
58+
* [Lines] provider for JUnit [ParameterizedTest].
59+
* It reads the file named `{test_method_name}.txt` placed in the same directory as the test class.
60+
*
61+
* Usage:
62+
* ```kotlin
63+
* @ParameterizedTest
64+
* @ArgumentsSource(LinesSource::class)
65+
* fun part1(input: Lines) {}
66+
* ```
67+
*/
68+
class LinesSource : ArgumentsProvider {
69+
private fun Class<*>.readInput(inputFileName: String) =
70+
this::class.java.getResource(inputFileName)?.toURI()?.let(::File)?.takeIf { it.length() > 0 }
71+
72+
override fun provideArguments(context: ExtensionContext): Stream<out Arguments> {
73+
val clazz = context.testClass.get()
74+
val method = context.testMethod.get()
75+
val input = clazz.readInput("${method.name}.txt")
76+
?: clazz.readInput("${method.name}.txt".replace("2", "1"))
77+
?: error("${method.name}.txt not found")
78+
return Stream.of(Arguments.of(input.readLines()))
79+
}
80+
81+
}
5082
}

0 commit comments

Comments
 (0)