From 029d43da9c29e55dca1d9573ba07345c49a2ad6a Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Thu, 19 Nov 2020 16:53:01 +0100 Subject: [PATCH] Added automatic tests for junit samples --- util/test/AbstractTestExamples.kt | 9 ++++++--- util/test/AbstractTestExercises.kt | 1 - util/test/TestAllExamples.java | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/util/test/AbstractTestExamples.kt b/util/test/AbstractTestExamples.kt index c7cd68b8..b686f2fd 100644 --- a/util/test/AbstractTestExamples.kt +++ b/util/test/AbstractTestExamples.kt @@ -1,11 +1,14 @@ import atomictest.ERROR_TAG import org.junit.Assert +import org.junit.runner.JUnitCore import util.LINE_SEPARATOR import util.normalizeLineSeparators import java.io.* import java.util.function.Consumer abstract class AbstractTestExamples { + private val junit = JUnitCore() + protected fun testExample(fileName: String, main: Runnable) { testExample(fileName, Consumer { main.run() }) } @@ -26,9 +29,9 @@ abstract class AbstractTestExamples { } } - protected fun testExercise(outputFileName: String, main: Consumer>) { - val output = File(outputFileName).readText() - testOutput(output, main, trim = false) + protected fun testUnitTest(testClass: Class<*>) { + val result = junit.run(testClass) + Assert.assertTrue(result.wasSuccessful()) } private fun extractOutput(exampleCode: String, outputComment: String) = diff --git a/util/test/AbstractTestExercises.kt b/util/test/AbstractTestExercises.kt index c70f0dd0..5706d2b0 100644 --- a/util/test/AbstractTestExercises.kt +++ b/util/test/AbstractTestExercises.kt @@ -1,7 +1,6 @@ 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 diff --git a/util/test/TestAllExamples.java b/util/test/TestAllExamples.java index 7f8e9d5d..a01a5f16 100644 --- a/util/test/TestAllExamples.java +++ b/util/test/TestAllExamples.java @@ -2206,6 +2206,21 @@ public class TestAllExamples extends AbstractTestExamples { testExample("Preventing Failure/Unit Testing/Examples/src/UsingExpect.kt", unittesting.UsingExpectKt::main); } + @Test + public void testSampleTest() { + testUnitTest(unittesting.SampleTest.class); + } + + @Test + public void testLearnerTest() { + testUnitTest(unittesting.LearnerTest.class); + } + + @Test + public void testStateMachineTest() { + testUnitTest(unittesting.StateMachineTest.class); + } + @Test public void testTransform() { testExample("Power Tools/Extension Lambdas/Examples/src/Transform.kt", extensionlambdas.TransformKt::main);