Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: test/mjsunit/es6/object-tostring.js

Issue 546803003: Update ObjectToString to Harmony-draft algorithm (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Add lots more tests, make it safer Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 // Flags: --harmony-tostring
6
7 var global = this;
8
9 // TODO(dslomov, caitp): remove the following simple tests (dupe from
10 // class-of-builtins) when shipping.
Dmitry Lomov (no reviews) 2014/10/18 21:24:19 I wouldn't even add this TODO here, extra tests ar
11 var funs = {
12 Object: [ Object ],
13 Function: [ Function ],
14 Array: [ Array ],
15 String: [ String ],
16 Boolean: [ Boolean ],
17 Number: [ Number ],
18 Date: [ Date ],
19 RegExp: [ RegExp ],
20 Error: [ Error, TypeError, RangeError, SyntaxError, ReferenceError, EvalErr or, URIError ]
Dmitry Lomov (no reviews) 2014/10/18 21:24:19 Nit: 80 line string
Dmitry Lomov (no reviews) 2014/10/18 21:27:14 s/line/column/
21 }
22 for (f in funs) {
23 for (i in funs[f]) {
24 assertEquals("[object " + f + "]",
25 Object.prototype.toString.call(new funs[f][i]),
26 funs[f][i]);
27 assertEquals("[object Function]",
28 Object.prototype.toString.call(funs[f][i]),
29 funs[f][i]);
30 }
31 }
32
33 function testToStringTag(className) {
34 // Using builtin toStringTags
35 var obj = {};
36 obj[Symbol.toStringTag] = className;
37 assertEquals("[object ~" + className + "]",
38 Object.prototype.toString.call(obj));
39
40 // Getter throws
41 obj = {};
42 Object.defineProperty(obj, Symbol.toStringTag, {
43 get: function() { throw className; }
44 });
45 assertThrows(function() {
46 Object.prototype.toString.call(obj);
47 }, className);
48
49 // Getter does not throw
50 obj = {};
51 Object.defineProperty(obj, Symbol.toStringTag, {
52 get: function() { return className; }
53 });
54 assertEquals("[object ~" + className + "]",
55 Object.prototype.toString.call(obj));
56
57 // Custom, non-builtin toStringTags
58 obj = {};
59 obj[Symbol.toStringTag] = "X" + className;
60 assertEquals("[object X" + className + "]",
61 Object.prototype.toString.call(obj));
62
63 // With getter
64 obj = {};
65 Object.defineProperty(obj, Symbol.toStringTag, {
66 get: function() { return "X" + className; }
67 });
68 assertEquals("[object X" + className + "]",
69 Object.prototype.toString.call(obj));
70
71 // Undefined toStringTag should return [object className]
72 var obj = className === "Arguments" ?
Dmitry Lomov (no reviews) 2014/10/18 21:24:19 Formatting is off
73 (function() { return arguments; })() :
74 new global[className];
75 obj[Symbol.toStringTag] = undefined;
76 assertEquals("[object " + className + "]",
77 Object.prototype.toString.call(obj));
78
79 // With getter
80 var obj = className === "Arguments" ?
Dmitry Lomov (no reviews) 2014/10/18 21:24:19 Formatting is off
81 (function() { return arguments; })() :
82 new global[className];
83 Object.defineProperty(obj, Symbol.toStringTag, {
84 get: function() { return undefined; }
85 });
86 assertEquals("[object " + className + "]",
87 Object.prototype.toString.call(obj));
88 }
89
90 [
91 "Arguments",
92 "Array",
93 "Boolean",
94 "Date",
95 "Error",
96 "Function",
97 "Number",
98 "RegExp",
99 "String"
100 ].forEach(testToStringTag);
101
102 function testToStringTagNonString(value) {
103 var obj = {};
104 obj[Symbol.toStringTag] = value;
105 assertEquals("[object ???]", Object.prototype.toString.call(obj));
106
107 // With getter
108 obj = {};
109 Object.defineProperty(obj, Symbol.toStringTag, {
110 get: function() { return value; }
111 });
112 assertEquals("[object ???]", Object.prototype.toString.call(obj));
113 }
114
115 [
116 null,
117 function() {},
118 [],
119 {},
120 /regexp/,
121 42,
122 Symbol("sym"),
123 new Date(),
124 (function() { return arguments; })(),
125 true,
126 new Error("oops"),
127 new String("str")
128 ].forEach(testToStringTagNonString);
Dmitry Lomov (no reviews) 2014/10/18 21:24:19 Also add a test that check properties of Object.pr
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698