Skip to content

Instantly share code, notes, and snippets.

@andyczerwonka
Created June 13, 2020 14:25
Show Gist options
  • Save andyczerwonka/206043f615ffb5cd35e7f80cbca2fe54 to your computer and use it in GitHub Desktop.
Save andyczerwonka/206043f615ffb5cd35e7f80cbca2fe54 to your computer and use it in GitHub Desktop.

Revisions

  1. andyczerwonka created this gist Jun 13, 2020.
    47 changes: 47 additions & 0 deletions BusinessRuleThoughts.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    package io.citrine.orion.core.domain

    import io.citrine.testing.BaseUnitTest

    import scala.collection.mutable.ListBuffer

    class BusinessRuleTest extends BaseUnitTest {

    trait StatusInfo {
    def write(msg: String)
    def msgs(): Seq[String]
    }

    class StatusInfoWriter extends StatusInfo {
    private val buffer = ListBuffer[String]()
    override def write(msg: String): Unit = buffer += msg
    override def msgs(): List[String] = buffer.toList
    }

    implicit val statusInfo = new StatusInfoWriter

    case class Row(contents: String)

    def rule(desc: String)(predicate: => Boolean)(implicit statusInfo: StatusInfo): Boolean = {
    val ex = predicate
    val prefix = if (ex) "Passed:" else "Failed:"
    statusInfo.write(s"$prefix $desc")
    ex
    }

    test("allow less than 100 rows") {
    val rows = List(Row("1"))
    rule("Limit rows to 100") {
    rows.length < 100
    } shouldBe true
    statusInfo.msgs contains ("Passed: Limit rows to 100")
    }

    test("fail when rows > 100") {
    val rows = for (i <- 0 to 101) yield Row(s"$i")
    rule("Limit rows to 100") {
    rows.length < 100
    } shouldBe false
    statusInfo.msgs contains ("Failed: Limit rows to 100")
    }

    }