OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 121 matching lines...) Loading... |
132 assertEquals("~~", str); | 132 assertEquals("~~", str); |
133 | 133 |
134 // Test zero-length matches that have non-zero-length sub-captures that do not | 134 // Test zero-length matches that have non-zero-length sub-captures that do not |
135 // start at the match start position. | 135 // start at the match start position. |
136 str = "up up up up"; | 136 str = "up up up up"; |
137 str = str.replace(/\b(?=u(p))/g, function(match, capture) { | 137 str = str.replace(/\b(?=u(p))/g, function(match, capture) { |
138 return capture.length; | 138 return capture.length; |
139 }); | 139 }); |
140 | 140 |
141 assertEquals("1up 1up 1up 1up", str); | 141 assertEquals("1up 1up 1up 1up", str); |
| 142 |
| 143 |
| 144 // Create regexp that has a *lot* of captures. |
| 145 var re_string = "(a)"; |
| 146 for (var i = 0; i < 500; i++) { |
| 147 re_string = "(" + re_string + ")"; |
| 148 } |
| 149 re_string = re_string + "1"; |
| 150 // re_string = "(((...((a))...)))1" |
| 151 |
| 152 var regexps = new Array(); |
| 153 var last_match_expectations = new Array(); |
| 154 var first_capture_expectations = new Array(); |
| 155 |
| 156 // Atomic regexp. |
| 157 regexps.push(/a1/g); |
| 158 last_match_expectations.push("a1"); |
| 159 first_capture_expectations.push(""); |
| 160 // Small regexp (no capture); |
| 161 regexps.push(/\w1/g); |
| 162 last_match_expectations.push("a1"); |
| 163 first_capture_expectations.push(""); |
| 164 // Small regexp (one capture). |
| 165 regexps.push(/(a)1/g); |
| 166 last_match_expectations.push("a1"); |
| 167 first_capture_expectations.push("a"); |
| 168 // Large regexp (a lot of captures). |
| 169 regexps.push(new RegExp(re_string, "g")); |
| 170 last_match_expectations.push("a1"); |
| 171 first_capture_expectations.push("a"); |
| 172 |
| 173 function test_replace(result_expectation, |
| 174 subject, |
| 175 regexp, |
| 176 replacement) { |
| 177 for (var i = 0; i < regexps.length; i++) { |
| 178 // Overwrite last match info. |
| 179 "deadbeef".replace(/(dead)beef/, "$1holeycow"); |
| 180 // Conduct tests. |
| 181 assertEquals(result_expectation, subject.replace(regexps[i], replacement)); |
| 182 if (subject.length == 0) { |
| 183 assertEquals("deadbeef", RegExp.lastMatch); |
| 184 assertEquals("dead", RegExp["$1"]); |
| 185 } else { |
| 186 assertEquals(last_match_expectations[i], RegExp.lastMatch); |
| 187 assertEquals(first_capture_expectations[i], RegExp["$1"]); |
| 188 } |
| 189 } |
| 190 } |
| 191 |
| 192 |
| 193 function test_match(result_expectation, |
| 194 subject, |
| 195 regexp) { |
| 196 for (var i = 0; i < regexps.length; i++) { |
| 197 // Overwrite last match info. |
| 198 "deadbeef".replace(/(dead)beef/, "$1holeycow"); |
| 199 // Conduct tests. |
| 200 if (result_expectation == null) { |
| 201 assertNull(subject.match(regexps[i])); |
| 202 } else { |
| 203 assertArrayEquals(result_expectation, subject.match(regexps[i])); |
| 204 } |
| 205 if (subject.length == 0) { |
| 206 assertEquals("deadbeef", RegExp.lastMatch); |
| 207 assertEquals("dead", RegExp["$1"]); |
| 208 } else { |
| 209 assertEquals(last_match_expectations[i], RegExp.lastMatch); |
| 210 assertEquals(first_capture_expectations[i], RegExp["$1"]); |
| 211 } |
| 212 } |
| 213 } |
| 214 |
| 215 |
| 216 // Test for different number of matches. |
| 217 for (var m = 0; m < 200; m++) { |
| 218 // Create string that matches m times. |
| 219 var subject = ""; |
| 220 var test_1_expectation = ""; |
| 221 var test_2_expectation = ""; |
| 222 var test_3_expectation = (m == 0) ? null : new Array(); |
| 223 for (var i = 0; i < m; i++) { |
| 224 subject += "a11"; |
| 225 test_1_expectation += "x1"; |
| 226 test_2_expectation += "1"; |
| 227 test_3_expectation.push("a1"); |
| 228 } |
| 229 |
| 230 // Test 1a: String.replace with string. |
| 231 test_replace(test_1_expectation, subject, /a1/g, "x"); |
| 232 |
| 233 // Test 1b: String.replace with function. |
| 234 function f() { return "x"; } |
| 235 test_replace(test_1_expectation, subject, /a1/g, f); |
| 236 |
| 237 // Test 2a: String.replace with empty string. |
| 238 test_replace(test_2_expectation, subject, /a1/g, ""); |
| 239 |
| 240 // Test 3a: String.match. |
| 241 test_match(test_3_expectation, subject, /a1/g); |
| 242 } |
OLD | NEW |