js/src/tests/js1_5/Regress/regress-224956.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/js1_5/Regress/regress-224956.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,251 @@
     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:    08 November 2003
    1.12 + * SUMMARY: |expr()| should cause a TypeError if |typeof expr| != 'function'
    1.13 + *
    1.14 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=224956
    1.15 + *
    1.16 + */
    1.17 +//-----------------------------------------------------------------------------
    1.18 +var UBound = 0;
    1.19 +var BUGNUMBER = 224956;
    1.20 +var summary = "|expr()| should cause TypeError if |typeof expr| != 'function'";
    1.21 +var TEST_PASSED = 'TypeError';
    1.22 +var TEST_FAILED = 'Generated an error, but NOT a TypeError! ';
    1.23 +var TEST_FAILED_BADLY = 'Did not generate ANY error!!!';
    1.24 +var CHECK_PASSED = 'Should not generate an error';
    1.25 +var CHECK_FAILED = 'Generated an error!';
    1.26 +var status = '';
    1.27 +var statusitems = [];
    1.28 +var actual = '';
    1.29 +var actualvalues = [];
    1.30 +var expect= '';
    1.31 +var expectedvalues = [];
    1.32 +var x;
    1.33 +
    1.34 +
    1.35 +/*
    1.36 + * All the following contain invalid uses of the call operator ()
    1.37 + * and should generate TypeErrors. That's what we're testing for.
    1.38 + *
    1.39 + * To save writing try...catch over and over, we hide all the
    1.40 + * errors inside eval strings, and let testThis() catch them.
    1.41 + */
    1.42 +status = inSection(1);
    1.43 +x = 'abc';
    1.44 +testThis('x()');
    1.45 +
    1.46 +status = inSection(2);
    1.47 +testThis('"abc"()');
    1.48 +
    1.49 +status = inSection(3);
    1.50 +x = new Date();
    1.51 +testThis('x()');
    1.52 +
    1.53 +status = inSection(4);
    1.54 +testThis('Date(12345)()');
    1.55 +
    1.56 +status = inSection(5);
    1.57 +x = Number(1);
    1.58 +testThis('x()');
    1.59 +
    1.60 +status = inSection(6);
    1.61 +testThis('1()');
    1.62 +
    1.63 +status = inSection(7);
    1.64 +x = void(0);
    1.65 +testThis('x()');
    1.66 +
    1.67 +status = inSection(8);
    1.68 +testThis('void(0)()');
    1.69 +
    1.70 +status = inSection(9);
    1.71 +x = Math;
    1.72 +testThis('x()');
    1.73 +
    1.74 +status = inSection(10);
    1.75 +testThis('Math()');
    1.76 +
    1.77 +status = inSection(11);
    1.78 +x = Array(5);
    1.79 +testThis('x()');
    1.80 +
    1.81 +status = inSection(12);
    1.82 +testThis('[1,2,3,4,5]()');
    1.83 +
    1.84 +status = inSection(13);
    1.85 +x = [1,2,3].splice(1,2);
    1.86 +testThis('x()');
    1.87 +
    1.88 +
    1.89 +/*
    1.90 + * Same as above, but with non-empty call parentheses
    1.91 + */
    1.92 +status = inSection(14);
    1.93 +x = 'abc';
    1.94 +testThis('x(1)');
    1.95 +
    1.96 +status = inSection(15);
    1.97 +testThis('"abc"(1)');
    1.98 +
    1.99 +status = inSection(16);
   1.100 +x = new Date();
   1.101 +testThis('x(1)');
   1.102 +
   1.103 +status = inSection(17);
   1.104 +testThis('Date(12345)(1)');
   1.105 +
   1.106 +status = inSection(18);
   1.107 +x = Number(1);
   1.108 +testThis('x(1)');
   1.109 +
   1.110 +status = inSection(19);
   1.111 +testThis('1(1)');
   1.112 +
   1.113 +status = inSection(20);
   1.114 +x = void(0);
   1.115 +testThis('x(1)');
   1.116 +
   1.117 +status = inSection(21);
   1.118 +testThis('void(0)(1)');
   1.119 +
   1.120 +status = inSection(22);
   1.121 +x = Math;
   1.122 +testThis('x(1)');
   1.123 +
   1.124 +status = inSection(23);
   1.125 +testThis('Math(1)');
   1.126 +
   1.127 +status = inSection(24);
   1.128 +x = Array(5);
   1.129 +testThis('x(1)');
   1.130 +
   1.131 +status = inSection(25);
   1.132 +testThis('[1,2,3,4,5](1)');
   1.133 +
   1.134 +status = inSection(26);
   1.135 +x = [1,2,3].splice(1,2);
   1.136 +testThis('x(1)');
   1.137 +
   1.138 +
   1.139 +/*
   1.140 + * Expression from website in original bug report above -
   1.141 + */
   1.142 +status = inSection(27);
   1.143 +var A = 1, C=2, Y=3, T=4, I=5;
   1.144 +testThis('(((C/A-0.3)/0.2)+((Y/A-3)/4)+((T/A)/0.05)+((0.095-I/A)/0.4))(100/6)');
   1.145 +
   1.146 +
   1.147 +status = inSection(28);
   1.148 +x = /a()/;
   1.149 +testThis('x("abc")');
   1.150 +
   1.151 +status = inSection(29);
   1.152 +x = /a()/gi;
   1.153 +testThis('x("abc")');
   1.154 +
   1.155 +status = inSection(30);
   1.156 +x = RegExp('a()');
   1.157 +testThis('x("abc")');
   1.158 +
   1.159 +status = inSection(31);
   1.160 +x = new RegExp('a()', 'gi');
   1.161 +testThis('x("")');
   1.162 +
   1.163 +
   1.164 +/*
   1.165 + * Functions have |typeof| == 'function'.
   1.166 + *
   1.167 + * Therefore these expressions should not cause any errors.
   1.168 + * Note we use checkThis() instead of testThis()
   1.169 + *
   1.170 + */
   1.171 +status = inSection(32);
   1.172 +x = function (y) {return y+1;};
   1.173 +checkThis('x("abc")');
   1.174 +
   1.175 +status = inSection(33);
   1.176 +checkThis('(function (y) {return y+1;})("abc")');
   1.177 +
   1.178 +status = inSection(34);
   1.179 +function f(y) { function g() {return y;}; return g();};
   1.180 +checkThis('f("abc")');
   1.181 +
   1.182 +
   1.183 +
   1.184 +//-----------------------------------------------------------------------------
   1.185 +test();
   1.186 +//-----------------------------------------------------------------------------
   1.187 +
   1.188 +
   1.189 +
   1.190 +
   1.191 +/*
   1.192 + * |expr()| should generate a TypeError if |expr| is not a function
   1.193 + */
   1.194 +function testThis(sInvalidSyntax)
   1.195 +{
   1.196 +  expect = TEST_PASSED;
   1.197 +  actual = TEST_FAILED_BADLY;
   1.198 +
   1.199 +  try
   1.200 +  {
   1.201 +    eval(sInvalidSyntax);
   1.202 +  }
   1.203 +  catch(e)
   1.204 +  {
   1.205 +    if (e instanceof TypeError)
   1.206 +      actual = TEST_PASSED;
   1.207 +    else
   1.208 +      actual = TEST_FAILED + e;
   1.209 +  }
   1.210 +
   1.211 +  statusitems[UBound] = status;
   1.212 +  expectedvalues[UBound] = expect;
   1.213 +  actualvalues[UBound] = actual;
   1.214 +  UBound++;
   1.215 +}
   1.216 +
   1.217 +
   1.218 +/*
   1.219 + * Valid syntax shouldn't generate any errors
   1.220 + */
   1.221 +function checkThis(sValidSyntax)
   1.222 +{
   1.223 +  expect = CHECK_PASSED;
   1.224 +  actual = CHECK_PASSED;
   1.225 +
   1.226 +  try
   1.227 +  {
   1.228 +    eval(sValidSyntax);
   1.229 +  }
   1.230 +  catch(e)
   1.231 +  {
   1.232 +    actual = CHECK_FAILED;
   1.233 +  }
   1.234 +
   1.235 +  statusitems[UBound] = status;
   1.236 +  expectedvalues[UBound] = expect;
   1.237 +  actualvalues[UBound] = actual;
   1.238 +  UBound++;
   1.239 +}
   1.240 +
   1.241 +
   1.242 +function test()
   1.243 +{
   1.244 +  enterFunc('test');
   1.245 +  printBugNumber(BUGNUMBER);
   1.246 +  printStatus(summary);
   1.247 +
   1.248 +  for (var i=0; i<UBound; i++)
   1.249 +  {
   1.250 +    reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
   1.251 +  }
   1.252 +
   1.253 +  exitFunc ('test');
   1.254 +}

mercurial