From acad3508b3c28ece6b9b8682445d30fe9991f8cb Mon Sep 17 00:00:00 2001 From: Tuan Pham Date: Mon, 16 Sep 2024 21:07:20 +1000 Subject: [PATCH 1/2] different colour between actual and expected for column comparer --- .../mrpowers/spark/fast/tests/ArrayUtil.scala | 75 +++---------------- .../spark/fast/tests/ColumnComparer.scala | 4 +- .../spark/fast/tests/ArrayUtilTest.scala | 2 +- 3 files changed, 14 insertions(+), 67 deletions(-) diff --git a/core/src/main/scala/com/github/mrpowers/spark/fast/tests/ArrayUtil.scala b/core/src/main/scala/com/github/mrpowers/spark/fast/tests/ArrayUtil.scala index 98b0514..7fdd719 100644 --- a/core/src/main/scala/com/github/mrpowers/spark/fast/tests/ArrayUtil.scala +++ b/core/src/main/scala/com/github/mrpowers/spark/fast/tests/ArrayUtil.scala @@ -32,70 +32,16 @@ object ArrayUtil { } def showTwoColumnString(arr: Array[(Any, Any)], truncate: Int = 20): String = { - val rows = weirdTypesToStrings(arr, truncate) - val numCols = 2 - - // Initialise the width of each column to a minimum value of '3' - val colWidths = Array.fill(numCols)(3) - - // Compute the width of each column - for (row <- rows) { - for ((cell, i) <- row.zipWithIndex) { - colWidths(i) = math.max(colWidths(i), cell.length) - } - } - - val sb = new StringBuilder - - // Create SeparateLine - val sep: String = - colWidths - .map("-" * _) - .addString(sb, "+", "+", "+\n") - .toString() - - // column names - val h: Seq[(String, Int)] = rows.head.zipWithIndex - h.map { case (cell, i) => - if (truncate > 0) { - StringUtils.leftPad(cell, colWidths(i)) - } else { - StringUtils.rightPad(cell, colWidths(i)) - } - }.addString(sb, "|", "|", "|\n") - - sb.append(sep) - - // data - rows.tail.map { row => - val color = if (row(0) == row(1)) "blue" else "red" - row.zipWithIndex - .map { case (cell, i) => - val r = if (truncate > 0) { - StringUtils.leftPad(cell.toString, colWidths(i)) - } else { - StringUtils.rightPad(cell.toString, colWidths(i)) - } - if (color == "blue") { - ufansi.Color.DarkGray(r) - } else { - ufansi.Color.Red(r) - } - } - .addString(sb, "|", "|", "|\n") - } - - sb.append(sep) - - sb.toString() + showTwoColumnStringColorCustomizable(arr, truncate = truncate) } def showTwoColumnStringColorCustomizable( arr: Array[(Any, Any)], - rowEqual: Array[Boolean], + rowEqual: Option[Array[Boolean]] = None, truncate: Int = 20, equalColor: EscapeAttr = ufansi.Color.Blue, - unequalColor: EscapeAttr = ufansi.Color.Red + unequalColorLeft: EscapeAttr = ufansi.Color.Red, + unequalColorRight: EscapeAttr = ufansi.Color.Green ): String = { val sb = new StringBuilder val numCols = 2 @@ -119,8 +65,7 @@ object ArrayUtil { .toString() // column names - val h: Seq[(String, Int)] = rows.head.zipWithIndex - h.map { case (cell, i) => + rows.head.zipWithIndex.map { case (cell, i) => if (truncate > 0) { StringUtils.leftPad(cell, colWidths(i)) } else { @@ -135,14 +80,16 @@ object ArrayUtil { row.zipWithIndex .map { case (cell, i) => val r = if (truncate > 0) { - StringUtils.leftPad(cell.toString, colWidths(i)) + StringUtils.leftPad(cell, colWidths(i)) } else { - StringUtils.rightPad(cell.toString, colWidths(i)) + StringUtils.rightPad(cell, colWidths(i)) } - if (rowEqual(j)) { + if (rowEqual.fold(row.head == row(1))(_(j))) { equalColor(r) + } else if (i == 0) { + unequalColorLeft(r) } else { - unequalColor(r) + unequalColorRight(r) } } .addString(sb, "|", "|", "|\n") diff --git a/core/src/main/scala/com/github/mrpowers/spark/fast/tests/ColumnComparer.scala b/core/src/main/scala/com/github/mrpowers/spark/fast/tests/ColumnComparer.scala index 003ac17..75c5879 100644 --- a/core/src/main/scala/com/github/mrpowers/spark/fast/tests/ColumnComparer.scala +++ b/core/src/main/scala/com/github/mrpowers/spark/fast/tests/ColumnComparer.scala @@ -89,7 +89,7 @@ trait ColumnComparer { // Diffs\n is a hack, but a newline isn't added in ScalaTest unless we add "Diffs" val mismatchMessage = "Diffs\n" + ArrayUtil.showTwoColumnStringColorCustomizable( Array((colName1, colName2)) ++ colName1Elements.zip(colName2Elements), - rowsEqual.toArray + Some(rowsEqual.toArray) ) throw ColumnMismatch(mismatchMessage) } @@ -136,7 +136,7 @@ trait ColumnComparer { // Diffs\n is a hack, but a newline isn't added in ScalaTest unless we add "Diffs" val mismatchMessage = "Diffs\n" + ArrayUtil.showTwoColumnStringColorCustomizable( Array((colName1, colName2)) ++ colName1Elements.zip(colName2Elements), - rowsEqual.toArray + Some(rowsEqual.toArray) ) throw ColumnMismatch(mismatchMessage) } diff --git a/core/src/test/scala/com/github/mrpowers/spark/fast/tests/ArrayUtilTest.scala b/core/src/test/scala/com/github/mrpowers/spark/fast/tests/ArrayUtilTest.scala index 3443ede..0fae29a 100644 --- a/core/src/test/scala/com/github/mrpowers/spark/fast/tests/ArrayUtilTest.scala +++ b/core/src/test/scala/com/github/mrpowers/spark/fast/tests/ArrayUtilTest.scala @@ -27,7 +27,7 @@ class ArrayUtilTest extends AnyFreeSpec { "dumbshowTwoColumnString" in { val arr: Array[(Any, Any)] = Array(("word1", "word2"), ("hi", "there"), ("fun", "train")) val rowEqual = Array(true, false) - println(ArrayUtil.showTwoColumnStringColorCustomizable(arr, rowEqual)) + println(ArrayUtil.showTwoColumnStringColorCustomizable(arr, Some(rowEqual))) } } From fa9fc76e90694354efffa18b7f8fe9c09774b937 Mon Sep 17 00:00:00 2001 From: Tuan Pham Date: Mon, 16 Sep 2024 21:13:03 +1000 Subject: [PATCH 2/2] Fix formatting --- .../mrpowers/spark/fast/tests/ArrayUtil.scala | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/core/src/main/scala/com/github/mrpowers/spark/fast/tests/ArrayUtil.scala b/core/src/main/scala/com/github/mrpowers/spark/fast/tests/ArrayUtil.scala index 7fdd719..adeb7c1 100644 --- a/core/src/main/scala/com/github/mrpowers/spark/fast/tests/ArrayUtil.scala +++ b/core/src/main/scala/com/github/mrpowers/spark/fast/tests/ArrayUtil.scala @@ -65,13 +65,15 @@ object ArrayUtil { .toString() // column names - rows.head.zipWithIndex.map { case (cell, i) => - if (truncate > 0) { - StringUtils.leftPad(cell, colWidths(i)) - } else { - StringUtils.rightPad(cell, colWidths(i)) + rows.head.zipWithIndex + .map { case (cell, i) => + if (truncate > 0) { + StringUtils.leftPad(cell, colWidths(i)) + } else { + StringUtils.rightPad(cell, colWidths(i)) + } } - }.addString(sb, "|", "|", "|\n") + .addString(sb, "|", "|", "|\n") sb.append(sep)