Skip to content

Commit

Permalink
Fix single test case (#12)
Browse files Browse the repository at this point in the history
* Fix single test case

* Fix single test case handling

* Ignore temporary files

* Run Scala tests
  • Loading branch information
ErikSchierboom authored Apr 14, 2021
1 parent b88d3be commit 535db2a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ target/
tests/**/results.json
tests/**/build.log
tests/**/runner.log
tests/**/*.original
2 changes: 2 additions & 0 deletions bin/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
27 changes: 15 additions & 12 deletions src/main/scala/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/HelloWorld_successful.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version='1.0' encoding='UTF-8'?>
<testsuite hostname="localhost" name="HelloWorldTest" tests="1" errors="0" failures="0" skipped="0" time="0.034" timestamp="2021-04-14T13:56:41">
<properties>
<property name="jline.esc.timeout" value="0"/><property name="jna.platform.library.path" value="/usr/lib/x86_64-linux-gnu:/lib/x86_64-linux-gnu:/lib64:/usr/lib:/lib:/usr/local/openjdk-8/lib/amd64/jli:/usr/local/openjdk-8/lib/amd64"/><property name="java.runtime.name" value="OpenJDK Runtime Environment"/><property name="sun.boot.library.path" value="/usr/local/openjdk-8/jre/lib/amd64"/><property name="java.vm.version" value="25.232-b09"/><property name="java.vm.vendor" value="Oracle Corporation"/><property name="java.vendor.url" value="http://java.oracle.com/"/><property name="path.separator" value=":"/><property name="jna.loaded" value="true"/><property name="java.vm.name" value="OpenJDK 64-Bit Server VM"/><property name="file.encoding.pkg" value="sun.io"/><property name="sun.java.launcher" value="SUN_STANDARD"/><property name="sun.os.patch.level" value="unknown"/><property name="jna.nosys" value="true"/><property name="java.vm.specification.name" value="Java Virtual Machine Specification"/><property name="user.dir" value="/mnt/exercism-iteration"/><property name="java.runtime.version" value="1.8.0_232-b09"/><property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"/><property name="java.endorsed.dirs" value="/usr/local/openjdk-8/jre/lib/endorsed"/><property name="os.arch" value="amd64"/><property name="java.io.tmpdir" value="/tmp"/><property name="line.separator" value="
"/><property name="java.vm.specification.vendor" value="Oracle Corporation"/><property name="log4j.ignoreTCL" value="true"/><property name="os.name" value="Linux"/><property name="sun.jnu.encoding" value="UTF-8"/><property name="jnidispatch.path" value="/root/.cache/JNA/temp/jna1920022913379980793.tmp"/><property name="java.library.path" value="/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib"/><property name="java.specification.name" value="Java Platform API Specification"/><property name="java.class.version" value="52.0"/><property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/><property name="os.version" value="4.19.128-microsoft-standard"/><property name="swoval.tmpdir" value="/root/.sbt/1.0"/><property name="user.home" value="/root"/><property name="user.timezone" value="Etc/UTC"/><property name="java.awt.printerjob" value="sun.print.PSPrinterJob"/><property name="file.encoding" value="UTF-8"/><property name="java.specification.version" value="1.8"/><property name="java.class.path" value="/usr/share/sbt/bin/sbt-launch.jar"/><property name="user.name" value="root"/><property name="jline.shutdownhook" value="false"/><property name="java.vm.specification.version" value="1.8"/><property name="sun.java.command" value="/usr/share/sbt/bin/sbt-launch.jar set offline := true test"/><property name="java.home" value="/usr/local/openjdk-8/jre"/><property name="sun.arch.data.model" value="64"/><property name="user.language" value="en"/><property name="java.specification.vendor" value="Oracle Corporation"/><property name="awt.toolkit" value="sun.awt.X11.XToolkit"/><property name="java.vm.info" value="mixed mode"/><property name="java.version" value="1.8.0_232"/><property name="java.ext.dirs" value="/usr/local/openjdk-8/jre/lib/ext:/usr/java/packages/lib/ext"/><property name="sun.boot.class.path" value="/usr/local/openjdk-8/jre/lib/resources.jar:/usr/local/openjdk-8/jre/lib/rt.jar:/usr/local/openjdk-8/jre/lib/sunrsasign.jar:/usr/local/openjdk-8/jre/lib/jsse.jar:/usr/local/openjdk-8/jre/lib/jce.jar:/usr/local/openjdk-8/jre/lib/charsets.jar:/usr/local/openjdk-8/jre/lib/jfr.jar:/usr/local/openjdk-8/jre/classes"/><property name="java.vendor" value="Oracle Corporation"/><property name="file.separator" value="/"/><property name="java.vendor.url.bug" value="http://bugreport.sun.com/bugreport/"/><property name="sun.io.unicode.encoding" value="UnicodeLittle"/><property name="sun.cpu.endian" value="little"/><property name="sun.cpu.isalist" value=""/>
</properties>
<testcase classname="HelloWorldTest" name="Say Hi!" time="0.034">

</testcase>
<system-out><![CDATA[]]></system-out>
<system-err><![CDATA[]]></system-err>
</testsuite>
15 changes: 15 additions & 0 deletions src/test/resources/outputs/output_empty.txt
Original file line number Diff line number Diff line change
@@ -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
36 changes: 22 additions & 14 deletions src/test/scala/ApplicationSpec.scala
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 535db2a

Please sign in to comment.