Skip to content

Instantly share code, notes, and snippets.

@neko-kai
Last active May 9, 2020 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neko-kai/cac921d00c74fcf643e3d072601ffa8c to your computer and use it in GitHub Desktop.
Save neko-kai/cac921d00c74fcf643e3d072601ffa8c to your computer and use it in GitHub Desktop.
Dotty immutable test syntax
object MyTest extends Test {
val suite =
"MySuite"
> "should do x" {
assert(2 + 2 == 4)
}
> "should do y" {
assert(2 + 2 == 8)
}
}
trait Test {
def suite: SuiteSyntax.type ?=> Suite
object SuiteSyntax {
def (name: String) > (firstTest: TestSyntax.type ?=> Test) = Suite(name, Seq(firstTest(using TestSyntax)))
}
object TestSyntax {
def (name: String).apply(f: => Unit): Test = Test(name, () => f)
}
final case class Suite(name: String, tests: Seq[Test]) {
def >(f: TestSyntax.type ?=> Test): Suite = copy(tests = tests :+ f(using TestSyntax))
}
final case class Test(name: String, f: () => Unit)
def main(a: Array[String]): Unit = {
locally(suite(using SuiteSyntax).tests.foreach {
t =>
println(s"TEST: ${t.name}")
util.Try(t.f()).fold(
_ => println(s"FAILED: ${t.name}"),
_ => println(s"PASSED: ${t.name}")
)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment