Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_5 / extensions / extension-methods-reject-null-undefined-this.js
1 /*
2  * Any copyright is dedicated to the Public Domain.
3  * http://creativecommons.org/licenses/publicdomain/
4  */
5
6 //-----------------------------------------------------------------------------
7 var BUGNUMBER = 619283;
8 var summary =
9   "ECMAScript built-in methods that immediately throw when |this| is " +
10   "|undefined| or |null| (due to CheckObjectCoercible, ToObject, or ToString)";
11
12 print(BUGNUMBER + ": " + summary);
13
14 /**************
15  * BEGIN TEST *
16  **************/
17
18 // This test fills out for the non-standard methods which
19 // ecma_5/misc/builtin-methods-reject-null-undefined-this.js declines to test.
20
21 var ClassToMethodMap =
22   {
23     Object:   [/*
24                 * Don't box this just yet for these methods -- they're used too
25                 * much without qualification to do that.  :-(
26                 */
27                /* "__defineGetter__", "__defineSetter__", */
28                "__lookupGetter__", "__lookupSetter__", "watch", "unwatch",
29                "toSource"],
30     Function: ["toSource"],
31     Array:    ["toSource"],
32     String:   ["toSource", "quote", "bold", "italics", "fixed", "fontsize",
33                "fontcolor", "link", "anchor", "strike", "small", "big", "blink",
34                "sup", "sub", "substr", "trimLeft", "trimRight", "toJSON"],
35     Boolean:  ["toSource", "toJSON"],
36     Number:   ["toSource", "toJSON"],
37     Date:     ["toSource", "toLocaleFormat", "getYear", "setYear",
38                "toGMTString"],
39     RegExp:   ["toSource"],
40     Error:    ["toSource"],
41   };
42
43 var badThisValues = [null, undefined];
44
45 function testMethod(Class, className, method)
46 {
47   var expr;
48
49   // Try out explicit this values
50   for (var i = 0, sz = badThisValues.length; i < sz; i++)
51   {
52     var badThis = badThisValues[i];
53
54     expr = className + ".prototype." + method + ".call(" + badThis + ")";
55     try
56     {
57       Class.prototype[method].call(badThis);
58       throw new Error(expr + " didn't throw a TypeError");
59     }
60     catch (e)
61     {
62       assertEq(e instanceof TypeError, true,
63                "wrong error for " + expr + ", instead threw " + e);
64     }
65
66     expr = className + ".prototype." + method + ".apply(" + badThis + ")";
67     try
68     {
69       Class.prototype[method].apply(badThis);
70       throw new Error(expr + " didn't throw a TypeError");
71     }
72     catch (e)
73     {
74       assertEq(e instanceof TypeError, true,
75                "wrong error for " + expr + ", instead threw " + e);
76     }
77   }
78
79   // ..and for good measure..
80
81   expr = "(0, " + className + ".prototype." + method + ")()"
82   try
83   {
84     // comma operator to call GetValue() on the method and de-Reference it
85     (0, Class.prototype[method])();
86     throw new Error(expr + " didn't throw a TypeError");
87   }
88   catch (e)
89   {
90     assertEq(e instanceof TypeError, true,
91              "wrong error for " + expr + ", instead threw " + e);
92   }
93 }
94
95 for (var className in ClassToMethodMap)
96 {
97   var Class = this[className];
98
99   var methodNames = ClassToMethodMap[className];
100   for (var i = 0, sz = methodNames.length; i < sz; i++)
101   {
102     var method = methodNames[i];
103     testMethod(Class, className, method);
104   }
105 }
106
107 /******************************************************************************/
108
109 if (typeof reportCompare === "function")
110   reportCompare(true, true);
111
112 print("All tests passed!");