Last active
May 9, 2020 19:35
-
-
Save neko-kai/cac921d00c74fcf643e3d072601ffa8c to your computer and use it in GitHub Desktop.
Dotty immutable test syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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