Index: test/mjsunit/harmony/symbols.js |
diff --git a/test/mjsunit/harmony/symbols.js b/test/mjsunit/harmony/symbols.js |
index 3fcd06dfdca985fcd81abd8630cb8ededa8fe0d7..274bac81028b0cb20a717d538838ab565e957637 100644 |
--- a/test/mjsunit/harmony/symbols.js |
+++ b/test/mjsunit/harmony/symbols.js |
@@ -30,22 +30,30 @@ |
var symbols = [] |
+ |
+// Returns true if the string is a valid |
+// serialization of Symbols added to the 'symbols' |
+// array. Adjust if you extend 'symbols' with other |
+// values. |
+function isValidSymbolString(s) { |
+ return ["Symbol(66)", "Symbol()"].indexOf(s) >= 0; |
+} |
+ |
+ |
// Test different forms of constructor calls, all equivalent. |
function TestNew() { |
- function IndirectSymbol() { return new Symbol } |
- function indirect() { return new IndirectSymbol() } |
+ function IndirectSymbol() { return Symbol() } |
arv (Not doing code reviews)
2014/02/01 01:33:10
Should be lower case now since it is not a constr
|
+ function indirect() { return IndirectSymbol() } |
for (var i = 0; i < 2; ++i) { |
for (var j = 0; j < 5; ++j) { |
symbols.push(Symbol()) |
symbols.push(Symbol(undefined)) |
symbols.push(Symbol("66")) |
symbols.push(Symbol(66)) |
- symbols.push(Symbol(Symbol())) |
- symbols.push((new Symbol).valueOf()) |
- symbols.push((new Symbol()).valueOf()) |
- symbols.push((new Symbol(Symbol())).valueOf()) |
- symbols.push(Object(Symbol()).valueOf()) |
- symbols.push((indirect()).valueOf()) |
+ symbols.push(Symbol().valueOf()) |
+ symbols.push((indirect())) |
+ assertThrows(function () { Symbol(Symbol()) }, TypeError) |
+ assertThrows(function () { new Symbol(66) }, TypeError) |
} |
%OptimizeFunctionOnNextCall(indirect) |
indirect() // Call once before GC throws away type feedback. |
@@ -61,7 +69,6 @@ function TestType() { |
assertTrue(typeof symbols[i] === "symbol") |
assertFalse(%SymbolIsPrivate(symbols[i])) |
assertEquals(null, %_ClassOf(symbols[i])) |
- assertEquals("Symbol", %_ClassOf(new Symbol(symbols[i]))) |
assertEquals("Symbol", %_ClassOf(Object(symbols[i]))) |
} |
} |
@@ -71,10 +78,6 @@ TestType() |
function TestPrototype() { |
assertSame(Object.prototype, Symbol.prototype.__proto__) |
assertSame(Symbol.prototype, Symbol().__proto__) |
- assertSame(Symbol.prototype, Symbol(Symbol()).__proto__) |
- assertSame(Symbol.prototype, (new Symbol).__proto__) |
- assertSame(Symbol.prototype, (new Symbol()).__proto__) |
- assertSame(Symbol.prototype, (new Symbol(Symbol())).__proto__) |
assertSame(Symbol.prototype, Object(Symbol()).__proto__) |
for (var i in symbols) { |
assertSame(Symbol.prototype, symbols[i].__proto__) |
@@ -84,14 +87,11 @@ TestPrototype() |
function TestConstructor() { |
+ assertSame(Function.prototype, Symbol.__proto__) |
assertFalse(Object === Symbol.prototype.constructor) |
assertFalse(Symbol === Object.prototype.constructor) |
assertSame(Symbol, Symbol.prototype.constructor) |
assertSame(Symbol, Symbol().__proto__.constructor) |
- assertSame(Symbol, Symbol(Symbol()).__proto__.constructor) |
- assertSame(Symbol, (new Symbol).__proto__.constructor) |
- assertSame(Symbol, (new Symbol()).__proto__.constructor) |
- assertSame(Symbol, (new Symbol(Symbol())).__proto__.constructor) |
assertSame(Symbol, Object(Symbol()).__proto__.constructor) |
for (var i in symbols) { |
assertSame(Symbol, symbols[i].__proto__.constructor) |
@@ -100,22 +100,31 @@ function TestConstructor() { |
TestConstructor() |
-function TestName() { |
+function TestValueOf() { |
for (var i in symbols) { |
- var name = symbols[i].name |
- assertTrue(name === undefined || name === "66") |
+ assertTrue(symbols[i] === symbols[i].valueOf()) |
+ assertThrows(function() { Symbol.prototype.valueOf.call(symbols[i]) }, TypeError) |
} |
} |
-TestName() |
+TestValueOf() |
function TestToString() { |
for (var i in symbols) { |
assertThrows(function() { String(symbols[i]) }, TypeError) |
assertThrows(function() { symbols[i] + "" }, TypeError) |
- assertThrows(function() { symbols[i].toString() }, TypeError) |
- assertThrows(function() { (new Symbol(symbols[i])).toString() }, TypeError) |
- assertThrows(function() { Object(symbols[i]).toString() }, TypeError) |
+ assertThrows(function() { (Symbol(symbols[i])).toString() }, TypeError) |
+ assertThrows(function() { Symbol.prototype.toString.call(symbols[i]) }, TypeError) |
+ |
+ function checkToString(f) { |
+ var stringValue |
+ assertDoesNotThrow(function() { stringValue = f() }) |
+ assertTrue(isValidSymbolString(stringValue)) |
+ } |
+ checkToString(function() { return String(Object(symbols[i])) }) |
+ checkToString(function() { return symbols[i].toString() }) |
+ checkToString(function() { return Object(symbols[i]).toString() }) |
+ |
assertEquals("[object Symbol]", Object.prototype.toString.call(symbols[i])) |
} |
} |
@@ -156,10 +165,12 @@ function TestEquality() { |
assertTrue(Object.is(symbols[i], symbols[i])) |
assertTrue(symbols[i] === symbols[i]) |
assertTrue(symbols[i] == symbols[i]) |
- assertFalse(symbols[i] === new Symbol(symbols[i])) |
- assertFalse(new Symbol(symbols[i]) === symbols[i]) |
- assertTrue(symbols[i] == new Symbol(symbols[i])) |
- assertTrue(new Symbol(symbols[i]) == symbols[i]) |
+ assertTrue(symbols[i] === symbols[i].valueOf()) |
+ assertTrue(symbols[i].valueOf() === symbols[i]) |
+ assertTrue(symbols[i] == symbols[i].valueOf()) |
+ assertTrue(symbols[i].valueOf() == symbols[i]) |
+ assertEquals(Object(symbols[i]), Object(symbols[i])) |
arv (Not doing code reviews)
2014/02/01 01:33:10
This looks strange. If symbol[i] is a symbol (not
sof
2014/02/01 13:58:10
They're all Symbol values. This is an equals check
arv (Not doing code reviews)
2014/02/02 19:49:35
If all elements in symbols are symbol values (not
sof
2014/02/02 19:59:57
I know; the object also doesn't have any "keys" in
|
+ assertEquals(Object(symbols[i]).valueOf(), Object(symbols[i]).valueOf()) |
} |
// All symbols should be distinct. |
@@ -187,7 +198,7 @@ TestEquality() |
function TestGet() { |
for (var i in symbols) { |
- assertThrows(function() { symbols[i].toString() }, TypeError) |
+ assertDoesNotThrow(function() { symbols[i].toString() }) |
assertEquals(symbols[i], symbols[i].valueOf()) |
assertEquals(undefined, symbols[i].a) |
assertEquals(undefined, symbols[i]["a" + "b"]) |
@@ -201,7 +212,7 @@ TestGet() |
function TestSet() { |
for (var i in symbols) { |
symbols[i].toString = 0 |
- assertThrows(function() { symbols[i].toString() }, TypeError) |
+ assertDoesNotThrow(function() { symbols[i].toString() }) |
symbols[i].valueOf = 0 |
assertEquals(symbols[i], symbols[i].valueOf()) |
symbols[i].a = 0 |
@@ -215,6 +226,18 @@ function TestSet() { |
TestSet() |
+// Test Symbol wrapping/boxing over non-builtins. |
+Symbol.prototype.extra = function (m) { return (m + this.toString()) } |
+function TestCall() { |
+ for (var i in symbols) { |
+ var stringValue |
+ assertDoesNotThrow(function() { stringValue = symbols[i].extra("") }) |
+ assertTrue(isValidSymbolString(stringValue)) |
+ } |
+} |
+TestCall() |
+ |
+ |
function TestCollections() { |
var set = new Set |
var map = new Map |
@@ -300,7 +323,7 @@ function TestKeyNames(obj) { |
function TestKeyDescriptor(obj) { |
for (var i in symbols) { |
- var desc = Object.getOwnPropertyDescriptor(obj, symbols[i]); |
+ var desc = Object.getOwnPropertyDescriptor(obj, symbols[i]) |
assertEquals(i|0, desc.value) |
assertTrue(desc.configurable) |
assertEquals(i % 2 == 0, desc.writable) |