Created
March 3, 2021 23:51
-
-
Save darkfrog26/da9eebb9f70e9b3ac09e5e066ddc4f44 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
Allows me to write MUnit tests like: | |
"Such and such" should { | |
"do something" in { | |
value should be(123) | |
} | |
} | |
*/ | |
trait Spec extends munit.FunSuite { | |
private var parts = List.empty[String] | |
implicit class Buildable(s: String) { | |
def should(f: => Unit): Unit = { | |
val current = parts | |
parts = "should" :: s :: parts | |
try { | |
f | |
} finally { | |
parts = current | |
} | |
} | |
def in(f: => Unit): Unit = { | |
val name = (s :: parts).reverse.mkString(" ") | |
test(name)(f) | |
} | |
} | |
implicit def t2Assertable[T](t: T): Assertable[T] = Assertable[T](t, this) | |
def be[T](expected: T): Assertion[T] = BeAssertion[T](expected, this) | |
} | |
case class Assertable[T](value: T, suite: FunSuite) { | |
def should(assertion: Assertion[T]): Unit = assertion.assertWith(value) | |
} | |
trait Assertion[T] { | |
def assertWith(value: T): Unit | |
} | |
case class BeAssertion[T](expected: T, suite: FunSuite) extends Assertion[T] { | |
override def assertWith(value: T): Unit = suite.assertEquals(value, expected) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment