blob: 973b3c36285b73a0ef7f12dcd9c0f18185b7a5a6 [file] [log] [blame]
Ben Murdoch014dc512016-03-22 12:00:34 +00001// 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
3// found in the LICENSE file.
4
Ben Murdoch014dc512016-03-22 12:00:34 +00005var global = this;
6
7var funs = {
8 Object: [ Object ],
9 Function: [ Function ],
10 String: [ String ],
11 Boolean: [ Boolean ],
12 Number: [ Number ],
13 Date: [ Date ],
14 RegExp: [ RegExp ],
15 Error: [ Error, TypeError, RangeError, SyntaxError, ReferenceError,
16 EvalError, URIError ]
17}
18for (f in funs) {
19 for (i in funs[f]) {
20 assertEquals("[object " + f + "]",
21 Array.prototype.toString.call(new funs[f][i]),
22 funs[f][i]);
23 assertEquals("[object Function]",
24 Array.prototype.toString.call(funs[f][i]),
25 funs[f][i]);
26 }
27}
28
29
30function testToStringTag(className) {
31 // Using builtin toStringTags
32 var obj = {};
33 obj[Symbol.toStringTag] = className;
34 assertEquals("[object " + className + "]",
35 Array.prototype.toString.call(obj));
36
37 // Getter throws
38 obj = {};
39 Object.defineProperty(obj, Symbol.toStringTag, {
40 get: function() { throw className; }
41 });
42 assertThrowsEquals(function() {
43 Array.prototype.toString.call(obj);
44 }, className);
45
46 // Getter does not throw
47 obj = {};
48 Object.defineProperty(obj, Symbol.toStringTag, {
49 get: function() { return className; }
50 });
51 assertEquals("[object " + className + "]",
52 Array.prototype.toString.call(obj));
53
54 // Custom, non-builtin toStringTags
55 obj = {};
56 obj[Symbol.toStringTag] = "X" + className;
57 assertEquals("[object X" + className + "]",
58 Array.prototype.toString.call(obj));
59
60 // With getter
61 obj = {};
62 Object.defineProperty(obj, Symbol.toStringTag, {
63 get: function() { return "X" + className; }
64 });
65 assertEquals("[object X" + className + "]",
66 Array.prototype.toString.call(obj));
67
68 // Undefined toStringTag should return [object className]
69 var obj = className === "Arguments" ?
70 (function() { return arguments; })() : new global[className];
71 obj[Symbol.toStringTag] = undefined;
72 assertEquals("[object " + className + "]",
73 Array.prototype.toString.call(obj));
74
75 // With getter
76 var obj = className === "Arguments" ?
77 (function() { return arguments; })() : new global[className];
78 Object.defineProperty(obj, Symbol.toStringTag, {
79 get: function() { return undefined; }
80 });
81 assertEquals("[object " + className + "]",
82 Array.prototype.toString.call(obj));
83}
84
85
86[
87 "Arguments",
88 "Boolean",
89 "Date",
90 "Error",
91 "Function",
92 "Number",
93 "RegExp",
94 "String"
95].forEach(testToStringTag);
96
97
98function testToStringTagNonString(value) {
99 var obj = {};
100 obj[Symbol.toStringTag] = value;
101 assertEquals("[object Object]", Array.prototype.toString.call(obj));
102
103 // With getter
104 obj = {};
105 Object.defineProperty(obj, Symbol.toStringTag, {
106 get: function() { return value; }
107 });
108 assertEquals("[object Object]", Array.prototype.toString.call(obj));
109}
110
111
112[
113 null,
114 function() {},
115 [],
116 {},
117 /regexp/,
118 42,
119 Symbol("sym"),
120 new Date(),
121 (function() { return arguments; })(),
122 true,
123 new Error("oops"),
124 new String("str")
125].forEach(testToStringTagNonString);
126
127
128function testArrayToStringPropertyDesc() {
129 var desc = Object.getOwnPropertyDescriptor(Object.prototype, "toString");
130 assertTrue(desc.writable);
131 assertFalse(desc.enumerable);
132 assertTrue(desc.configurable);
133}
134testArrayToStringPropertyDesc();
135
136
137function testArrayToStringOwnNonStringValue() {
138 var obj = Object.defineProperty({}, Symbol.toStringTag, { value: 1 });
139 assertEquals("[object Object]", ([]).toString.call(obj));
140}
141testArrayToStringOwnNonStringValue();
142
143
144function testArrayToStringBasic() {
145 assertEquals("1,2,3", [1,2,3].toString());
146 assertEquals(",,3", [,,3].toString());
147}
148testArrayToStringBasic();
149
150
151function testArrayToStringObjectWithCallableJoin() {
152 var obj = { join: function() { return "CallableJoin"; } };
153 assertEquals("CallableJoin", Array.prototype.toString.call(obj));
154}
155testArrayToStringObjectWithCallableJoin();