| 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 | * |
| 8 | * Date: 22 Jan 2002 |
| 9 | * SUMMARY: Testing Error.prototype.toString() |
| 10 | * |
| 11 | * Revised: 25 Nov 2002 |
| 12 | * See http://bugzilla.mozilla.org/show_bug.cgi?id=181909 |
| 13 | * |
| 14 | * Note that ECMA-262 3rd Edition Final, Section 15.11.4.4 states that |
| 15 | * Error.prototype.toString() returns an implementation-dependent string. |
| 16 | * Therefore any testcase on this property is somewhat arbitrary. |
| 17 | * |
| 18 | * However, [email protected] pointed out that Rhino was returning this: |
| 19 | * |
| 20 | * js> err = new Error() |
| 21 | * undefined: undefined |
| 22 | * |
| 23 | * js> err = new Error("msg") |
| 24 | * undefined: msg |
| 25 | * |
| 26 | * |
| 27 | * We expect Rhino to return what SpiderMonkey currently does: |
| 28 | * |
| 29 | * js> err = new Error() |
| 30 | * Error |
| 31 | * |
| 32 | * js> err = new Error("msg") |
| 33 | * Error: msg |
| 34 | * |
| 35 | * |
| 36 | * i.e. we expect err.toString() === err.name if err.message is not defined; |
| 37 | * otherwise, we expect err.toString() === err.name + ': ' + err.message. |
| 38 | * |
| 39 | * See also ECMA 15.11.4.2, 15.11.4.3 |
| 40 | */ |
| 41 | //----------------------------------------------------------------------------- |
| 42 | var gTestfile = '15.11.4.4-1.js'; |
| 43 | var UBound = 0; |
| 44 | var BUGNUMBER = '(none)'; |
| 45 | var summary = 'Testing Error.prototype.toString()'; |
| 46 | var status = ''; |
| 47 | var statusitems = []; |
| 48 | var actual = ''; |
| 49 | var actualvalues = []; |
| 50 | var expect= ''; |
| 51 | var expectedvalues = []; |
| 52 | var EMPTY_STRING = ''; |
| 53 | var EXPECTED_FORMAT = 0; |
| 54 | |
| 55 | |
| 56 | status = inSection(1); |
| 57 | var err1 = new Error('msg1'); |
| 58 | actual = examineThis(err1, 'msg1'); |
| 59 | expect = EXPECTED_FORMAT; |
| 60 | addThis(); |
| 61 | |
| 62 | status = inSection(2); |
| 63 | var err2 = new Error(err1); |
| 64 | actual = examineThis(err2, err1); |
| 65 | expect = EXPECTED_FORMAT; |
| 66 | addThis(); |
| 67 | |
| 68 | status = inSection(3); |
| 69 | var err3 = new Error(); |
| 70 | actual = examineThis(err3, EMPTY_STRING); |
| 71 | expect = EXPECTED_FORMAT; |
| 72 | addThis(); |
| 73 | |
| 74 | status = inSection(4); |
| 75 | var err4 = new Error(EMPTY_STRING); |
| 76 | actual = examineThis(err4, EMPTY_STRING); |
| 77 | expect = EXPECTED_FORMAT; |
| 78 | addThis(); |
| 79 | |
| 80 | // now generate a run-time error - |
| 81 | status = inSection(5); |
| 82 | try |
| 83 | { |
| 84 | eval('1=2'); |
| 85 | } |
| 86 | catch(err5) |
| 87 | { |
| 88 | actual = examineThis(err5, '.*'); |
| 89 | } |
| 90 | expect = EXPECTED_FORMAT; |
| 91 | addThis(); |
| 92 | |
| 93 | |
| 94 | |
| 95 | //----------------------------------------------------------------------------- |
| 96 | test(); |
| 97 | //----------------------------------------------------------------------------- |
| 98 | |
| 99 | |
| 100 | |
| 101 | /* |
| 102 | * Searches err.toString() for err.name + ':' + err.message, |
| 103 | * with possible whitespace on each side of the colon sign. |
| 104 | * |
| 105 | * We allow for no colon in case err.message was not provided by the user. |
| 106 | * In such a case, SpiderMonkey and Rhino currently set err.message = '', |
| 107 | * as allowed for by ECMA 15.11.4.3. This makes |pattern| work in this case. |
| 108 | * |
| 109 | * If this is ever changed to a non-empty string, e.g. 'undefined', |
| 110 | * you may have to modify |pattern| to take that into account - |
| 111 | * |
| 112 | */ |
| 113 | function examineThis(err, msg) |
| 114 | { |
| 115 | var pattern = err.name + '\\s*:?\\s*' + msg; |
| 116 | return err.toString().search(RegExp(pattern)); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | function addThis() |
| 121 | { |
| 122 | statusitems[UBound] = status; |
| 123 | actualvalues[UBound] = actual; |
| 124 | expectedvalues[UBound] = expect; |
| 125 | UBound++; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | function test() |
| 130 | { |
| 131 | enterFunc ('test'); |
| 132 | printBugNumber(BUGNUMBER); |
| 133 | printStatus (summary); |
| 134 | |
| 135 | for (var i = 0; i < UBound; i++) |
| 136 | { |
| 137 | reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
| 138 | } |
| 139 | |
| 140 | exitFunc ('test'); |
| 141 | } |