AocKt (short for Advent of Code - Kotlin) is a simple library that makes running and testing your Kotlin solutions to Advent of Code puzzles a breeze.
It is an opinionated testing framework built on Kotest that defines a new AdventSpec specialized
for testing AoC puzzle solutions with minimal boilerplate.
Visit the project website for installation instructions, DSL documentation, workflow guides, advanced configuration options, and more!
- Completely Offline - Puzzle inputs and solutions are read from local files, no need for tokens.
- Test-Driven - Run your code from unit tests for faster feedback loops and fearless refactorings.
- DSL-Driven - Define your test cases with minimal code.
- Configurable - You decide what runs and when using optional parameters.
- Minimal - The test framework is the only non-Kotlin dependency.
Project Template
For your convenience, there is an advent-of-code-kotlin-template repository which you can use to generate your own solutions repo. It comes with a pre-configured Gradle project with all bells and whistles you might need, as well as a modified source structure for easier navigation.
(If you need a working example, check out my solutions repo.)
Standalone Gradle Project
To add AocKt to your existing project, simply add the dependencies and configure your unit tests to run with Kotest:
plugins {
kotlin("jvm") version "$kotlinVersion"
}
repositories {
mavenCentral()
}
dependencies {
implementation("io.github.jadarma.aockt:aockt-core:$aocktVersion")
testImplementation("io.github.jadarma.aockt:aockt-test:$aocktVersion")
testImplementation("io.kotest:kotest-runner-junit5:$kotestVersion")
}
tasks.test {
useJUnitPlatform()
}AocKt provides the following DSL for testing puzzle solutions:
object Y9999D01 : Solution { // 1.
override fun partOne(input: String) = spoilers()
override fun partTwo(input: String) = spoilers()
}
@AdventDay(9999, 1, "Magic Numbers") // 2.
class Y9999D01Test : AdventSpec<Y9999D01>({ // 3.
partOne { // 4.
"1,2,3,4" shouldOutput 4 // 5.
listOf("2", "2,2", "2,4,6,8") shouldAllOutput 0 // 6.
}
partTwo() // 7.
})In the above example:
- Your solution should implement the
Solutioninterface. - Each test class should be annotated with the
@AdventDayannotation. Title is optional, but the year and day are required. - Rather than passing it as an instance, the
AdventSpectakes in your solution as a type parameter. - Use the
partOneandpartTwofunctions as needed. Inside the lambda you can define test cases. TheSolutionfunctions will only be invoked if the relevant part DSL is used. If you have not yet implemented the second part, or it doesn't exist (e.g.: Every year, part two of the last day just requires collecting all other 49 stars), then you may simply omit it. - To define a test case, use the
shouldOutputfunction. Each usage will define another test case. The value tested against is checked against its string value, soshouldOutput 4andshouldOutput "4"are equivalent. - As a shorthand for defining multiple examples that should output the same thing, use the
shouldAllOutputfunction. - If you don't have any examples, but do want to run the part against your input the lambda can be omitted.
If you'd like to help out:
- Report bugs and submit feature requests via an issue.
- If this helped you unlock some stars, consider giving one to this repo! ⭐
This project is licensed under the MIT License - see the LICENSE for details.
Advent of Code is a registered trademark of Eric K. Wastl in the United States.