|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 //----------------------------------------------------------------------------- |
|
7 var BUGNUMBER = 444787; |
|
8 var summary = 'Object.getPrototypeOf'; |
|
9 var actual = ''; |
|
10 var expect = ''; |
|
11 |
|
12 |
|
13 //----------------------------------------------------------------------------- |
|
14 test(); |
|
15 //----------------------------------------------------------------------------- |
|
16 |
|
17 function test() |
|
18 { |
|
19 enterFunc ('test'); |
|
20 printBugNumber(BUGNUMBER); |
|
21 printStatus (summary); |
|
22 |
|
23 var i; |
|
24 var type; |
|
25 var instance; |
|
26 var types = [ |
|
27 Array, |
|
28 Boolean, |
|
29 Date, |
|
30 Error, |
|
31 Function, |
|
32 Math, |
|
33 Number, |
|
34 Object, |
|
35 RegExp, |
|
36 String, |
|
37 ]; |
|
38 |
|
39 for (i = 0; i < types.length; i++) |
|
40 { |
|
41 type = types[i]; |
|
42 |
|
43 if (typeof type.__proto__ != 'undefined') |
|
44 { |
|
45 expect = type.__proto__; |
|
46 actual = Object.getPrototypeOf(type); |
|
47 reportCompare(expect, actual, summary + ': ' + type.name); |
|
48 } |
|
49 |
|
50 try |
|
51 { |
|
52 eval('instance = new ' + type.name); |
|
53 expect = type.prototype; |
|
54 actual = Object.getPrototypeOf(instance); |
|
55 reportCompare(expect, actual, summary + ': new ' + type.name); |
|
56 } |
|
57 catch(ex if ex instanceof TypeError) |
|
58 { |
|
59 print('Ignore ' + ex); |
|
60 } |
|
61 catch(ex) |
|
62 { |
|
63 actual = ex + ''; |
|
64 reportCompare(expect, actual, summary + ': new ' + type.name); |
|
65 } |
|
66 |
|
67 } |
|
68 |
|
69 types = [null, undefined]; |
|
70 |
|
71 for (i = 0; i < types.length; i++) |
|
72 { |
|
73 type = types[i]; |
|
74 expect = 'TypeError: Object.getPrototype is not a function'; |
|
75 try |
|
76 { |
|
77 actual = Object.getPrototype(null); |
|
78 } |
|
79 catch(ex) |
|
80 { |
|
81 actual = ex + ''; |
|
82 } |
|
83 reportCompare(expect, actual, summary + ': ' + type); |
|
84 } |
|
85 |
|
86 var objects = [ |
|
87 {instance: [0], type: Array}, |
|
88 {instance: (function () {}), type: Function}, |
|
89 {instance: eval, type: Function}, |
|
90 {instance: parseInt, type: Function}, |
|
91 {instance: {a: ''}, type: Object}, |
|
92 {instance: /foo/, type: RegExp} |
|
93 ]; |
|
94 |
|
95 for (i = 0; i < objects.length; i++) |
|
96 { |
|
97 instance = objects[i].instance; |
|
98 type = objects[i].type; |
|
99 expect = type.prototype; |
|
100 actual = Object.getPrototypeOf(instance); |
|
101 reportCompare(expect, actual, summary + ' instance: ' + instance + ', type: ' + type.name); |
|
102 } |
|
103 |
|
104 var non_objects = [ true, false, 1.0, Infinity, NaN, Math.PI, "bar" ]; |
|
105 |
|
106 for (i = 0; i < non_objects.length; i++) |
|
107 { |
|
108 instance = non_objects[i]; |
|
109 expect = 'TypeError: instance is not an object'; |
|
110 try |
|
111 { |
|
112 actual = Object.getPrototypeOf(instance); |
|
113 } |
|
114 catch(ex) |
|
115 { |
|
116 actual = ex + ''; |
|
117 } |
|
118 reportCompare(expect, actual, summary + ' non-object: ' + actual); |
|
119 } |
|
120 |
|
121 exitFunc ('test'); |
|
122 } |