summaryrefslogtreecommitdiff
path: root/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/RegexTest.scala
blob: a27584aa2bd15c7cc483b63cda3ab28eab04d76e (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js Test Suite        **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */
package scala.scalajs.testsuite.javalib

import scala.scalajs.js
import scala.scalajs.js.JSConverters._

import org.scalajs.jasminetest.JasmineTest

import java.util.regex.Pattern

object RegexTest extends JasmineTest {

  describe("java.util.regex.Pattern") {

    it("should respond to `matches`") {
      expect(Pattern.matches("[Scal]*\\.js", "Scala.js")).toBeTruthy
      expect(Pattern.matches(".[cal]*\\.j.", "Scala.js")).toBeTruthy
      expect(Pattern.matches(".*\\.js", "Scala.js")).toBeTruthy
      expect(Pattern.matches("S[a-z]*", "Scala.js")).toBeFalsy
    }

    it("should respond to `matches` with flags") {
      matches("scala.js", "Scala.js")
      matches("SCALA.JS", "Scala.js")
      matches("waz*up", "WAZZZZZZZZZZZUP")

      def matches(regex: String, input: String): Unit = {
        val result = Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(input)
        expect(result.matches()).toBeTruthy
      }
    }

    it("should respond to `split`") {
      val result = Pattern.compile("[aj]").split("Scala.js")
      val expected = js.Array("Sc", "l", ".", "s")
      expect(result.length).toEqual(4)
      expect(result.toJSArray).toEqual(expected)

      // Tests from JavaDoc
      split("boo:and:foo", ":", Array("boo", "and", "foo"))
      split("boo:and:foo", "o", Array("b", "", ":and:f"))

      // Splitting the empty string must return 1 element - #987
      split("", "a", Array(""))
      split("", "\\*", Array(""))
      split("", "\n", Array(""))
      split("", "", Array(""))

      // Should remove leading empty match under some conditions - #1171
      // These tests are "measured" on the JVM since the spec is unclear
      split("abc", "(?=a)", Array("abc"))
      split("abc", "(?=b)", Array("a", "bc"))
      split("abc", "(?=a)|b", Array("", "a", "c"))
      split("abc", "", Array("", "a", "b", "c"))
      split("abc", "(?=a)|(?=b)", Array("", "a", "bc"))
      split("abc", "(?=a)|(?=a)", Array("abc"))
      split("abc", "(?=a|b)", Array("", "a", "bc"))
      split("abc", "(?=a|d)", Array("abc"))
      split("abc", "^d*", Array("abc"))
      split("abc", "d*", Array("", "a", "b", "c"))
      split("a", "", Array("", "a"))
      split("a", "^d*", Array("a"))
      split("a", "d*", Array("", "a"))
      split("a", "(?=a)", Array("a"))
      split("ab", "a", Array("", "b"))

      def split(input: String, regex: String, expected: Array[String]): Unit = {
        val result = Pattern.compile(regex).split(input)
        expect(result.toJSArray).toEqual(expected.toJSArray)
      }
    }

    it("should respond to `split` with limit") {
      // Tests from JavaDoc
      splitWithLimit("boo:and:foo", ":", 2, Array("boo", "and:foo"))
      splitWithLimit("boo:and:foo", ":", 5, Array("boo", "and", "foo"))
      splitWithLimit("boo:and:foo", ":", -2, Array("boo", "and", "foo"))
      splitWithLimit("boo:and:foo", "o", 5, Array("b", "", ":and:f", "", ""))
      splitWithLimit("boo:and:foo", "o", -2, Array("b", "", ":and:f", "", ""))
      splitWithLimit("boo:and:foo", "o", 0, Array("b", "", ":and:f"))

      // Splitting the empty string must return 1 element - #987
      splitWithLimit("", "a", 0, Array(""))
      splitWithLimit("", "\\*", 5, Array(""))
      splitWithLimit("", "\n", -2, Array(""))
      splitWithLimit("", "", 1, Array(""))

      // Should remove leading empty match under some conditions - #1171
      splitWithLimit("abc", "", 2, Array("", "abc"))
      splitWithLimit("abc", "(?=a)", 2, Array("abc"))
      splitWithLimit("ab", "a", 1, Array("ab"))

      def splitWithLimit(input: String, regex: String, limit: Int, expected: Array[String]): Unit = {
        val result = Pattern.compile(regex).split(input, limit)
        expect(result.toJSArray).toEqual(expected.toJSArray)
      }
    }

    it("should respond to `flags`") {
      val pattern0 = Pattern.compile("a")
      val pattern1 = Pattern.compile("a", 0)
      val flags2 = Pattern.CASE_INSENSITIVE | Pattern.DOTALL
      val pattern2 = Pattern.compile("a", flags2)

      expect(pattern0.flags).toEqual(0)
      expect(pattern1.flags).toEqual(0)
      expect(pattern2.flags).toEqual(flags2)
    }

    it("should respond to `pattern` and `toString`") {
      def checkPatternAndToString(regex: String): Unit = {
        val pattern0 = Pattern.compile(regex)
        expect(pattern0.pattern).toEqual(regex)
        expect(pattern0.toString).toEqual(regex)

        val pattern1 = Pattern.compile(regex, Pattern.CASE_INSENSITIVE)
        expect(pattern1.pattern).toEqual(regex)
        expect(pattern1.toString).toEqual(regex)
      }

      checkPatternAndToString("a*b+c")
      checkPatternAndToString("\\S[(a1]a.js")
    }

    it("should respond to `quote`") {
      val splitWithQuote = Pattern.compile(Pattern.quote("$1&$2")).split("Scala$1&$2.js")
      val splitNoQuote = Pattern.compile("$1&$2").split("Scala$1&$2.js")
      expect(splitWithQuote.mkString).toEqual("Scala.js")
      expect(splitNoQuote.mkString).toEqual("Scala$1&$2.js")
    }

  }

  describe("java.util.regex.Matcher") {

    it("should respond to `find`") {
      val matcher = Pattern.compile("a").matcher("Scala.js")

      expect(matcher.find()).toBeTruthy
      expect(matcher.find()).toBeTruthy
      expect(matcher.find()).toBeFalsy
      expect(matcher.find(4)).toBeTruthy
      expect(matcher.find()).toBeFalsy
    }

    it("should respond to `start`, `end`, `group`, and `toMatchResult`") {
      val matcher = Pattern.compile("\\s(([A-Za-z]{5}(hum)?).js)\\s").matcher("Write Scala.js everyday!")

      def checkGroup0(start: Int, end: Int, group: String) =
        checkGroup(start, 5, end, 15, group, " Scala.js ")

      def checkGroup1(start: Int, end: Int, group: String) =
        checkGroup(start, 6, end, 14, group, "Scala.js")

      def checkGroup2(start: Int, end: Int, group: String) =
        checkGroup(start, 6, end, 11, group, "Scala")

      def checkGroup3(start: Int, end: Int, group: String) =
        checkGroup(start, -1, end, -1, group, null)

      def checkGroup(start: Int, startExpected: Int, end: Int, endExpected: Int,
                     group: String, groupExpected: String): Unit = {
        expect(start).toEqual(startExpected)
        expect(end).toEqual(endExpected)
        expect(group).toEqual(groupExpected)
      }

      expect(matcher.find()).toBeTruthy
      expect(matcher.groupCount).toEqual(3)
      checkGroup0(matcher.start, matcher.end, matcher.group)
      checkGroup0(matcher.start(0), matcher.end(0), matcher.group(0))
      checkGroup1(matcher.start(1), matcher.end(1), matcher.group(1))
      checkGroup2(matcher.start(2), matcher.end(2), matcher.group(2))
      checkGroup3(matcher.start(3), matcher.end(3), matcher.group(3))

      val matchResult = matcher.toMatchResult
      expect(matchResult.groupCount).toEqual(3)
      checkGroup0(matchResult.start, matchResult.end, matchResult.group)
      checkGroup0(matchResult.start(0), matchResult.end(0), matchResult.group(0))
      checkGroup1(matchResult.start(1), matchResult.end(1), matchResult.group(1))
      checkGroup2(matchResult.start(2), matchResult.end(2), matchResult.group(2))
      checkGroup3(matchResult.start(3), matchResult.end(3), matchResult.group(3))
    }

    it("should respond to `matches`") {
      val matcher0 = Pattern.compile("S[a-z]+").matcher("Scala")
      val matcher1 = Pattern.compile("S[a-z]+").matcher("Scala.js")

      expect(matcher0.matches()).toBeTruthy
      expect(matcher1.matches()).toBeFalsy
    }

    it("should respond to `reset`") {
      val matcher = Pattern.compile("S[a-z]+").matcher("Scalable")

      expect(matcher.find()).toBeTruthy
      expect(matcher.find()).toBeFalsy
      matcher.reset()
      expect(matcher.find()).toBeTruthy
    }

    it("should respond to `reset(String)`") {
      val matcher = Pattern.compile("S[a-z]+").matcher("Scalable")

      expect(matcher.matches()).toBeTruthy
      matcher.reset("Scala.js")
      expect(matcher.matches()).toBeFalsy
    }

    it("should respond to `usePattern`") {
      val patternNoDots = Pattern.compile("S[a-z]+")
      val patternWithDots = Pattern.compile("S[a-z.]+")

      val matcher0 = patternNoDots.matcher("Scala.js")
      expect(matcher0.matches()).toBeFalsy
      matcher0.usePattern(patternWithDots)
      expect(matcher0.matches()).toBeTruthy

      val matcher1 = patternWithDots.matcher("Scala.js")
      expect(matcher1.matches()).toBeTruthy
      matcher1.usePattern(patternNoDots)
      expect(matcher1.matches()).toBeFalsy
    }

    it("should respond to `lookingAt`") {
      val matcher0 = Pattern.compile("S[a-z]+").matcher("Scala")
      val matcher1 = Pattern.compile("S[a-z]+").matcher("Scala.js")
      val matcher2 = Pattern.compile("[a-z]+").matcher("Scala.js")

      expect(matcher0.lookingAt()).toBeTruthy
      expect(matcher1.lookingAt()).toBeTruthy
      expect(matcher2.lookingAt()).toBeFalsy

      val matcher3 = Pattern.compile("S[a-z]+").matcher("Scala.js")
      expect(matcher3.find()).toBeTruthy
      expect(matcher3.lookingAt()).toBeTruthy
    }

    it("should respond to `hitEnd`") {
      val matcher0 = Pattern.compile("S[a-z]*").matcher("Scala.js")
      expect(matcher0.find()).toBeTruthy
      expect(matcher0.hitEnd).toBeFalsy
      expect(matcher0.find()).toBeFalsy
      expect(matcher0.hitEnd).toBeTruthy

      val matcher1 = Pattern.compile("[A-Za-z]+").matcher("Scala.js")
      expect(matcher1.find()).toBeTruthy
      expect(matcher1.hitEnd).toBeFalsy
      expect(matcher1.group).toBe("Scala")
      expect(matcher1.find()).toBeTruthy
      expect(matcher1.hitEnd).toBeTruthy
      expect(matcher1.group).toBe("js")
      expect(matcher1.lookingAt()).toBeTruthy
      expect(matcher1.group).toBe("Scala")
      expect(matcher1.hitEnd).toBeFalsy
    }

    it("should respond to `region`") {
      val matcher0 = Pattern.compile("S[a-z]+").matcher("A Scalable Solution")

      val region0to3 = matcher0.region(0, 3)
      expect(region0to3.regionStart).toBe(0)
      expect(region0to3.regionEnd).toBe(3)
      expect(region0to3.find()).toBeFalsy

      val region0to15 = matcher0.region(0, 15)
      expect(region0to15.regionStart).toBe(0)
      expect(region0to15.regionEnd).toBe(15)
      expect(region0to15.find()).toBeTruthy
      expect(region0to15.group).toEqual("Scalable")

      val region2to7 = region0to15.region(2, 7)
      expect(region2to7.regionStart).toBe(2)
      expect(region2to7.regionEnd).toBe(7)
      expect(region2to7.find()).toBeTruthy
      expect(region2to7.group).toEqual("Scala")

      val region5toEnd = matcher0.region(5, matcher0.regionEnd)
      expect(region5toEnd.regionStart).toBe(5)
      expect(region5toEnd.regionEnd).toBe(19)
      expect(region5toEnd.find()).toBeTruthy
      expect(region5toEnd.group).toEqual("Solution")

      val matcher1 = Pattern.compile("0[xX][A-Fa-f0-9]{3}$").matcher("In CSS, 0xc4fe is not a color")

      val region5to13 = matcher1.region(5, 13)
      expect(region5to13.regionStart).toBe(5)
      expect(region5to13.regionEnd).toBe(13)
      expect(region5to13.find()).toBeTruthy
      expect(region5to13.group).toEqual("0xc4f")

      val region5to20 = matcher1.region(5, 20)
      expect(region5to20.regionStart).toBe(5)
      expect(region5to20.regionEnd).toBe(20)
      expect(region5to20.find()).toBeFalsy
    }

    it("should respond to `appendReplacement` and `appendTail`") {
      // From the JavaDoc
      val matcher = Pattern.compile("cat").matcher("one cat two cats in the yard")
      val sb = new StringBuffer

      while (matcher.find()) {
        matcher.appendReplacement(sb, "dog")
      }
      matcher.appendTail(sb)

      expect(sb.toString).toBe("one dog two dogs in the yard")
    }

    it("should respond to `replaceAll`") {
      // From the JavaDoc
      val matcher = Pattern.compile("a*b").matcher("aabfooaabfooabfoob")
      expect(matcher.replaceAll("-")).toBe("-foo-foo-foo-")
    }

    it("should respond to `replaceFirst`") {
      // From the JavaDoc
      val matcher = Pattern.compile("dog").matcher("zzzdogzzzdogzzz")
      expect(matcher.replaceFirst("cat")).toBe("zzzcatzzzdogzzz")
    }

    it("should throw exception if match accessors are called before `find`") {
      def checkInvalidAccess(block: => Unit): Unit = {
        val exception: Throwable = try {
          block
          throw new Error("No exception thrown")
        } catch {
          case e: Throwable => e
        }

        expect(exception.getClass.getName).toBe("java.lang.IllegalStateException")
        expect(exception.getMessage).toBe("No match available")
      }

      val matcher = Pattern.compile("(Sc([a-z]*))").matcher("Scala.js")

      checkInvalidAccess { matcher.start }
      checkInvalidAccess { matcher.end }
      checkInvalidAccess { matcher.group }
      checkInvalidAccess { matcher.group(42) }

      val matchResult = matcher.toMatchResult

      checkInvalidAccess { matchResult.start }
      checkInvalidAccess { matchResult.end }
      checkInvalidAccess { matchResult.group }
      checkInvalidAccess { matchResult.group(42) }
    }

    it("should correctly handle zero-length matches") {
      val pat = Pattern.compile("a*?")
      val mat = pat.matcher("aaaaa")
      for (i <- 0 to 5) {
        expect(mat.find()).toBeTruthy
        expect(mat.start).toEqual(i)
        expect(mat.end).toEqual(i)
      }

      // Make sure we don't suddenly re-match
      for (i <- 0 to 5) {
        expect(mat.find()).toBeFalsy
      }
    }

    it("should support in-pattern flags - #997") {
      val p0 = Pattern.compile("(?i)abc")

      expect(p0.flags() & Pattern.CASE_INSENSITIVE).not.toBe(0)

      val m0 = p0.matcher("abcABC")

      expect(m0.find()).toBeTruthy
      expect(m0.group()).toEqual("abc")
      expect(m0.find()).toBeTruthy
      expect(m0.group()).toEqual("ABC")
      expect(m0.find()).toBeFalsy

      val p1 = Pattern.compile("(?-i)abc", Pattern.CASE_INSENSITIVE)

      expect(p1.flags() & Pattern.CASE_INSENSITIVE).toBe(0)

      val m1 = p1.matcher("abcABC")

      expect(m1.find()).toBeTruthy
      expect(m1.group()).toEqual("abc")
      expect(m1.find()).toBeFalsy
    }

  }
}