From 6b0e8c40477194c19e24e522a4eb8d4cfb49f909 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Mon, 18 Jun 2012 22:59:50 -0400 Subject: [PATCH] Print an ugly "diff" when an equivalents test fails Helpful for debugging failed tests. --- .../config/impl/EquivalentsTest.scala | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/config/src/test/scala/com/typesafe/config/impl/EquivalentsTest.scala b/config/src/test/scala/com/typesafe/config/impl/EquivalentsTest.scala index cda1e7cb..294d9744 100644 --- a/config/src/test/scala/com/typesafe/config/impl/EquivalentsTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/EquivalentsTest.scala @@ -50,6 +50,41 @@ class EquivalentsTest extends TestUtils { postParse(ConfigFactory.parseFile(f, options).root) } + private def printIndented(indent: Int, s: String): Unit = { + for (i <- 0 to indent) + System.err.print(' ') + System.err.println(s) + } + + private def showDiff(a: ConfigValue, b: ConfigValue, indent: Int = 0): Unit = { + if (a != b) { + if (a.valueType != b.valueType) { + printIndented(indent, "- " + a.valueType) + printIndented(indent, "+ " + b.valueType) + } else if (a.valueType == ConfigValueType.OBJECT) { + import scala.collection.JavaConverters._ + printIndented(indent, "OBJECT") + val aS = a.asInstanceOf[ConfigObject].asScala + val bS = b.asInstanceOf[ConfigObject].asScala + for (aKV <- aS) { + val bVOption = bS.get(aKV._1) + if (Some(aKV._2) != bVOption) { + printIndented(indent + 1, aKV._1) + if (bVOption.isDefined) { + showDiff(aKV._2, bVOption.get, indent + 2) + } else { + printIndented(indent + 2, "- " + aKV._2) + printIndented(indent + 2, "+ (missing)") + } + } + } + } else { + printIndented(indent, "- " + a) + printIndented(indent, "+ " + b) + } + } + } + // would like each "equivNN" directory to be a suite and each file in the dir // to be a test, but not sure how to convince junit to do that. @Test @@ -71,7 +106,13 @@ class EquivalentsTest extends TestUtils { fileCount += 1 val value = parse(testFile) describeFailure(testFile.getPath()) { - assertEquals(original, value) + try { + assertEquals(original, value) + } catch { + case e => + showDiff(original, value) + throw e + } } // check that all .json files can be parsed as .conf, @@ -79,7 +120,13 @@ class EquivalentsTest extends TestUtils { if (testFile.getName().endsWith(".json")) { val parsedAsConf = parse(ConfigSyntax.CONF, testFile) describeFailure(testFile.getPath() + " parsed as .conf") { - assertEquals(original, parsedAsConf) + try { + assertEquals(original, parsedAsConf) + } catch { + case e => + showDiff(original, parsedAsConf) + throw e + } } } }