Chromium Code Reviews| Index: test/mjsunit/es6/object-tostring.js |
| diff --git a/test/mjsunit/es6/object-tostring.js b/test/mjsunit/es6/object-tostring.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..62808048a54984f4810d22e749dff4890d8b4d3e |
| --- /dev/null |
| +++ b/test/mjsunit/es6/object-tostring.js |
| @@ -0,0 +1,128 @@ |
| +// Copyright 2014 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// Flags: --harmony-tostring |
| + |
| +var global = this; |
| + |
| +// TODO(dslomov, caitp): remove the following simple tests (dupe from |
| +// 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
|
| +var funs = { |
| + Object: [ Object ], |
| + Function: [ Function ], |
| + Array: [ Array ], |
| + String: [ String ], |
| + Boolean: [ Boolean ], |
| + Number: [ Number ], |
| + Date: [ Date ], |
| + RegExp: [ RegExp ], |
| + Error: [ Error, TypeError, RangeError, SyntaxError, ReferenceError, EvalError, 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/
|
| +} |
| +for (f in funs) { |
| + for (i in funs[f]) { |
| + assertEquals("[object " + f + "]", |
| + Object.prototype.toString.call(new funs[f][i]), |
| + funs[f][i]); |
| + assertEquals("[object Function]", |
| + Object.prototype.toString.call(funs[f][i]), |
| + funs[f][i]); |
| + } |
| +} |
| + |
| +function testToStringTag(className) { |
| + // Using builtin toStringTags |
| + var obj = {}; |
| + obj[Symbol.toStringTag] = className; |
| + assertEquals("[object ~" + className + "]", |
| + Object.prototype.toString.call(obj)); |
| + |
| + // Getter throws |
| + obj = {}; |
| + Object.defineProperty(obj, Symbol.toStringTag, { |
| + get: function() { throw className; } |
| + }); |
| + assertThrows(function() { |
| + Object.prototype.toString.call(obj); |
| + }, className); |
| + |
| + // Getter does not throw |
| + obj = {}; |
| + Object.defineProperty(obj, Symbol.toStringTag, { |
| + get: function() { return className; } |
| + }); |
| + assertEquals("[object ~" + className + "]", |
| + Object.prototype.toString.call(obj)); |
| + |
| + // Custom, non-builtin toStringTags |
| + obj = {}; |
| + obj[Symbol.toStringTag] = "X" + className; |
| + assertEquals("[object X" + className + "]", |
| + Object.prototype.toString.call(obj)); |
| + |
| + // With getter |
| + obj = {}; |
| + Object.defineProperty(obj, Symbol.toStringTag, { |
| + get: function() { return "X" + className; } |
| + }); |
| + assertEquals("[object X" + className + "]", |
| + Object.prototype.toString.call(obj)); |
| + |
| + // Undefined toStringTag should return [object className] |
| + var obj = className === "Arguments" ? |
|
Dmitry Lomov (no reviews)
2014/10/18 21:24:19
Formatting is off
|
| + (function() { return arguments; })() : |
| + new global[className]; |
| + obj[Symbol.toStringTag] = undefined; |
| + assertEquals("[object " + className + "]", |
| + Object.prototype.toString.call(obj)); |
| + |
| + // With getter |
| + var obj = className === "Arguments" ? |
|
Dmitry Lomov (no reviews)
2014/10/18 21:24:19
Formatting is off
|
| + (function() { return arguments; })() : |
| + new global[className]; |
| + Object.defineProperty(obj, Symbol.toStringTag, { |
| + get: function() { return undefined; } |
| + }); |
| + assertEquals("[object " + className + "]", |
| + Object.prototype.toString.call(obj)); |
| +} |
| + |
| +[ |
| + "Arguments", |
| + "Array", |
| + "Boolean", |
| + "Date", |
| + "Error", |
| + "Function", |
| + "Number", |
| + "RegExp", |
| + "String" |
| +].forEach(testToStringTag); |
| + |
| +function testToStringTagNonString(value) { |
| + var obj = {}; |
| + obj[Symbol.toStringTag] = value; |
| + assertEquals("[object ???]", Object.prototype.toString.call(obj)); |
| + |
| + // With getter |
| + obj = {}; |
| + Object.defineProperty(obj, Symbol.toStringTag, { |
| + get: function() { return value; } |
| + }); |
| + assertEquals("[object ???]", Object.prototype.toString.call(obj)); |
| +} |
| + |
| +[ |
| + null, |
| + function() {}, |
| + [], |
| + {}, |
| + /regexp/, |
| + 42, |
| + Symbol("sym"), |
| + new Date(), |
| + (function() { return arguments; })(), |
| + true, |
| + new Error("oops"), |
| + new String("str") |
| +].forEach(testToStringTagNonString); |
|
Dmitry Lomov (no reviews)
2014/10/18 21:24:19
Also add a test that check properties of Object.pr
|