From 535db2a5d378637470b8768831fd05cca3f2198a Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Wed, 14 Apr 2021 16:52:30 +0200 Subject: [PATCH] Fix single test case (#12) * Fix single test case * Fix single test case handling * Ignore temporary files * Run Scala tests --- .gitignore | 1 + bin/run-tests.sh | 2 ++ src/main/scala/Application.scala | 27 ++++++++------- src/test/resources/HelloWorld_successful.xml | 12 +++++++ src/test/resources/outputs/output_empty.txt | 15 ++++++++ src/test/scala/ApplicationSpec.scala | 36 ++++++++++++-------- 6 files changed, 67 insertions(+), 26 deletions(-) create mode 100644 src/test/resources/HelloWorld_successful.xml create mode 100644 src/test/resources/outputs/output_empty.txt diff --git a/.gitignore b/.gitignore index 66a38c8..4fcecfb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ target/ tests/**/results.json tests/**/build.log tests/**/runner.log +tests/**/*.original \ No newline at end of file diff --git a/bin/run-tests.sh b/bin/run-tests.sh index 72d832d..bfce5db 100755 --- a/bin/run-tests.sh +++ b/bin/run-tests.sh @@ -13,6 +13,8 @@ exit_code=0 +sbt test + # Iterate over all test directories for test_dir in tests/*; do test_dir_name=$(basename "${test_dir}") diff --git a/src/main/scala/Application.scala b/src/main/scala/Application.scala index 6a02a28..96e4e63 100644 --- a/src/main/scala/Application.scala +++ b/src/main/scala/Application.scala @@ -40,6 +40,16 @@ rawContent.contains("[error] (Test / compileIncremental) Compilation failed")) rawContent else "" } + def toTestCaseJSON(testCase: JSONObject): JSONObject = { + val fail = testCase.optJSONObject("failure") + new JSONObject() + .put("name", testCase.getString("name")) + .put("status", if(fail != null) "fail" else "pass" ) + .put("message", if(fail != null) fail.getString("message") else JSONObject.NULL) + .put("output", JSONObject.NULL) + .put("test_code", JSONObject.NULL) + } + def toExercismJSON(buildLogFilePath: String, testResultsFilePath: String): JSONObject = { val baseObject = new JSONObject().put("version", 2) val errorMessage = findErrorsInLog(buildLogFilePath) @@ -50,18 +60,11 @@ } else { val testSuite = getTestSuiteObject(testResultsFilePath) val failuresNum = testSuite.getInt("failures") - val testCasesArray = testSuite.getJSONArray("testcase") - - val testCases: Array[JSONObject] = (0 until testCasesArray.length).toArray.map(idx => { - val o = testCasesArray.getJSONObject(idx) - val fail = o.optJSONObject("failure") - new JSONObject() - .put("name", o.getString("name")) - .put("status", if(fail != null) "fail" else "pass" ) - .put("message", if(fail != null) fail.getString("message") else JSONObject.NULL) - .put("output", JSONObject.NULL) - .put("test_code", JSONObject.NULL) - }) + val testcase = testSuite.get("testcase") + val testCases: Array[JSONObject] = testcase match { + case arr: JSONArray => (0 until arr.length).map(idx => toTestCaseJSON(arr.getJSONObject(idx))).toArray + case obj: JSONObject => Array(toTestCaseJSON(obj)) + } baseObject .put("status", if(failuresNum > 0) "fail" else "pass") diff --git a/src/test/resources/HelloWorld_successful.xml b/src/test/resources/HelloWorld_successful.xml new file mode 100644 index 0000000..f4fdc0b --- /dev/null +++ b/src/test/resources/HelloWorld_successful.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/outputs/output_empty.txt b/src/test/resources/outputs/output_empty.txt new file mode 100644 index 0000000..fa318e1 --- /dev/null +++ b/src/test/resources/outputs/output_empty.txt @@ -0,0 +1,15 @@ +[error] /opt/test-runner/tests/example-empty-file/src/test/scala/ExampleEmptyFileTest.scala:7:5: not found: value Leap +[error] Leap.leapYear(2015) should be (false) +[error] ^ +[error] /opt/test-runner/tests/example-empty-file/src/test/scala/ExampleEmptyFileTest.scala:12:5: not found: value Leap +[error] Leap.leapYear(1996) should be (true) +[error] ^ +[error] /opt/test-runner/tests/example-empty-file/src/test/scala/ExampleEmptyFileTest.scala:17:5: not found: value Leap +[error] Leap.leapYear(2100) should be (false) +[error] ^ +[error] /opt/test-runner/tests/example-empty-file/src/test/scala/ExampleEmptyFileTest.scala:22:5: not found: value Leap +[error] Leap.leapYear(2000) should be (true) +[error] ^ +[error] four errors found +[error] (Test / compileIncremental) Compilation failed +[error] Total time: 4 s, completed Apr 14, 2021 2:11:23 PM diff --git a/src/test/scala/ApplicationSpec.scala b/src/test/scala/ApplicationSpec.scala index c6a47dd..00e8dfc 100644 --- a/src/test/scala/ApplicationSpec.scala +++ b/src/test/scala/ApplicationSpec.scala @@ -1,21 +1,16 @@ -import org.scalatest.flatspec.AnyFlatSpec -import org.scalatest.matchers.should.Matchers - -import scala.io.Source import org.json.JSONObject +import org.scalatest.{Matchers, FunSuite} -import java.io.File - -class ApplicationSpec extends AnyFlatSpec with Matchers { +class ApplicationSpec extends FunSuite with Matchers { - "A successful xml" should "pass simply" in { + test("A successful xml should pass simply") { val xmlTestURL = getClass.getResource("/GradeSchool_successful.xml").getPath val jsonArray = Application.getTestCasesJSON(xmlTestURL) val objects = (0 until jsonArray.length).map( jsonArray.getJSONObject(_).optJSONObject("failure") ) objects should contain only (null) } - "A successful xml" should "be properly formatted as JSON" in { + test("A successful xml should be properly formatted as JSON") { val xmlTestURL = getClass.getResource("/GradeSchool_successful.xml").getPath val outputFileURL = getClass.getResource("/outputs/output.txt").getPath val exercismOutput: JSONObject = Application.toExercismJSON(outputFileURL, xmlTestURL) @@ -35,14 +30,28 @@ class ApplicationSpec extends AnyFlatSpec with Matchers { assert(testCases(6).toString() == """{"output":null,"name":"sort school","test_code":null,"message":null,"status":"pass"}""") } - "A failing xml" should "contain a failure object" in { + test("A successful xml with a single test case should be properly formatted as JSON") { + val xmlTestURL = getClass.getResource("/HelloWorld_successful.xml").getPath + val outputFileURL = getClass.getResource("/outputs/output.txt").getPath + val exercismOutput: JSONObject = Application.toExercismJSON(outputFileURL, xmlTestURL) + + assert(exercismOutput.getInt("version") == 2) + assert(exercismOutput.getString("status") == "pass") + assert(exercismOutput.opt("message") == null) + + val testCases = exercismOutput.get("tests").asInstanceOf[Array[JSONObject]] + assert(testCases.length == 1) + assert(testCases(0).toString() == """{"output":null,"name":"Say Hi!","test_code":null,"message":null,"status":"pass"}""") + } + + test("A failing xml should contain a failure object") { val xmlTestURL = getClass.getResource("/GradeSchool_failure.xml").getPath val jsonArray = Application.getTestCasesJSON(xmlTestURL) val objects = (0 until jsonArray.length).map( jsonArray.getJSONObject(_).optJSONObject("failure") ) objects.filter( _ !== null ).length > 0 } - "A failing xml" should "be properly formatted as JSON" in { + test("A failing xml should be properly formatted as JSON") { val xmlTestURL = getClass.getResource("/GradeSchool_failure.xml").getPath val outputFileURL = getClass.getResource("/outputs/output_fail.txt").getPath val exercismOutput: JSONObject = Application.toExercismJSON(outputFileURL, xmlTestURL) @@ -63,17 +72,16 @@ class ApplicationSpec extends AnyFlatSpec with Matchers { assert(testCases(4).toString() == """{"output":null,"name":"get students in a grade","test_code":null,"message":null,"status":"pass"}""") assert(testCases(5).toString() == """{"output":null,"name":"get students in a non-existent grade","test_code":null,"message":null,"status":"pass"}""") assert(testCases(6).toString() == """{"output":null,"name":"sort school","test_code":null,"message":null,"status":"pass"}""") - } - "An xml with a syntax error" should "be properly reported as JSON" in { + test("An xml with a syntax error should be properly reported as JSON") { val outputFileURL = getClass.getResource("/outputs/output_error.txt").getPath val exercismOutput: JSONObject = Application.toExercismJSON(outputFileURL, null) assert(exercismOutput.getInt("version") == 2) assert(exercismOutput.getString("status") == "error") } - "An xml with a syntax error due to an empty file" should "be properly reported as JSON" in { + test("An xml with a syntax error due to an empty file should be properly reported as JSON") { val outputFileURL = getClass.getResource("/outputs/output_empty.txt").getPath val exercismOutput: JSONObject = Application.toExercismJSON(outputFileURL, null) assert(exercismOutput.getInt("version") == 2)