-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegexStringContextTest.scala
55 lines (44 loc) · 1.35 KB
/
RegexStringContextTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import scala.util.matching.Regex
import org.scalatest.FunSuite
import RegexStringContext._
class RegexStringContextTest extends FunSuite {
def check(expected: Regex, actual: Regex) {
assert(expected.pattern.pattern === actual.pattern.pattern)
assert(expected.pattern.flags === actual.pattern.flags)
}
test("plain") {
check(new Regex("foo"), r"foo")
}
test("with metacharacter") {
check(new Regex("\\w+"), r"\w+")
}
test("with plain parameter") {
val bar = "bar"
check(new Regex("foo\\Qbar\\E"), r"foo$bar")
}
test("with parameter that has metacharacters") {
val price = "$100.00"
check(new Regex("Price = \\Q$100.00\\E"), r"Price = $price")
}
test("with multiple parameters that have metacharacters") {
val url = "http://example.com/"
val foo = "foo"
check(new Regex("url=\\Qhttp://example.com/\\E,foo=\\Qfoo\\E"), r"url=$url,foo=$foo")
}
test("with parameter that includes \\E") {
val e = "\\E"
check(new Regex("foo\\Q\\E\\\\E\\Q\\Ebar"), r"foo${e}bar")
}
test("support for pattern matching") {
"foobar" match {
case r"foo[bar]{3}" => assert(true)
case _ => assert(false)
}
}
test("support for pattern matching with parameters") {
"foobar" match {
case r"foo$x" => assert(x === "bar")
case _ => assert(false)
}
}
}