diff options
author | Som Snytt <som.snytt@gmail.com> | 2013-06-19 06:23:02 -0700 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2013-08-10 13:55:04 -0700 |
commit | 93e9623f5babee93c50eb1ce82e443d6771b693d (patch) | |
tree | cf754eb5ee0034008789694cbbc5bfe024601047 /test/junit | |
parent | e7876d5c41bbea0ee4679815edb5dc1ecef4c6a9 (diff) | |
download | scala-93e9623f5babee93c50eb1ce82e443d6771b693d.tar.gz scala-93e9623f5babee93c50eb1ce82e443d6771b693d.tar.bz2 scala-93e9623f5babee93c50eb1ce82e443d6771b693d.zip |
SI-7737 Regex matches Char
Enables Char extraction by regex.
```
val r = """(\p{Lower})""".r
"cat"(0) match { case r(x) => true }
val nc = """\p{Lower}""".r
"cat"(0) match { case nc() => true }
```
Diffstat (limited to 'test/junit')
-rw-r--r-- | test/junit/scala/util/matching/regextract-char.scala | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/test/junit/scala/util/matching/regextract-char.scala b/test/junit/scala/util/matching/regextract-char.scala new file mode 100644 index 0000000000..50fdcd9d46 --- /dev/null +++ b/test/junit/scala/util/matching/regextract-char.scala @@ -0,0 +1,58 @@ + +package scala.util.matching + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +import PartialFunction._ + +/** Regex can match a Char. + * If the pattern includes a group, + * always return a single char. + */ +@RunWith(classOf[JUnit4]) +class CharRegexTest { + implicit class Averrable(val b: Boolean) /*extends AnyVal*/ { + def yes = assert(b) + def no = assert(!b) + } + val c: Char = 'c' // "cat"(0) + val d: Char = 'D' // "Dog"(0) + + @Test def comparesGroupCorrectly(): Unit = { + val r = """(\p{Lower})""".r + cond(c) { case r(x) => true } .yes + cond(c) { case r(_) => true } .yes + cond(c) { case r(_*) => true } .yes + cond(c) { case r() => true } .no + + cond(d) { case r(x) => true } .no + cond(d) { case r(_) => true } .no + cond(d) { case r(_*) => true } .no + cond(d) { case r() => true } .no + } + + @Test def comparesNoGroupCorrectly(): Unit = { + val rnc = """\p{Lower}""".r + cond(c) { case rnc(x) => true } .no + cond(c) { case rnc(_) => true } .no + cond(c) { case rnc(_*) => true } .yes + cond(c) { case rnc() => true } .yes + + cond(d) { case rnc(x) => true } .no + cond(d) { case rnc(_) => true } .no + cond(d) { case rnc(_*) => true } .no + cond(d) { case rnc() => true } .no + } + + @Test(expected = classOf[MatchError]) + def failCorrectly(): Unit = { + val headAndTail = """(\p{Lower})([a-z]+)""".r + val n = "cat"(0) match { + case headAndTail(ht @ _*) => ht.size + } + assert(false, s"Match size $n") + } +} |