1// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28/**
29 * @fileoverview Test String.prototype.match
30 */
31
32
33function testMatch(name, input, regexp, result, captures, from, to) {
34  var matchResult = input.match(regexp);
35  assertEquals(result, matchResult, name + "-match");
36
37  var match = input.substring(from, to);
38  var preMatch = input.substring(0, from);
39  var postMatch = input.substring(to);
40  var lastParen = captures.length > 0 ? captures[captures.length - 1] : "";
41
42  if (regexp.global) {
43    // Returns array of matched strings.
44    var lastMatch = matchResult[matchResult.length - 1];
45    assertEquals(match, lastMatch, name + "-match-string_g");
46  } else {
47    // Returns array of match and captures.
48    assertEquals(match, matchResult[0], name + "-match-string");
49    assertEquals(captures.length + 1, matchResult.length, name + "-cap-return");
50    for (var i = 1; i < matchResult.length; i++) {
51      assertEquals(captures[i - 1], matchResult[i], name + "-cap-return-" + i);
52    }
53  }
54
55  assertEquals(match, RegExp["$&"], name + "-$&");
56  assertEquals(match, RegExp.lastMatch, name + "-lastMatch");
57
58  assertEquals(undefined, RegExp.$0, name + "-nocapture-10");
59  for (var i = 1; i <= 9; i++) {
60    if (i <= captures.length) {
61      assertEquals(captures[i - 1], RegExp["$" + i], name + "-capture-" + i);
62    } else {
63      assertEquals("", RegExp["$" + i], name + "-nocapture-" + i);
64    }
65  }
66  assertEquals(undefined, RegExp.$10, name + "-nocapture-10");
67
68  assertEquals(input, RegExp.input, name + "-input");
69  assertEquals(input, RegExp.$_, name + "-$_");
70
71  assertEquals(preMatch, RegExp["$`"], name + "-$`");
72  assertEquals(preMatch, RegExp.leftContext, name + "-leftContex");
73
74  assertEquals(postMatch, RegExp["$'"], name + "-$'");
75  assertEquals(postMatch, RegExp.rightContext, name + "-rightContex");
76
77  assertEquals(lastParen, RegExp["$+"], name + "-$+");
78  assertEquals(lastParen, RegExp.lastParen, name + "-lastParen");
79
80}
81
82
83var stringSample = "A man, a plan, a canal: Panama";
84var stringSample2 = "Argle bargle glop glyf!";
85var stringSample3 = "abcdefghijxxxxxxxxxx";
86
87
88// Non-capturing, non-global regexp.
89
90var re_nog = /\w+/;
91
92testMatch("Nonglobal", stringSample, re_nog,
93          ["A"],
94          [], 0, 1);
95
96
97re_nog.lastIndex = 2;
98
99testMatch("Nonglobal-ignore-lastIndex", stringSample, re_nog,
100          ["A"],
101          [], 0, 1);
102
103
104// Capturing non-global regexp.
105
106var re_multicap = /(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)/;
107testMatch("Capture-Nonglobal", stringSample3, re_multicap,
108          ["abcdefghij", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
109          ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"], 0, 10);
110
111
112// Global regexp (also check that capture from before are cleared)
113
114var re = /\w+/g;
115testMatch("Global", stringSample2, re,
116          ["Argle", "bargle", "glop", "glyf"], [], 18, 22);
117
118re.lastIndex = 10;
119
120testMatch("Global-ignore-lastIndex", stringSample2, re,
121          ["Argle", "bargle", "glop", "glyf"], [], 18, 22);
122
123
124// Capturing global regexp
125
126var re_cap = /\w(\w*)/g;
127
128testMatch("Capture-Global", stringSample, re_cap,
129          ["A", "man", "a", "plan", "a", "canal", "Panama"],
130          ["anama"], 24, 30);
131
132
133// Atom, non-global
134
135var re_atom = /an/;
136
137testMatch("Atom", stringSample, re_atom,
138          ["an"],
139          [], 3, 5);
140
141
142// Atom, global
143
144var re_atomg = /an/g;
145
146testMatch("Global-Atom", stringSample, re_atomg,
147          ["an", "an", "an", "an"],
148          [], 25, 27);
149