js/src/tests/ecma_5/Function/function-call.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_5/Function/function-call.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,134 @@
     1.4 +/*
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/licenses/publicdomain/
     1.7 + * Contributor:
     1.8 + *   Jeff Walden <jwalden+code@mit.edu>
     1.9 + */
    1.10 +
    1.11 +//-----------------------------------------------------------------------------
    1.12 +var BUGNUMBER = 575535;
    1.13 +var summary = 'Function.prototype.call';
    1.14 +print(BUGNUMBER + ": " + summary);
    1.15 +
    1.16 +/**************
    1.17 + * BEGIN TEST *
    1.18 + **************/
    1.19 +
    1.20 +function expectTypeError(fun, msg)
    1.21 +{
    1.22 +  try
    1.23 +  {
    1.24 +    fun();
    1.25 +    assertEq(true, false, "should have thrown a TypeError");
    1.26 +  }
    1.27 +  catch (e)
    1.28 +  {
    1.29 +    assertEq(e instanceof TypeError, true, msg + "; instead threw " + e);
    1.30 +  }
    1.31 +}
    1.32 +
    1.33 +function fun() { }
    1.34 +
    1.35 +var global = this;
    1.36 +
    1.37 +assertEq(Function.prototype.call.length, 1);
    1.38 +
    1.39 +
    1.40 +/* Step 1. */
    1.41 +var nonfuns = [null, 1, -1, 2.5, "[[Call]]", undefined, true, false, {}];
    1.42 +for (var i = 0, sz = nonfuns.length; i < sz; i++)
    1.43 +{
    1.44 +  var f = function()
    1.45 +  {
    1.46 +    Function.prototype.call.apply(nonfuns[i]);
    1.47 +  };
    1.48 +  var msg =
    1.49 +    "expected TypeError calling Function.prototype.call with uncallable this";
    1.50 +  expectTypeError(f, msg);
    1.51 +}
    1.52 +
    1.53 +
    1.54 +/* Steps 2-4. */
    1.55 +function none()
    1.56 +{
    1.57 +  assertEq(this, global, "bad this");
    1.58 +  assertEq(arguments.length, 0, "wrong arguments");
    1.59 +}
    1.60 +
    1.61 +none.call();
    1.62 +none.call(undefined);
    1.63 +none.call(null);
    1.64 +
    1.65 +var seenThis;
    1.66 +function strictNone()
    1.67 +{
    1.68 +  "use strict";
    1.69 +  assertEq(this, seenThis, "bad this");
    1.70 +  assertEq(arguments.length, 0, "wrong arguments");
    1.71 +}
    1.72 +
    1.73 +seenThis = undefined;
    1.74 +strictNone.call();
    1.75 +strictNone.call(undefined);
    1.76 +
    1.77 +seenThis = null;
    1.78 +strictNone.call(null);
    1.79 +
    1.80 +seenThis = 17;
    1.81 +strictNone.call(17);
    1.82 +
    1.83 +var seenThisBox, args;
    1.84 +function some()
    1.85 +{
    1.86 +  assertEq(this instanceof seenThisBox, true,
    1.87 +           "this not instanceof " + seenThisBox);
    1.88 +  assertEq(this.valueOf(), seenThis,
    1.89 +           "wrong this valueOf()");
    1.90 +  assertEq(arguments.length, args.length, "wrong arguments count");
    1.91 +  for (var i = 0; i < args.length; i++)
    1.92 +    assertEq(arguments[i], args[i], "wrong argument " + i);
    1.93 +}
    1.94 +
    1.95 +seenThis = false;
    1.96 +seenThisBox = Boolean;
    1.97 +args = [8, 6, 7, NaN, undefined, 0.3];
    1.98 +some.call(false, 8, 6, 7, NaN, undefined, 0.3);
    1.99 +
   1.100 +var obj = {};
   1.101 +
   1.102 +seenThis = "foo";
   1.103 +seenThisBox = String;
   1.104 +args = [obj];
   1.105 +some.call("foo", obj);
   1.106 +
   1.107 +seenThis = obj;
   1.108 +seenThisBox = Object;
   1.109 +some.call(obj, obj);
   1.110 +
   1.111 +function strictSome()
   1.112 +{
   1.113 +  "use strict";
   1.114 +  assertEq(this, seenThis, "wrong this");
   1.115 +  assertEq(arguments.length, args.length, "wrong arguments count");
   1.116 +  for (var i = 0; i < args.length; i++)
   1.117 +    assertEq(arguments[i], args[i], "wrong argument " + i);
   1.118 +}
   1.119 +
   1.120 +seenThis = NaN;
   1.121 +args = [8, 6, 7, NaN, undefined, 0.3];
   1.122 +strictSome.call(NaN, 8, 6, 7, NaN, undefined, 0.3);
   1.123 +
   1.124 +seenThis = "foo";
   1.125 +args = [obj];
   1.126 +strictSome.call("foo", obj);
   1.127 +
   1.128 +seenThis = obj;
   1.129 +strictSome.call(obj, obj);
   1.130 +
   1.131 +
   1.132 +/******************************************************************************/
   1.133 +
   1.134 +if (typeof reportCompare === "function")
   1.135 +  reportCompare(true, true);
   1.136 +
   1.137 +print("All tests passed!");

mercurial