Skip to content

Commit

Permalink
More threeification
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Apr 11, 2021
1 parent 6fe95b4 commit d977567
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 31 deletions.
8 changes: 4 additions & 4 deletions .fury/config
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#
# For more information, please visit https://propensive.com/fury/
#
layerRef QmZWLcDEzNPSNu7hEhiR2nsFwiXpTXiP1oZcuFeXMQyGvT
layerRef Qmc1dVLyQaQ4nLDB66iAKSfPdnQyjAbESDf5GfNokr5bCT
published Some url fury://vent.dev/propensive/probably
version 8
layerRef QmYizRUALexSXMhx8zZz3r2PQ9wLqHMePzdLp6adogT79Y
expiry Some 1620644586373
version 9
layerRef QmQuBAJbs8KC2Sjszi9uSuPTkUSPZdvw54QiBfHJoLUwai
expiry Some 1620646572554
# vim: set noai ts=12 sw=12:
Binary file modified .fury/layers.db
Binary file not shown.
14 changes: 7 additions & 7 deletions src/cli/cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ object Suite:
def show(report: Report): String =
val simple = report.results.forall(_.count == 1)

given AnsiShow[Outcome] = _ match
case Outcome.Passed => pass
case Outcome.FailsAt(Fail(map, _), 1) => fail
case Outcome.FailsAt(ThrowsInCheck(_, _, _), n) => checkThrows
case Outcome.FailsAt(Throws(exception, map), 1) => throws
case Outcome.FailsAt(_, n) => tailFail
case Outcome.Mixed => mixed
given AnsiShow[Outcome] =
case Outcome.Passed => pass
case Outcome.FailsAt(Datapoint.Fail(map, _), 1) => fail
case Outcome.FailsAt(Datapoint.ThrowsInCheck(_, _, _), n) => checkThrows
case Outcome.FailsAt(Datapoint.Throws(exception, map), 1) => throws
case Outcome.FailsAt(_, n) => tailFail
case Outcome.Mixed => mixed

val status = Heading[Summary, Outcome]("", _.outcome)
val hash = Heading[Summary, String]("Hash", v => Runner.shortDigest(v.name))
Expand Down
29 changes: 12 additions & 17 deletions src/core/probably.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,18 @@ object Runner:
case Mixed

def failed: Boolean = !passed

def passed: Boolean = this match
case Passed => true
case Mixed => false
case FailsAt(_, _) => false
def passed: Boolean = this == Passed

def debug: String = this match
case FailsAt(datapoint, count) => datapoint.map.map { case (k, v) => s"$k=$v" }.mkString(" ")
case _ => ""


sealed abstract class Datapoint(val map: Map[String, String])
case object Pass extends Datapoint(Map())

case class Fail(failMap: Map[String, String], index: Int) extends Datapoint(failMap)
case class Throws(exception: Throwable, throwMap: Map[String, String]) extends Datapoint(throwMap)
case class ThrowsInCheck(exception: Exception, throwMap: Map[String, String], index: Int) extends Datapoint(throwMap)
enum Datapoint(val map: Map[String, String]):
case Pass extends Datapoint(Map())
case Fail(failMap: Map[String, String], index: Int) extends Datapoint(failMap)
case Throws(exception: Throwable, throwMap: Map[String, String]) extends Datapoint(throwMap)
case ThrowsInCheck(exception: Exception, throwMap: Map[String, String], index: Int) extends Datapoint(throwMap)

object Show:
given Show[Int] = _.toString
Expand Down Expand Up @@ -119,13 +114,13 @@ class Runner(specifiedTests: Set[TestId] = Set()) extends Dynamic {

def check(preds: (Type => Boolean)*): Type =
def handler(index: Int): PartialFunction[Throwable, Datapoint] =
case e: Exception => ThrowsInCheck(e, map, index)
case e: Exception => Datapoint.ThrowsInCheck(e, map, index)

def makeDatapoint(preds: Seq[Type => Boolean], count: Int, datapoint: Datapoint, value: Type): Datapoint =
try
if preds.isEmpty then datapoint
else if preds.head(value) then makeDatapoint(preds.tail, count + 1, Pass, value)
else Fail(map, count)
else if preds.head(value) then makeDatapoint(preds.tail, count + 1, Datapoint.Pass, value)
else Datapoint.Fail(map, count)
catch handler(count)


Expand All @@ -135,10 +130,10 @@ class Runner(specifiedTests: Set[TestId] = Set()) extends Dynamic {

value match
case Success(value) =>
record(this, time, makeDatapoint(preds, 0, Pass, value))
record(this, time, makeDatapoint(preds, 0, Datapoint.Pass, value))
value
case Failure(exception) =>
record(this, time, Throws(exception, map))
record(this, time, Datapoint.Throws(exception, map))
throw exception

def report(): Report = Report(results.values.to(List))
Expand Down Expand Up @@ -170,7 +165,7 @@ case class Summary(id: TestId, name: String, count: Int, tmin: Long, ttot: Long,
Summary(id, name, count + 1, tmin min duration, ttot + duration, tmax max duration, outcome match
case Outcome.FailsAt(dp, c) => Outcome.FailsAt(dp, c)
case Outcome.Passed => datapoint match
case Pass => Outcome.Passed
case Datapoint.Pass => Outcome.Passed
case other => Outcome.FailsAt(other, count + 1)
case Outcome.Mixed => Outcome.FailsAt(datapoint, 0)
)
Expand Down
6 changes: 3 additions & 3 deletions src/test/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ object Main extends Suite("Probably Tests"):

report.results.head.outcome
}.assert {
case Outcome.FailsAt(Fail(map, _), _) if map("i") == "6" => true
case Outcome.FailsAt(Datapoint.Fail(map, _), _) if map("i") == "6" => true
case _ => false
}

Expand Down Expand Up @@ -130,9 +130,9 @@ object Main extends Suite("Probably Tests"):
test("assert with 2 predicates") {
val report = reportTest(_("test double")(0.001).assert(_ >= 0.0, _ < 0.0))
report.results.head.outcome
}.assert(_ == Outcome.FailsAt(Fail(Map(), 1), 1))
}.assert(_ == Outcome.FailsAt(Datapoint.Fail(Map(), 1), 1))

test("assert with 3 predicates") {
val report = reportTest(_("test double")(0.001).assert(_ >= 0.0, _ <= 1.0, _ < 0.0))
report.results.head.outcome
}.assert(_ == Outcome.FailsAt(Fail(Map(), 2), 1))
}.assert(_ == Outcome.FailsAt(Datapoint.Fail(Map(), 2), 1))

0 comments on commit d977567

Please sign in to comment.