Add test
This commit is contained in:
parent
3e09eee5b7
commit
15b11a191d
65
2021/src/test/kotlin/BaseDayTest.kt
Normal file
65
2021/src/test/kotlin/BaseDayTest.kt
Normal file
@ -0,0 +1,65 @@
|
||||
package be.vandewalleh.aoc.days
|
||||
|
||||
import be.vandewalleh.aoc.utils.factory.findDayDefinition
|
||||
import be.vandewalleh.aoc.utils.factory.registerExampleLoader
|
||||
import io.micronaut.context.BeanContext
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.junit.jupiter.api.AfterAll
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
abstract class BaseDayTest(day: Int) {
|
||||
|
||||
abstract val example: String
|
||||
|
||||
abstract val part1Example: Any
|
||||
abstract val part2Example: Any
|
||||
|
||||
abstract val part1Answer: Any
|
||||
abstract val part2Answer: Any
|
||||
|
||||
private val ctx = lazy {
|
||||
BeanContext.run()
|
||||
}
|
||||
|
||||
val instance: Any by lazy { ctx.value.getBean(ctx.value.findDayDefinition(day)) }
|
||||
|
||||
|
||||
private val exampleCtx = lazy {
|
||||
BeanContext.build()
|
||||
.apply { registerExampleLoader(example) }
|
||||
.start()
|
||||
}
|
||||
|
||||
val exampleInstance: Any by lazy { exampleCtx.value.getBean(exampleCtx.value.findDayDefinition(day)) }
|
||||
|
||||
@AfterAll
|
||||
fun `after all`() {
|
||||
arrayOf(ctx, exampleCtx).filter { it.isInitialized() }.forEach { it.value.stop() }
|
||||
}
|
||||
|
||||
private fun BeanContext.run(instance: Any, method: String) =
|
||||
findExecutionHandle<Any, Any>(instance, method).get().invoke()
|
||||
|
||||
@Test
|
||||
fun `part1 example result`() {
|
||||
Assertions.assertThat(ctx.value.run(exampleInstance, "part1")).isEqualTo(part1Example)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `part1 result`() {
|
||||
Assertions.assertThat(ctx.value.run(instance, "part1")).isEqualTo(part1Answer)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `part2 example result`() {
|
||||
Assertions.assertThat(ctx.value.run(exampleInstance, "part2")).isEqualTo(part2Example)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `part2 result`() {
|
||||
Assertions.assertThat(ctx.value.run(instance, "part2")).isEqualTo(part2Answer)
|
||||
}
|
||||
|
||||
}
|
||||
22
2021/src/test/kotlin/Day01Test.kt
Normal file
22
2021/src/test/kotlin/Day01Test.kt
Normal file
@ -0,0 +1,22 @@
|
||||
package be.vandewalleh.aoc.days
|
||||
|
||||
class Day01Test : BaseDayTest(1) {
|
||||
override val example = """
|
||||
199
|
||||
200
|
||||
208
|
||||
210
|
||||
200
|
||||
207
|
||||
240
|
||||
269
|
||||
260
|
||||
263
|
||||
""".trimIndent()
|
||||
|
||||
override val part1Example = 7
|
||||
override val part2Example = 5
|
||||
|
||||
override val part1Answer = 1559
|
||||
override val part2Answer = 1600
|
||||
}
|
||||
@ -15,7 +15,7 @@ fun BeanContext.findDayDefinition(day: Int): BeanDefinition<*>? = allBeanDefinit
|
||||
inline fun <reified T> createDay() = createDay(T::class.java)
|
||||
|
||||
// A custom resourceLoader that returns a string should be more appropriate but ¯\_(ツ)_/¯
|
||||
private fun BeanContext.registerExampleLoader(example: String) {
|
||||
fun BeanContext.registerExampleLoader(example: String) {
|
||||
registerSingleton(TempFileResourceLoader(File.createTempFile("aoc-example", ".txt").apply {
|
||||
writeText(example)
|
||||
}))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user