Skip to content

Instantly share code, notes, and snippets.

@darkfrog26
Created March 3, 2021 23:51
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 darkfrog26/da9eebb9f70e9b3ac09e5e066ddc4f44 to your computer and use it in GitHub Desktop.
Save darkfrog26/da9eebb9f70e9b3ac09e5e066ddc4f44 to your computer and use it in GitHub Desktop.
/*
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