Skip to content

Commit

Permalink
refactor clues
Browse files Browse the repository at this point in the history
  • Loading branch information
majk-p committed Apr 23, 2024
1 parent 129a98c commit 374926c
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package munit.internal

import munit.diff.Clue
import munit.Clue
import munit.Location
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package munit.internal

import munit.diff.Clue
import munit.Clue
import munit.Location
import scala.quoted._
import scala.language.experimental.macros
Expand Down
4 changes: 2 additions & 2 deletions munit/shared/src/main/scala/munit/Assertions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package munit
import munit.internal.console.{Lines, StackTraces}
import munit.diff.console.Printers
import munit.diff.Printer
import munit.diff.Clue
import munit.diff.Clues
import munit.Clue
import munit.Clues
import munit.diff.EmptyPrinter

import scala.reflect.ClassTag
Expand Down
20 changes: 10 additions & 10 deletions munit/shared/src/main/scala/munit/Clue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package munit

import munit.internal.MacroCompat

// class Clue[+T](
// val source: String,
// val value: T,
// val valueType: String
// ) extends Serializable {
// override def toString(): String = s"Clue($source, $value)"
// }
class Clue[+T](
val source: String,
val value: T,
val valueType: String
) extends Serializable {
override def toString(): String = s"Clue($source, $value)"
}
object Clue extends MacroCompat.ClueMacro {
@deprecated("use fromValue instead", "1.0.0")
def empty[T](value: T): munit.diff.Clue[T] = fromValue(value)
def fromValue[T](value: T): munit.diff.Clue[T] =
new munit.diff.Clue("", value, "")
def empty[T](value: T): Clue[T] = fromValue(value)
def fromValue[T](value: T): Clue[T] =
new Clue("", value, "")
}
47 changes: 38 additions & 9 deletions munit/shared/src/main/scala/munit/Clues.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
package munit

import munit.internal.console.Printers

// class Clues(val values: List[Clue[_]]) {
// override def toString(): String = Printers.print(this)
// }
// object Clues {
// def empty: Clues = new Clues(List())
// def fromValue[T](value: T): Clues = new Clues(List(Clue.fromValue(value)))
// }
import munit.diff.console.Printers
import munit.diff.NextPrintable

class Clues(val values: List[Clue[_]]) extends NextPrintable {

override def print(
out: StringBuilder,
indent: Int,
continue: (Any, Int) => Unit
): Unit = {
val nextIndent = indent + Printers.indentStep
Printers.printApply(
"Clues",
values.iterator,
out,
indent,
nextIndent,
open = " {",
close = "}",
comma = ""
) { clue =>
if (clue.source.nonEmpty) {
out.append(clue.source)
}
if (clue.valueType.nonEmpty) {
out.append(": ").append(clue.valueType)
}
out.append(" = ")
continue(clue.value, nextIndent)
}
}

override def toString(): String = Printers.print(this)
}
object Clues {
def empty: Clues = new Clues(List())
def fromValue[T](value: T): Clues = new Clues(List(Clue.fromValue(value)))
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package munit.internal

import munit.diff.Clue
import munit.Clue
import munit.Location
import scala.reflect.macros.blackbox.Context
import scala.reflect.macros.TypecheckException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import munit.Location
import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.util.control.NonFatal
import munit.diff.Clues
import munit.Clues

class Lines extends Serializable {
private val filecache = mutable.Map.empty[Path, Array[String]]
Expand Down
13 changes: 0 additions & 13 deletions scala-diff/shared/src/main/scala/munit/diff/Clue.scala

This file was deleted.

11 changes: 0 additions & 11 deletions scala-diff/shared/src/main/scala/munit/diff/Clues.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package munit.diff

/**
* Don't override this ever
*/
trait NextPrintable {
def print(out: StringBuilder, indent: Int, continue: (Any, Int) => Unit): Unit
}
33 changes: 7 additions & 26 deletions scala-diff/shared/src/main/scala/munit/diff/console/Printers.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Adaptation of https://github.com/lihaoyi/PPrint/blob/e6a918c259ed7ae1998bbf58c360334a3f0157ca/pprint/src/pprint/Walker.scala
package munit.diff.console

import munit.diff.{EmptyPrinter, Printable, Printer}
import munit.diff.{EmptyPrinter, Printable, NextPrintable, Printer}

import scala.annotation.switch
import munit.diff.Clues
import munit.diff.internal.Compat

object Printers {
val indentStep = 2

/** Pretty-prints the value in a format that's optimized for producing diffs */
def print(any: Any, printer: Printer = EmptyPrinter): String = {
var height = printer.height
val out = new StringBuilder()
val indentStep = 2

def loop(a: Any, indent: Int): Unit = {
height -= 1
if (height < 0) {
Expand All @@ -24,8 +24,9 @@ object Printers {
val isDone = printer.print(a, out, indent)
if (!isDone) {
a match {
case null => out.append("null")
case x: Printable => x.print(out, indent)
case null => out.append("null")
case x: Printable => x.print(out, indent)
case x: NextPrintable => x.print(out, indent, loop)
case x: Char =>
out.append('\'')
if (x == '\'') out.append("\\'")
Expand All @@ -38,26 +39,6 @@ object Printers {
case x: Float => out.append(x.toString())
case x: Double => out.append(x.toString())
case x: String => printString(x, out, printer)
case x: Clues =>
printApply(
"Clues",
x.values.iterator,
out,
indent,
nextIndent,
open = " {",
close = "}",
comma = ""
) { clue =>
if (clue.source.nonEmpty) {
out.append(clue.source)
}
if (clue.valueType.nonEmpty) {
out.append(": ").append(clue.valueType)
}
out.append(" = ")
loop(clue.value, nextIndent)
}
case None =>
out.append("None")
case Nil =>
Expand Down Expand Up @@ -120,7 +101,7 @@ object Printers {
munit.diff.console.AnsiColors.filterAnsi(out.toString())
}

private def printApply[T](
private[munit] def printApply[T](
prefix: String,
it: Iterator[T],
out: StringBuilder,
Expand Down
4 changes: 2 additions & 2 deletions tests/shared/src/test/scala/munit/TypeCheckSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TypeCheckSuite extends FunSuite {
|
|The following import might make progress towards fixing the problem:
|
| import munit.diff.Clue.generate
| import munit.Clue.generate
|
|msg.foobar
| ^
Expand Down Expand Up @@ -84,7 +84,7 @@ class TypeCheckSuite extends FunSuite {
|
|The following import might make progress towards fixing the problem:
|
| import munit.diff.Clue.generate
| import munit.Clue.generate
|
|val n: Int = msg
| ^
Expand Down

0 comments on commit 374926c

Please sign in to comment.