js/src/tests/ecma_3/Exceptions/15.11.4.4-1.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_3/Exceptions/15.11.4.4-1.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,140 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + *
    1.11 + * Date:    22 Jan 2002
    1.12 + * SUMMARY: Testing Error.prototype.toString()
    1.13 + *
    1.14 + * Revised: 25 Nov 2002
    1.15 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=181909
    1.16 + *
    1.17 + * Note that ECMA-262 3rd Edition Final, Section 15.11.4.4 states that
    1.18 + * Error.prototype.toString() returns an implementation-dependent string.
    1.19 + * Therefore any testcase on this property is somewhat arbitrary.
    1.20 + *
    1.21 + * However, d-russo@ti.com pointed out that Rhino was returning this:
    1.22 + *
    1.23 + *               js> err = new Error()
    1.24 + *               undefined: undefined
    1.25 + *
    1.26 + *               js> err = new Error("msg")
    1.27 + *               undefined: msg
    1.28 + *
    1.29 + *
    1.30 + * We expect Rhino to return what SpiderMonkey currently does:
    1.31 + *
    1.32 + *               js> err = new Error()
    1.33 + *               Error
    1.34 + *
    1.35 + *               js> err = new Error("msg")
    1.36 + *               Error: msg
    1.37 + *
    1.38 + *
    1.39 + * i.e. we expect err.toString() === err.name if err.message is not defined;
    1.40 + * otherwise, we expect err.toString() === err.name + ': ' + err.message.
    1.41 + *
    1.42 + * See also ECMA 15.11.4.2, 15.11.4.3
    1.43 + */
    1.44 +//-----------------------------------------------------------------------------
    1.45 +var UBound = 0;
    1.46 +var BUGNUMBER = '(none)';
    1.47 +var summary = 'Testing Error.prototype.toString()';
    1.48 +var status = '';
    1.49 +var statusitems = [];
    1.50 +var actual = '';
    1.51 +var actualvalues = [];
    1.52 +var expect= '';
    1.53 +var expectedvalues = [];
    1.54 +var EMPTY_STRING = '';
    1.55 +var EXPECTED_FORMAT = 0;
    1.56 +
    1.57 +
    1.58 +status = inSection(1);
    1.59 +var err1 = new Error('msg1');
    1.60 +actual = examineThis(err1, 'msg1');
    1.61 +expect = EXPECTED_FORMAT;
    1.62 +addThis();
    1.63 +
    1.64 +status = inSection(2);
    1.65 +var err2 = new Error(err1);
    1.66 +actual = examineThis(err2, err1);
    1.67 +expect = EXPECTED_FORMAT;
    1.68 +addThis();
    1.69 +
    1.70 +status = inSection(3);
    1.71 +var err3 = new Error();
    1.72 +actual = examineThis(err3, EMPTY_STRING);
    1.73 +expect = EXPECTED_FORMAT;
    1.74 +addThis();
    1.75 +
    1.76 +status = inSection(4);
    1.77 +var err4 = new Error(EMPTY_STRING);
    1.78 +actual = examineThis(err4, EMPTY_STRING);
    1.79 +expect = EXPECTED_FORMAT;
    1.80 +addThis();
    1.81 +
    1.82 +// now generate a run-time error -
    1.83 +status = inSection(5);
    1.84 +try
    1.85 +{
    1.86 +  eval('1=2');
    1.87 +}
    1.88 +catch(err5)
    1.89 +{
    1.90 +  actual = examineThis(err5, '.*');
    1.91 +}
    1.92 +expect = EXPECTED_FORMAT;
    1.93 +addThis();
    1.94 +
    1.95 +
    1.96 +
    1.97 +//-----------------------------------------------------------------------------
    1.98 +test();
    1.99 +//-----------------------------------------------------------------------------
   1.100 +
   1.101 +
   1.102 +
   1.103 +/*
   1.104 + * Searches err.toString() for err.name + ':' + err.message,
   1.105 + * with possible whitespace on each side of the colon sign.
   1.106 + *
   1.107 + * We allow for no colon in case err.message was not provided by the user.
   1.108 + * In such a case, SpiderMonkey and Rhino currently set err.message = '',
   1.109 + * as allowed for by ECMA 15.11.4.3. This makes |pattern| work in this case.
   1.110 + *
   1.111 + * If this is ever changed to a non-empty string, e.g. 'undefined',
   1.112 + * you may have to modify |pattern| to take that into account -
   1.113 + *
   1.114 + */
   1.115 +function examineThis(err, msg)
   1.116 +{
   1.117 +  var pattern = err.name + '\\s*:?\\s*' + msg;
   1.118 +  return err.toString().search(RegExp(pattern));
   1.119 +}
   1.120 +
   1.121 +
   1.122 +function addThis()
   1.123 +{
   1.124 +  statusitems[UBound] = status;
   1.125 +  actualvalues[UBound] = actual;
   1.126 +  expectedvalues[UBound] = expect;
   1.127 +  UBound++;
   1.128 +}
   1.129 +
   1.130 +
   1.131 +function test()
   1.132 +{
   1.133 +  enterFunc ('test');
   1.134 +  printBugNumber(BUGNUMBER);
   1.135 +  printStatus (summary);
   1.136 +
   1.137 +  for (var i = 0; i < UBound; i++)
   1.138 +  {
   1.139 +    reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
   1.140 +  }
   1.141 +
   1.142 +  exitFunc ('test');
   1.143 +}

mercurial