Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions test-tests/shared/src/test/scala/zio/test/PrettyPrintSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package zio.test

object PrettyPrintSpec extends ZIOBaseSpec {

def spec: ZSpec[Environment, Failure] = suite("PrettyPrint")(
test("String") {
assertTrue(PrettyPrint("A String") == "\"A String\"")
},
test("List") {
assertTrue(PrettyPrint(List(1, 2, 3)) == "List(1, 2, 3)")
},
test("List of String") {
assertTrue(PrettyPrint(List("1", "2", "3")) == "List(\"1\", \"2\", \"3\")")
},
test("Array of String") {
assertTrue(PrettyPrint(Array("1", "2", "3")) == "Array(\"1\", \"2\", \"3\")")
},
test("Map") {
val expected = """
Map(
"name" -> "Biff",
"age" -> 123,
"inventory" -> Map(
"food" -> "Cake",
"candy" -> "Chocolate"
)
)
""".trim
assertTrue(
PrettyPrint(
Map("name" -> "Biff", "age" -> 123, "inventory" -> Map("food" -> "Cake", "candy" -> "Chocolate"))
) == expected
)
},
test("Case Class") {
final case class Person(name: String, age: Int)
val expected = """
Person(
name = "Glenda",
age = 123
)
""".trim
assertTrue(
PrettyPrint(
Person("Glenda", 123)
) == expected
)
} @@ TestAspect.exceptScala211 @@ TestAspect.exceptScala212
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ object SmartAssertionSpec extends ZIOBaseSpec {
val l1 = Set(1, 2, 3, 4)
val l2 = Set(1, 2, 8, 4, 5)
assertTrue(l1 == l2)
} @@ failing,
test("Map diffs") {
val l1 = Map("name" -> "Kit", "age" -> "100")
val l2 = Map("name" -> "Bill", "rage" -> "9000")
assertTrue(l1 == l2)
} @@ failing
),
test("Package qualified identifiers") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package zio.test

trait PrettyPrintVersionSpecific {
def labels(product: Product): Iterator[String] = {
val _ = product
Iterator.continually("")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package zio.test

trait PrettyPrintVersionSpecific {
def labels(product: Product): Iterator[String] = {
val _ = product
Iterator.continually("")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package zio.test

trait PrettyPrintVersionSpecific {
def labels(product: Product): Iterator[String] = product.productElementNames
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package zio.test

trait PrettyPrintVersionSpecific {
def labels(product: Product): Iterator[String] = product.productElementNames
}
8 changes: 0 additions & 8 deletions test/shared/src/main/scala/zio/test/ErrorMessage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,3 @@ sealed trait ErrorMessage { self =>
}

}

private[zio] object PrettyPrint {
def apply(any: Any): String = any match {
case array: Array[_] => array.mkString("Array(", ", ", ")")
case string: String => string.replace("\"", """\"""").mkString("\"", "", "\"")
case other => other.toString
}
}
45 changes: 45 additions & 0 deletions test/shared/src/main/scala/zio/test/PrettyPrint.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package zio.test

/**
* PrettyPrint will attempt to render a Scala value as the syntax used to create
* that value. This makes it easier to copy-paste from values printed to the
* console during tests back into runnable code.
*/
private[zio] object PrettyPrint extends PrettyPrintVersionSpecific {
def apply(any: Any): String = any match {
case array: Array[_] =>
array.map(PrettyPrint.apply).mkString("Array(", ", ", ")")

case iterable: Seq[_] =>
iterable.map(PrettyPrint.apply).mkString(s"${className(iterable)}(", ", ", ")")

case map: Map[_, _] =>
val body = map.map { case (key, value) => s"${PrettyPrint(key)} -> ${PrettyPrint(value)}" }
s"""Map(
${indent(body.mkString(",\n"))}
)"""

case product: Product =>
val name = product.productPrefix
val body = labels(product).zip(product.productIterator).map { case (key, value) =>
s"$key = ${PrettyPrint(value)}"
}
s"""$name(
${indent(body.mkString(",\n"))}
)"""

case string: String =>
string.replace("\"", """\"""").mkString("\"", "", "\"")

case other => other.toString
}

private def indent(string: String, n: Int = 2): String =
string.split("\n").map((" " * n) + _).mkString("\n")

private def className(any: Any): String = any match {
case _: List[_] => "List"
case other => other.getClass.getSimpleName
}

}