fix(day): refactor tests

• Fix a bug that caused ‹precompute()› not to be run if tests were run
  only for the second part.
• Merge the ‹test()› overloads into one function:
  · bugs in one place
  · DRY

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-12-03 09:44:34 +01:00
parent 7341109f4d
commit 1613941d96
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -8,37 +8,33 @@ abstract class Day<Part1Answer, Part2Answer> {
*/ */
abstract fun precompute() abstract fun precompute()
/**
* Tests the first part of the challenge.
*
* @param part1Answer Expected answer to the 1º part of the challenge
*/
fun test(part1Answer: Part1Answer) {
precompute()
print("Checking part 1:\t")
check(part1() == part1Answer)
println("[OK]")
}
/** /**
* Tests both parts of the challenge. * Tests both parts of the challenge.
* *
* @param part1Answer Expected answer to the 1º part of the challenge; * @param part1Answer Expected answer to the 1º part of the challenge;
* also can be `null`, in such case, the 1º part is not being tested * also can be `null`, in such case, the 1º part is not being tested
* @param part2Answer Expected answer to the 2º part of the challenge * @param part2Answer Expected answer to the 2º part of the challenge;
* also can be `null`, in such case, the 2º part is not being tested
*/ */
fun test( fun test(
part1Answer: Part1Answer?, part1Answer: Part1Answer?,
part2Answer: Part2Answer, part2Answer: Part2Answer? = null,
) { ) {
part1Answer?.let { test(it) } precompute()
print("Checking part 2:\t") part1Answer?.let {
check(part2() == part2Answer) { "Given answer: ${part2()}" } print("Checking part 1:\t")
check(part1() == it) { "Given answer: ${part1()}" }
println("[OK]") println("[OK]")
} }
part2Answer?.let {
print("Checking part 2:\t")
check(part2() == it) { "Given answer: ${part2()}" }
println("[OK]")
}
}
/** /**
* Runs the `precompute()` and then each part of the challenge. * Runs the `precompute()` and then each part of the challenge.
*/ */