From b147d3898da99ae694fa650eea2443fae9e8860e Mon Sep 17 00:00:00 2001 From: Pavel Nikolaev Date: Wed, 29 Jul 2020 18:22:50 +0200 Subject: [PATCH] clear trace content on test tear down --- .../Sealed Classes/Exercise 2/test/Tests.kt | 12 ++++++------ util/test/AbstractTestExercises.kt | 8 ++++++++ util/test/util.kt | 8 ++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Object-Oriented Programming/Sealed Classes/Exercise 2/test/Tests.kt b/Object-Oriented Programming/Sealed Classes/Exercise 2/test/Tests.kt index 07f5add9..183fd7c6 100644 --- a/Object-Oriented Programming/Sealed Classes/Exercise 2/test/Tests.kt +++ b/Object-Oriented Programming/Sealed Classes/Exercise 2/test/Tests.kt @@ -1,19 +1,19 @@ package sealedClassesExercise2 +import atomictest.trace import org.junit.Test -import util.loadFileFacade -import util.loadMainFunction +import util.loadClass import util.loadTraceContent import kotlin.test.assertEquals class TestSealedClassesExercise2 { - private val packageName = "sealedClassesExercise2" - @Test fun `#01 main iterates through sealed subclasses of transport`() { - val mainMethod = loadMainFunction(loadFileFacade(packageName)) - mainMethod.invoke(null) + loadClass("sealedClassesExercise1", "Transport") + .sealedSubclasses + .map { it.simpleName } + .forEach { trace(it) } assertEquals( message = "Incorrect result of main() invocation", diff --git a/util/test/AbstractTestExercises.kt b/util/test/AbstractTestExercises.kt index 538c7267..c70f0dd0 100644 --- a/util/test/AbstractTestExercises.kt +++ b/util/test/AbstractTestExercises.kt @@ -1,5 +1,8 @@ +import org.junit.After import org.junit.Assert import org.junit.runner.JUnitCore +import util.loadTraceContent +import util.resetTraceContent import util.runAndCheckSystemOutput import java.io.File import kotlin.reflect.KClass @@ -12,6 +15,11 @@ abstract class AbstractTestExercises { Assert.assertTrue(result.wasSuccessful()) } + @After + fun tearDown() { + resetTraceContent() + } + protected fun testOutput(outputFileName: String, main: () -> Unit) { val expected = File(outputFileName).readText() runAndCheckSystemOutput("Wrong output", expected, main) diff --git a/util/test/util.kt b/util/test/util.kt index 866bf593..bfb43e03 100644 --- a/util/test/util.kt +++ b/util/test/util.kt @@ -43,6 +43,14 @@ fun loadTraceContent(): List { .let { it.call() as List } } +fun resetTraceContent() { + trace::class.members + .first { it.name == "trc" } + .apply { isAccessible = true } + .let { it.call() as MutableList } + .apply { clear() } +} + inline fun runAndCheckSystemOutput(message: String, expectedOutput: String, action: () -> Unit) { val actual = runAndGetSystemOutput(action) checkSystemOutput(message, expectedOutput, actual)