| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | |
| 6 | /* |
| 7 | * Date: 22 October 2001 |
| 8 | * |
| 9 | * SUMMARY: Regression test for Bugzilla bug 105972: |
| 10 | * "/^.*?$/ will not match anything" |
| 11 | * |
| 12 | * See http://bugzilla.mozilla.org/show_bug.cgi?id=105972 |
| 13 | */ |
| 14 | //----------------------------------------------------------------------------- |
| 15 | var gTestfile = 'regress-105972.js'; |
| 16 | var i = 0; |
| 17 | var BUGNUMBER = 105972; |
| 18 | var summary = 'Regression test for Bugzilla bug 105972'; |
| 19 | var cnEmptyString = ''; |
| 20 | var status = ''; |
| 21 | var statusmessages = new Array(); |
| 22 | var pattern = ''; |
| 23 | var patterns = new Array(); |
| 24 | var string = ''; |
| 25 | var strings = new Array(); |
| 26 | var actualmatch = ''; |
| 27 | var actualmatches = new Array(); |
| 28 | var expectedmatch = ''; |
| 29 | var expectedmatches = new Array(); |
| 30 | |
| 31 | |
| 32 | /* |
| 33 | * The bug: this match was coming up null in Rhino and SpiderMonkey. |
| 34 | * It should match the whole string. The reason: |
| 35 | * |
| 36 | * The * operator is greedy, but *? is non-greedy: it will stop |
| 37 | * at the simplest match it can find. But the pattern here asks us |
| 38 | * to match till the end of the string. So the simplest match must |
| 39 | * go all the way out to the end, and *? has no choice but to do it. |
| 40 | */ |
| 41 | status = inSection(1); |
| 42 | pattern = /^.*?$/; |
| 43 | string = 'Hello World'; |
| 44 | actualmatch = string.match(pattern); |
| 45 | expectedmatch = Array(string); |
| 46 | addThis(); |
| 47 | |
| 48 | |
| 49 | /* |
| 50 | * Leave off the '$' condition - here we expect the empty string. |
| 51 | * Unlike the above pattern, we don't have to match till the end of |
| 52 | * the string, so the non-greedy operator *? doesn't try to... |
| 53 | */ |
| 54 | status = inSection(2); |
| 55 | pattern = /^.*?/; |
| 56 | string = 'Hello World'; |
| 57 | actualmatch = string.match(pattern); |
| 58 | expectedmatch = Array(cnEmptyString); |
| 59 | addThis(); |
| 60 | |
| 61 | |
| 62 | /* |
| 63 | * Try '$' combined with an 'or' operator. |
| 64 | * |
| 65 | * The operator *? will consume the string from left to right, |
| 66 | * attempting to satisfy the condition (:|$). When it hits ':', |
| 67 | * the match will stop because the operator *? is non-greedy. |
| 68 | * |
| 69 | * The submatch $1 = (:|$) will contain the ':' |
| 70 | */ |
| 71 | status = inSection(3); |
| 72 | pattern = /^.*?(:|$)/; |
| 73 | string = 'Hello: World'; |
| 74 | actualmatch = string.match(pattern); |
| 75 | expectedmatch = Array('Hello:', ':'); |
| 76 | addThis(); |
| 77 | |
| 78 | |
| 79 | /* |
| 80 | * Again, '$' combined with an 'or' operator. |
| 81 | * |
| 82 | * The operator * will consume the string from left to right, |
| 83 | * attempting to satisfy the condition (:|$). When it hits ':', |
| 84 | * the match will not stop since * is greedy. The match will |
| 85 | * continue until it hits $, the end-of-string boundary. |
| 86 | * |
| 87 | * The submatch $1 = (:|$) will contain the empty string |
| 88 | * conceived to exist at the end-of-string boundary. |
| 89 | */ |
| 90 | status = inSection(4); |
| 91 | pattern = /^.*(:|$)/; |
| 92 | string = 'Hello: World'; |
| 93 | actualmatch = string.match(pattern); |
| 94 | expectedmatch = Array(string, cnEmptyString); |
| 95 | addThis(); |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | //----------------------------------------------------------------------------- |
| 101 | test(); |
| 102 | //----------------------------------------------------------------------------- |
| 103 | |
| 104 | |
| 105 | |
| 106 | function addThis() |
| 107 | { |
| 108 | statusmessages[i] = status; |
| 109 | patterns[i] = pattern; |
| 110 | strings[i] = string; |
| 111 | actualmatches[i] = actualmatch; |
| 112 | expectedmatches[i] = expectedmatch; |
| 113 | i++; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | function test() |
| 118 | { |
| 119 | enterFunc ('test'); |
| 120 | printBugNumber(BUGNUMBER); |
| 121 | printStatus (summary); |
| 122 | testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches); |
| 123 | exitFunc ('test'); |
| 124 | } |