Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / v8 / test / mjsunit / harmony / set-prototype-of.js
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29 function getObjects() {
30   function func() {}
31   return [
32     func,
33     new func(),
34     {x: 5},
35     /regexp/,
36     ['array'],
37     // new Error(),
38     new Date(),
39     new Number(1),
40     new Boolean(true),
41     new String('str'),
42     Object(Symbol())
43   ];
44 }
45
46
47 var coercibleValues = [
48   1,
49   true,
50   'string',
51   Symbol()
52 ];
53
54
55 var nonCoercibleValues = [
56   undefined,
57   null
58 ];
59
60
61 var valuesWithoutNull = coercibleValues.concat(undefined);
62
63
64 function TestSetPrototypeOfCoercibleValues() {
65   for (var i = 0; i < coercibleValues.length; i++) {
66     var value = coercibleValues[i];
67     assertThrows(function() {
68       Object.getPrototypeOf(value);
69     }, TypeError);
70
71     assertEquals(Object.setPrototypeOf(value, {}), value);
72
73     assertThrows(function() {
74       Object.getPrototypeOf(value);
75     }, TypeError);
76   }
77 }
78 TestSetPrototypeOfCoercibleValues();
79
80
81 function TestSetPrototypeOfNonCoercibleValues() {
82   for (var i = 0; i < nonCoercibleValues.length; i++) {
83     var value = nonCoercibleValues[i];
84     assertThrows(function() {
85       Object.setPrototypeOf(value, {});
86     }, TypeError);
87   }
88 }
89 TestSetPrototypeOfNonCoercibleValues();
90
91
92 function TestSetPrototypeToNonObject(proto) {
93   var objects = getObjects();
94   for (var i = 0; i < objects.length; i++) {
95     var object = objects[i];
96     for (var j = 0; j < valuesWithoutNull.length; j++) {
97       var proto = valuesWithoutNull[j];
98       assertThrows(function() {
99         Object.setPrototypeOf(object, proto);
100       }, TypeError);
101     }
102   }
103 }
104 TestSetPrototypeToNonObject();
105
106
107 function TestSetPrototypeOf(object, proto) {
108   assertEquals(Object.setPrototypeOf(object, proto), object);
109   assertEquals(Object.getPrototypeOf(object), proto);
110 }
111
112
113 function TestSetPrototypeOfForObjects() {
114   var objects1 = getObjects();
115   var objects2 = getObjects();
116   for (var i = 0; i < objects1.length; i++) {
117     for (var j = 0; j < objects2.length; j++) {
118       TestSetPrototypeOf(objects1[i], objects2[j]);
119     }
120   }
121 }
122 TestSetPrototypeOfForObjects();
123
124
125 function TestSetPrototypeToNull() {
126   var objects = getObjects();
127   for (var i = 0; i < objects.length; i++) {
128     TestSetPrototypeOf(objects[i], null);
129   }
130 }
131 TestSetPrototypeToNull();
132
133
134 function TestSetPrototypeOfNonExtensibleObject() {
135   var objects = getObjects();
136   var proto = {};
137   for (var i = 0; i < objects.length; i++) {
138     var object = objects[i];
139     Object.preventExtensions(object);
140     assertThrows(function() {
141       Object.setPrototypeOf(object, proto);
142     }, TypeError);
143   }
144 }
145 TestSetPrototypeOfNonExtensibleObject();
146
147
148 function TestLookup() {
149   var object = {};
150   assertFalse('x' in object);
151   assertFalse('y' in object);
152
153   var oldProto = {
154     x: 'old x',
155     y: 'old y'
156   };
157   Object.setPrototypeOf(object, oldProto);
158   assertEquals(object.x, 'old x');
159   assertEquals(object.y, 'old y');
160
161   var newProto = {
162     x: 'new x'
163   };
164   Object.setPrototypeOf(object, newProto);
165   assertEquals(object.x, 'new x');
166   assertFalse('y' in object);
167 }
168 TestLookup();