js/src/tests/ecma_5/extensions/extension-methods-reject-null-undefined-this.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_5/extensions/extension-methods-reject-null-undefined-this.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,112 @@
     1.4 +/*
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/licenses/publicdomain/
     1.7 + */
     1.8 +
     1.9 +//-----------------------------------------------------------------------------
    1.10 +var BUGNUMBER = 619283;
    1.11 +var summary =
    1.12 +  "ECMAScript built-in methods that immediately throw when |this| is " +
    1.13 +  "|undefined| or |null| (due to CheckObjectCoercible, ToObject, or ToString)";
    1.14 +
    1.15 +print(BUGNUMBER + ": " + summary);
    1.16 +
    1.17 +/**************
    1.18 + * BEGIN TEST *
    1.19 + **************/
    1.20 +
    1.21 +// This test fills out for the non-standard methods which
    1.22 +// ecma_5/misc/builtin-methods-reject-null-undefined-this.js declines to test.
    1.23 +
    1.24 +var ClassToMethodMap =
    1.25 +  {
    1.26 +    Object:   [/*
    1.27 +                * Don't box this just yet for these methods -- they're used too
    1.28 +                * much without qualification to do that.  :-(
    1.29 +                */
    1.30 +               /* "__defineGetter__", "__defineSetter__", */
    1.31 +               "__lookupGetter__", "__lookupSetter__", "watch", "unwatch",
    1.32 +               "toSource"],
    1.33 +    Function: ["toSource"],
    1.34 +    Array:    ["toSource"],
    1.35 +    String:   ["toSource", "quote", "bold", "italics", "fixed", "fontsize",
    1.36 +               "fontcolor", "link", "anchor", "strike", "small", "big", "blink",
    1.37 +               "sup", "sub", "substr", "trimLeft", "trimRight", "toJSON"],
    1.38 +    Boolean:  ["toSource", "toJSON"],
    1.39 +    Number:   ["toSource", "toJSON"],
    1.40 +    Date:     ["toSource", "toLocaleFormat", "getYear", "setYear",
    1.41 +               "toGMTString"],
    1.42 +    RegExp:   ["toSource"],
    1.43 +    Error:    ["toSource"],
    1.44 +  };
    1.45 +
    1.46 +var badThisValues = [null, undefined];
    1.47 +
    1.48 +function testMethod(Class, className, method)
    1.49 +{
    1.50 +  var expr;
    1.51 +
    1.52 +  // Try out explicit this values
    1.53 +  for (var i = 0, sz = badThisValues.length; i < sz; i++)
    1.54 +  {
    1.55 +    var badThis = badThisValues[i];
    1.56 +
    1.57 +    expr = className + ".prototype." + method + ".call(" + badThis + ")";
    1.58 +    try
    1.59 +    {
    1.60 +      Class.prototype[method].call(badThis);
    1.61 +      throw new Error(expr + " didn't throw a TypeError");
    1.62 +    }
    1.63 +    catch (e)
    1.64 +    {
    1.65 +      assertEq(e instanceof TypeError, true,
    1.66 +               "wrong error for " + expr + ", instead threw " + e);
    1.67 +    }
    1.68 +
    1.69 +    expr = className + ".prototype." + method + ".apply(" + badThis + ")";
    1.70 +    try
    1.71 +    {
    1.72 +      Class.prototype[method].apply(badThis);
    1.73 +      throw new Error(expr + " didn't throw a TypeError");
    1.74 +    }
    1.75 +    catch (e)
    1.76 +    {
    1.77 +      assertEq(e instanceof TypeError, true,
    1.78 +               "wrong error for " + expr + ", instead threw " + e);
    1.79 +    }
    1.80 +  }
    1.81 +
    1.82 +  // ..and for good measure..
    1.83 +
    1.84 +  expr = "(0, " + className + ".prototype." + method + ")()"
    1.85 +  try
    1.86 +  {
    1.87 +    // comma operator to call GetValue() on the method and de-Reference it
    1.88 +    (0, Class.prototype[method])();
    1.89 +    throw new Error(expr + " didn't throw a TypeError");
    1.90 +  }
    1.91 +  catch (e)
    1.92 +  {
    1.93 +    assertEq(e instanceof TypeError, true,
    1.94 +             "wrong error for " + expr + ", instead threw " + e);
    1.95 +  }
    1.96 +}
    1.97 +
    1.98 +for (var className in ClassToMethodMap)
    1.99 +{
   1.100 +  var Class = this[className];
   1.101 +
   1.102 +  var methodNames = ClassToMethodMap[className];
   1.103 +  for (var i = 0, sz = methodNames.length; i < sz; i++)
   1.104 +  {
   1.105 +    var method = methodNames[i];
   1.106 +    testMethod(Class, className, method);
   1.107 +  }
   1.108 +}
   1.109 +
   1.110 +/******************************************************************************/
   1.111 +
   1.112 +if (typeof reportCompare === "function")
   1.113 +  reportCompare(true, true);
   1.114 +
   1.115 +print("All tests passed!");

mercurial