From 804af29a7d79b990a52cd6cb15e7f9480f1cf07a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93lafur=20P=C3=A1ll=20Geirsson?= Date: Wed, 18 Nov 2020 05:21:05 +0100 Subject: [PATCH] Fix bug related to escaping single quotes inside strings. Previously, MUnit escaped single quotes inside strings. The escaping is only needed for single-quote characters. --- .../main/scala/munit/internal/console/Printers.scala | 4 ++-- tests/shared/src/test/scala/munit/PrintersSuite.scala | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/munit/shared/src/main/scala/munit/internal/console/Printers.scala b/munit/shared/src/main/scala/munit/internal/console/Printers.scala index 457255d3..3196f42e 100644 --- a/munit/shared/src/main/scala/munit/internal/console/Printers.scala +++ b/munit/shared/src/main/scala/munit/internal/console/Printers.scala @@ -34,7 +34,8 @@ object Printers { case x: Printable => x.print(out, indent) case x: Char => out.append('\'') - printChar(x, out) + if (x == '\'') out.append("\\'") + else printChar(x, out) out.append('\'') case x: Byte => out.append(x.toString()) case x: Short => out.append(x.toString()) @@ -190,7 +191,6 @@ object Printers { private def printChar(c: Char, sb: StringBuilder) = (c: @switch) match { case '"' => sb.append("\\\"") - case '\'' => sb.append("\\'") case '\\' => sb.append("\\\\") case '\b' => sb.append("\\b") case '\f' => sb.append("\\f") diff --git a/tests/shared/src/test/scala/munit/PrintersSuite.scala b/tests/shared/src/test/scala/munit/PrintersSuite.scala index ca035892..4a692af4 100644 --- a/tests/shared/src/test/scala/munit/PrintersSuite.scala +++ b/tests/shared/src/test/scala/munit/PrintersSuite.scala @@ -38,6 +38,16 @@ class PrintersSuite extends FunSuite { self => '\n', "'\\n'" ) + check( + "char-single-quote", + '\'', + "'\\''" + ) + check( + "string-single-quote", + "'a'", + "\"'a'\"" + ) check( "map", Map(1 -> 2, 3 -> 4, 5 -> Map(6 -> 7)),