Skip to content

Commit

Permalink
Start on tests.yaml and YamlTests suite
Browse files Browse the repository at this point in the history
  • Loading branch information
ysthakur committed Feb 8, 2023
1 parent 459c4ee commit a22d591
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .scalafmt.conf
Expand Up @@ -15,7 +15,7 @@ rewrite.scala3.removeEndMarkerMaxLines = 14
rewrite.rules = [Imports]
rewrite.imports.sort = original
# Any Vyxal imports go at the top
rewrite.imports.groups = [["vyxal\\..*"]]
rewrite.imports.groups = [["vyxal\\..*"], ["scala\\..*", "java\\..*"]]
rewrite.imports.contiguousGroups = no

fileOverride {
Expand Down
3 changes: 2 additions & 1 deletion build.sbt
Expand Up @@ -69,7 +69,8 @@ lazy val vyxal = crossProject(JSPlatform, JVMPlatform, NativePlatform)
assembly / assemblyJarName := s"vyxal-$vyxalVersion.jar",
libraryDependencies ++= Seq(
// For command line parsing
"com.github.scopt" %% "scopt" % "4.1.0"
"com.github.scopt" %% "scopt" % "4.1.0",
"org.yaml" % "snakeyaml" % "1.33" % Test
)
)
.jsSettings(
Expand Down
5 changes: 5 additions & 0 deletions jvm/src/test/resources/tests.yaml
@@ -0,0 +1,5 @@
- element: "+"
tests:
- ["1", 2] : "12"
- ["foo", "bar"]: "foobar"
- [[1, "a", []], 3]: [4, "a3", []]
81 changes: 81 additions & 0 deletions jvm/src/test/scala/YamlTests.scala
@@ -0,0 +1,81 @@
package vyxal

import java.util.{ArrayList, Map as JavaMap}
import scala.jdk.CollectionConverters.*

import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.Checkpoints.Checkpoint
import org.yaml.snakeyaml.Yaml

/** Tests for specific elements */
class YamlTests extends AnyFunSuite:
val TestsFile = "tests.yaml"

{
val yaml = new Yaml()
val inputStream = this
.getClass()
.getClassLoader()
.getResourceAsStream(TestsFile)
val parsed: ArrayList[JavaMap[String, Any]] = yaml.load(inputStream)

for elementInfo <- parsed.asScala do
val symbol = elementInfo.get("element").asInstanceOf[String]
val testsInfo = elementInfo
.get("tests")
.asInstanceOf[ArrayList[Any]]
.asScala
test(s"Element $symbol") {
val cp = Checkpoint()
for testInfo <- testsInfo do
(testInfo: @unchecked) match
case testInfo: JavaMap[?, ?] =>
if testInfo.containsKey("inputs") then
val inputs = testInfo
.get("inputs")
.asInstanceOf[ArrayList[Any]]
.asScala
.toSeq
.map(javaToVyxal)
val output = javaToVyxal(testInfo.get("outputs"))
cp {
given ctx: Context = Context(inputs = inputs)
Interpreter.execute(symbol)
assertResult(output)(ctx.peek)
}
else if testInfo.size() == 1 then
// Normal [...input]: output test
val inputs = testInfo
.keySet()
.iterator()
.next()
.asInstanceOf[ArrayList[Any]]
.asScala
.toSeq
.map(javaToVyxal)
val output = javaToVyxal(testInfo.values().iterator().next())
cp {
given ctx: Context = Context(inputs = inputs)
Interpreter.execute(symbol)
assertResult(output)(ctx.peek)
}
else
throw new Error(
s"Invalid test format (no inputs given): $testInfo"
)
case _ =>
throw new Error(
s"Invalid test format: $testInfo (${testInfo.getClass})"
)
end for
cp.reportAll()
}
end for
}

private def javaToVyxal(obj: Any): VAny = obj match
case s: String => s
case i: Int => VNum(i)
case d: Double => VNum(d)
case list: ArrayList[?] => VList(list.asScala.toSeq.map(javaToVyxal)*)
end YamlTests

0 comments on commit a22d591

Please sign in to comment.