| 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: 26 November 2000 |
| 8 | * |
| 9 | * |
| 10 | *SUMMARY: Passing a RegExp object to a RegExp() constructor. |
| 11 | *This test arose from Bugzilla bug 61266. The ECMA3 section is: |
| 12 | * |
| 13 | * 15.10.4.1 new RegExp(pattern, flags) |
| 14 | * |
| 15 | * If pattern is an object R whose [[Class]] property is "RegExp" and |
| 16 | * flags is undefined, then let P be the pattern used to construct R |
| 17 | * and let F be the flags used to construct R. If pattern is an object R |
| 18 | * whose [[Class]] property is "RegExp" and flags is not undefined, |
| 19 | * then throw a TypeError exception. Otherwise, let P be the empty string |
| 20 | * if pattern is undefined and ToString(pattern) otherwise, and let F be |
| 21 | * the empty string if flags is undefined and ToString(flags) otherwise. |
| 22 | * |
| 23 | * |
| 24 | *The current test will check the first scenario outlined above: |
| 25 | * |
| 26 | * "pattern" is itself a RegExp object R |
| 27 | * "flags" is undefined |
| 28 | * |
| 29 | * We check that a new RegExp object obj2 defined from these parameters |
| 30 | * is morally the same as the original RegExp object obj1. Of course, they |
| 31 | * can't be equal as objects - so we check their enumerable properties... |
| 32 | * |
| 33 | * In this test, the initial RegExp object obj1 will not include a |
| 34 | * flag. This test is identical to test 15.10.4.1-1.js, except that |
| 35 | * here we use this syntax: |
| 36 | * |
| 37 | * obj2 = new RegExp(obj1, undefined); |
| 38 | * |
| 39 | * instead of: |
| 40 | * |
| 41 | * obj2 = new RegExp(obj1); |
| 42 | */ |
| 43 | //----------------------------------------------------------------------------- |
| 44 | var gTestfile = '15.10.4.1-2.js'; |
| 45 | var BUGNUMBER = '61266'; |
| 46 | var summary = 'Passing a RegExp object to a RegExp() constructor'; |
| 47 | var statprefix = 'Applying RegExp() twice to pattern '; |
| 48 | var statsuffix = '; testing property '; |
| 49 | var singlequote = "'"; |
| 50 | var i = -1; var s = ''; |
| 51 | var obj1 = {}; var obj2 = {}; |
| 52 | var status = ''; var actual = ''; var expect = ''; var msg = ''; |
| 53 | var patterns = new Array(); |
| 54 | |
| 55 | |
| 56 | // various regular expressions to try - |
| 57 | patterns[0] = ''; |
| 58 | patterns[1] = 'abc'; |
| 59 | patterns[2] = '(.*)(3-1)\s\w'; |
| 60 | patterns[3] = '(.*)(...)\\s\\w'; |
| 61 | patterns[4] = '[^A-Za-z0-9_]'; |
| 62 | patterns[5] = '[^\f\n\r\t\v](123.5)([4 - 8]$)'; |
| 63 | |
| 64 | |
| 65 | |
| 66 | //------------------------------------------------------------------------------------------------- |
| 67 | test(); |
| 68 | //------------------------------------------------------------------------------------------------- |
| 69 | |
| 70 | |
| 71 | function test() |
| 72 | { |
| 73 | enterFunc ('test'); |
| 74 | printBugNumber(BUGNUMBER); |
| 75 | printStatus (summary); |
| 76 | |
| 77 | for (i in patterns) |
| 78 | { |
| 79 | s = patterns[i]; |
| 80 | status =getStatus(s); |
| 81 | obj1 = new RegExp(s); |
| 82 | obj2 = new RegExp(obj1, undefined); // see introduction to bug |
| 83 | |
| 84 | reportCompare (obj1 + '', obj2 + '', status); |
| 85 | } |
| 86 | |
| 87 | exitFunc ('test'); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | function getStatus(regexp) |
| 92 | { |
| 93 | return (statprefix + quote(regexp) + statsuffix); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | function quote(text) |
| 98 | { |
| 99 | return (singlequote + text + singlequote); |
| 100 | } |