OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony-tostring | 5 // Flags: --harmony-tostring --allow-natives-syntax |
caitp (gmail)
2015/02/03 15:49:54
hmmm, I thought I was going to use this for %_Clas
| |
6 | 6 |
7 var global = this; | 7 var global = this; |
8 | 8 |
9 var funs = { | 9 var funs = { |
10 Object: [ Object ], | 10 Object: [ Object ], |
11 Function: [ Function ], | 11 Function: [ Function ], |
12 Array: [ Array ], | 12 Array: [ Array ], |
13 String: [ String ], | 13 String: [ String ], |
14 Boolean: [ Boolean ], | 14 Boolean: [ Boolean ], |
15 Number: [ Number ], | 15 Number: [ Number ], |
(...skipping 10 matching lines...) Expand all Loading... | |
26 assertEquals("[object Function]", | 26 assertEquals("[object Function]", |
27 Object.prototype.toString.call(funs[f][i]), | 27 Object.prototype.toString.call(funs[f][i]), |
28 funs[f][i]); | 28 funs[f][i]); |
29 } | 29 } |
30 } | 30 } |
31 | 31 |
32 function testToStringTag(className) { | 32 function testToStringTag(className) { |
33 // Using builtin toStringTags | 33 // Using builtin toStringTags |
34 var obj = {}; | 34 var obj = {}; |
35 obj[Symbol.toStringTag] = className; | 35 obj[Symbol.toStringTag] = className; |
36 assertEquals("[object ~" + className + "]", | 36 assertEquals("[object " + className + "]", |
37 Object.prototype.toString.call(obj)); | 37 Object.prototype.toString.call(obj)); |
38 | 38 |
39 // Getter throws | 39 // Getter throws |
40 obj = {}; | 40 obj = {}; |
41 Object.defineProperty(obj, Symbol.toStringTag, { | 41 Object.defineProperty(obj, Symbol.toStringTag, { |
42 get: function() { throw className; } | 42 get: function() { throw className; } |
43 }); | 43 }); |
44 assertThrows(function() { | 44 assertThrows(function() { |
45 Object.prototype.toString.call(obj); | 45 Object.prototype.toString.call(obj); |
46 }, className); | 46 }, className); |
47 | 47 |
48 // Getter does not throw | 48 // Getter does not throw |
49 obj = {}; | 49 obj = {}; |
50 Object.defineProperty(obj, Symbol.toStringTag, { | 50 Object.defineProperty(obj, Symbol.toStringTag, { |
51 get: function() { return className; } | 51 get: function() { return className; } |
52 }); | 52 }); |
53 assertEquals("[object ~" + className + "]", | 53 assertEquals("[object " + className + "]", |
54 Object.prototype.toString.call(obj)); | 54 Object.prototype.toString.call(obj)); |
55 | 55 |
56 // Custom, non-builtin toStringTags | 56 // Custom, non-builtin toStringTags |
57 obj = {}; | 57 obj = {}; |
58 obj[Symbol.toStringTag] = "X" + className; | 58 obj[Symbol.toStringTag] = "X" + className; |
59 assertEquals("[object X" + className + "]", | 59 assertEquals("[object X" + className + "]", |
60 Object.prototype.toString.call(obj)); | 60 Object.prototype.toString.call(obj)); |
61 | 61 |
62 // With getter | 62 // With getter |
63 obj = {}; | 63 obj = {}; |
(...skipping 28 matching lines...) Expand all Loading... | |
92 "Error", | 92 "Error", |
93 "Function", | 93 "Function", |
94 "Number", | 94 "Number", |
95 "RegExp", | 95 "RegExp", |
96 "String" | 96 "String" |
97 ].forEach(testToStringTag); | 97 ].forEach(testToStringTag); |
98 | 98 |
99 function testToStringTagNonString(value) { | 99 function testToStringTagNonString(value) { |
100 var obj = {}; | 100 var obj = {}; |
101 obj[Symbol.toStringTag] = value; | 101 obj[Symbol.toStringTag] = value; |
102 assertEquals("[object ???]", Object.prototype.toString.call(obj)); | 102 assertEquals("[object Object]", Object.prototype.toString.call(obj)); |
103 | 103 |
104 // With getter | 104 // With getter |
105 obj = {}; | 105 obj = {}; |
106 Object.defineProperty(obj, Symbol.toStringTag, { | 106 Object.defineProperty(obj, Symbol.toStringTag, { |
107 get: function() { return value; } | 107 get: function() { return value; } |
108 }); | 108 }); |
109 assertEquals("[object ???]", Object.prototype.toString.call(obj)); | 109 assertEquals("[object Object]", Object.prototype.toString.call(obj)); |
110 } | 110 } |
111 | 111 |
112 [ | 112 [ |
113 null, | 113 null, |
114 function() {}, | 114 function() {}, |
115 [], | 115 [], |
116 {}, | 116 {}, |
117 /regexp/, | 117 /regexp/, |
118 42, | 118 42, |
119 Symbol("sym"), | 119 Symbol("sym"), |
120 new Date(), | 120 new Date(), |
121 (function() { return arguments; })(), | 121 (function() { return arguments; })(), |
122 true, | 122 true, |
123 new Error("oops"), | 123 new Error("oops"), |
124 new String("str") | 124 new String("str") |
125 ].forEach(testToStringTagNonString); | 125 ].forEach(testToStringTagNonString); |
126 | 126 |
127 function testObjectToStringPropertyDesc() { | 127 function testObjectToStringPropertyDesc() { |
128 var desc = Object.getOwnPropertyDescriptor(Object.prototype, "toString"); | 128 var desc = Object.getOwnPropertyDescriptor(Object.prototype, "toString"); |
129 assertTrue(desc.writable); | 129 assertTrue(desc.writable); |
130 assertFalse(desc.enumerable); | 130 assertFalse(desc.enumerable); |
131 assertTrue(desc.configurable); | 131 assertTrue(desc.configurable); |
132 } | 132 } |
133 testObjectToStringPropertyDesc(); | 133 testObjectToStringPropertyDesc(); |
134 | 134 |
135 function testObjectToStringOwnNonStringValue() { | 135 function testObjectToStringOwnNonStringValue() { |
136 var obj = Object.defineProperty({}, Symbol.toStringTag, { value: 1 }); | 136 var obj = Object.defineProperty({}, Symbol.toStringTag, { value: 1 }); |
137 assertEquals("[object ???]", ({}).toString.call(obj)); | 137 assertEquals("[object Object]", ({}).toString.call(obj)); |
138 } | 138 } |
139 testObjectToStringOwnNonStringValue(); | 139 testObjectToStringOwnNonStringValue(); |
OLD | NEW |