From 6ee01f23a42c944cbbddcd22e20f51d13410cdaa Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Tue, 28 Apr 2020 15:02:28 -0700 Subject: [PATCH] DirectTest checks compiler options Previously, a bad compiler option was ignored. Add a common idiom to StreamCapture to set stdout; the idiom may be suboptimal for testing. Convert a partest to junit for regex, and eliminate one check file. (cherry picked from commit 580ed0706d35e8320a248511165d29388a17f3d5) --- test/files/run/ReplacementMatching.scala | 47 ------------------- test/files/run/t6178.check | 1 - test/files/run/t6178.scala | 6 +-- .../junit/scala/util/matching/RegexTest.scala | 31 ++++++++++++ 4 files changed, 34 insertions(+), 51 deletions(-) delete mode 100644 test/files/run/ReplacementMatching.scala delete mode 100644 test/files/run/t6178.check diff --git a/test/files/run/ReplacementMatching.scala b/test/files/run/ReplacementMatching.scala deleted file mode 100644 index 81034aa51040..000000000000 --- a/test/files/run/ReplacementMatching.scala +++ /dev/null @@ -1,47 +0,0 @@ - - - -import util.matching._ - - - - -object Test { - - def main(args: Array[String]) { - replacementMatching - groupsMatching - } - - def replacementMatching { - val regex = """\$\{(.+?)\}""".r - val replaced = regex.replaceAllIn("Replacing: ${main}. And another method: ${foo}.", - (m: util.matching.Regex.Match) => { - val identifier = m.group(1) - identifier - }) - assert(replaced == "Replacing: main. And another method: foo.") - - val regex3 = """\$\{(.+?)\}""".r - val replaced3 = regex3.replaceSomeIn("Replacing: ${main}. And another: ${foo}.", (m: util.matching.Regex.Match) => { - val id = m.group(1) - if (id.startsWith("m")) Some(id) else None - }) - assert(replaced3 == "Replacing: main. And another: ${foo}.") - } - - def groupsMatching { - val Date = """(\d+)/(\d+)/(\d+)""".r - for (Regex.Groups(a, b, c) <- Date findFirstMatchIn "1/1/2001 marks the start of the millennium. 31/12/2000 doesn't.") { - assert(a == "1") - assert(b == "1") - assert(c == "2001") - } - for (Regex.Groups(a, b, c) <- (Date findAllIn "1/1/2001 marks the start of the millennium. 31/12/2000 doesn't.").matchData) { - assert(a == "1" || a == "31") - assert(b == "1" || b == "12") - assert(c == "2001" || c == "2000") - } - } - -} diff --git a/test/files/run/t6178.check b/test/files/run/t6178.check deleted file mode 100644 index d8263ee98605..000000000000 --- a/test/files/run/t6178.check +++ /dev/null @@ -1 +0,0 @@ -2 \ No newline at end of file diff --git a/test/files/run/t6178.scala b/test/files/run/t6178.scala index 41e148af9133..a9e1a1118626 100644 --- a/test/files/run/t6178.scala +++ b/test/files/run/t6178.scala @@ -2,6 +2,6 @@ import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentMirror => cm} object Test extends App { - val plus = typeOf[java.lang.String].member(TermName("$plus")).asMethod - println(cm.reflect("").reflectMethod(plus).apply("2")) -} \ No newline at end of file + val plusMethod = typeOf[java.lang.String].member(TermName("$plus")).asMethod + assert(cm.reflect("").reflectMethod(plusMethod).apply("2") == "2") +} diff --git a/test/junit/scala/util/matching/RegexTest.scala b/test/junit/scala/util/matching/RegexTest.scala index 15ba6c1f185b..14673a0170bd 100644 --- a/test/junit/scala/util/matching/RegexTest.scala +++ b/test/junit/scala/util/matching/RegexTest.scala @@ -176,4 +176,35 @@ class RegexTest { assertEquals("aaaaa", aes) } } + + @Test def replacementMatching(): Unit = { + val regex = """\$\{(.+?)\}""".r + val replaced = regex.replaceAllIn("Replacing: ${main}. And another method: ${foo}.", + (m: util.matching.Regex.Match) => { + val identifier = m.group(1) + identifier + }) + assertEquals("Replacing: main. And another method: foo.", replaced) + + val regex3 = """\$\{(.+?)\}""".r + val replaced3 = regex3.replaceSomeIn("Replacing: ${main}. And another: ${foo}.", (m: util.matching.Regex.Match) => { + val id = m.group(1) + if (id.startsWith("m")) Some(id) else None + }) + assertEquals("Replacing: main. And another: ${foo}.", replaced3) + } + + @Test def groupsMatching(): Unit = { + val Date = """(\d+)/(\d+)/(\d+)""".r + for (Regex.Groups(a, b, c) <- Date findFirstMatchIn "1/1/2001 marks the start of the millennium. 31/12/2000 doesn't.") { + assertEquals("1", a) + assertEquals("1", b) + assertEquals("2001", c) + } + for (Regex.Groups(a, b, c) <- Date.findAllIn("1/1/2001 marks the start of the millennium. 31/12/2000 doesn't.").matchData) { + assert(a == "1" || a == "31") + assert(b == "1" || b == "12") + assert(c == "2001" || c == "2000") + } + } }