Skip to content

Commit

Permalink
Added support for the modes
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Feb 10, 2024
1 parent 4924cc5 commit 2dd0516
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ case object GoldenAccept extends GoldenMode
case object GoldenGenerate extends GoldenMode
case object GoldenCheck extends GoldenMode

sealed trait GoldenDiff
case object GoldenScalactic extends GoldenDiff

// this is probably core!
final case class GoldenConfig(
mode: GoldenMode
mode: GoldenMode,
diff: GoldenDiff,
)
object GoldenConfig {
val default = GoldenConfig(mode = GoldenCheck)
val default = GoldenConfig(
mode = GoldenCheck,
diff = GoldenScalactic,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,53 @@ import org.scalatest.matchers.MatchResult
import org.scalatest.matchers.Matcher

import scala.io.Source
import scala.util.Try
import scala.util.Success
import scala.util.Failure
import java.io.FileWriter
import java.io.File
import java.io.BufferedWriter

// TODO: actually, it would be good if the status' of the tests can change
// depending on mode:
// in ACCEPT, tests that didn't match and were overwritten should be IGNORED
// in GENERATE, tests that were made now should be PENDING?
trait GoldenMatchers extends HasGoldenConfig {
def matchGolden(filename: String): Matcher[Any] = new Matcher[Any] {
private def compare(left: String, right: String) = new EqualMatchResult(
matches = left == right,
rawFailureMessage = s"The contents of $filename did not match",
rawNegatedFailureMessage = s"The contents of $filename matched",
Vector(left, right),
Vector(left, right),
)

private def generate(left: String) = {
val res = Try {
val buff = new BufferedWriter(new FileWriter(new File(filename)))
buff.write(left)
buff.close()
}
MatchResult(
matches = res.isSuccess,
rawFailureMessage = s"The golden test $filename could not be generated",
rawNegatedFailureMessage = s"The golden test $filename was generated",
)
}

def apply(left: Any): MatchResult = {
val leftStr = left.toString
val rightStr = Source.fromFile(filename).mkString
new EqualMatchResult(
matches = leftStr == rightStr,
rawFailureMessage = s"The contents of $filename did not match",
rawNegatedFailureMessage = s"The contents of $filename matched",
Vector(leftStr, rightStr),
Vector(leftStr, rightStr),
)
(goldenConfig.mode, Try(Source.fromFile(filename).mkString)) match {
case (GoldenGenerate | GoldenCheck, Success(rightStr)) => compare(leftStr, rightStr)
case (GoldenAccept, _) => generate(leftStr)
case (GoldenGenerate, Failure(_)) => generate(leftStr)
case (GoldenCheck, Failure(_)) =>
MatchResult(
matches = false,
rawFailureMessage = s"The golden test $filename does not exist",
rawNegatedFailureMessage = s"The golden test $filename existed?",
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ trait HasGoldenConfig extends Suite {
case Some("generate") => GoldenGenerate
case _ => GoldenCheck
}
// TODO: diffing modes
goldenConfig = goldenConfig.copy(mode = goldenMode)
super.run(testName, args)
}
Expand Down

0 comments on commit 2dd0516

Please sign in to comment.