From 1613941d96b2c56a111c8a6427ea72b44325bbe2 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 3 Dec 2024 09:44:34 +0100 Subject: [PATCH] fix(day): refactor tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • 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 --- src/Day.kt | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/Day.kt b/src/Day.kt index 8ae3482..1966a53 100644 --- a/src/Day.kt +++ b/src/Day.kt @@ -8,35 +8,31 @@ abstract class Day { */ 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. * * @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 - * @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( part1Answer: Part1Answer?, - part2Answer: Part2Answer, + part2Answer: Part2Answer? = null, ) { - part1Answer?.let { test(it) } + precompute() - print("Checking part 2:\t") - check(part2() == part2Answer) { "Given answer: ${part2()}" } - println("[OK]") + part1Answer?.let { + print("Checking part 1:\t") + check(part1() == it) { "Given answer: ${part1()}" } + println("[OK]") + } + + part2Answer?.let { + print("Checking part 2:\t") + check(part2() == it) { "Given answer: ${part2()}" } + println("[OK]") + } } /**