Index: test/mjsunit/harmony/symbols.js |
diff --git a/test/mjsunit/harmony/symbols.js b/test/mjsunit/harmony/symbols.js |
index ce02a05ace787196eb5971e416d6601beafa55c7..e432dc2ca5c6cf9cbd358849c4a2fb48f40d7db1 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) { |
rossberg
2014/02/14 10:55:30
Please move this down into TestToString.
sof
2014/02/14 14:20:02
I've kept it here, better to have it be next to wh
|
+ 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() } |
+ 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())) |
rossberg
2014/02/14 10:55:30
Nit: redundant parens
sof
2014/02/14 14:20:02
ok
|
+ 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()) |
+ assertTrue(Symbol.prototype.valueOf.call(symbols[i]) === symbols[i]) |
} |
} |
-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) |
+ |
+ function checkToString(f) { |
+ var stringValue |
+ assertDoesNotThrow(function() { stringValue = f() }) |
+ assertTrue(isValidSymbolString(stringValue)) |
rossberg
2014/02/14 10:55:30
I don't think we need the explicit assertDoesNotTh
sof
2014/02/14 14:20:02
Removed
|
+ } |
+ checkToString(function() { return String(Object(symbols[i])) }) |
+ checkToString(function() { return symbols[i].toString() }) |
+ checkToString(function() { return Object(symbols[i]).toString() }) |
+ checkToString(function() { return Symbol.prototype.toString.call(symbols[i]) }) |
rossberg
2014/02/14 10:55:30
Nit: line length
sof
2014/02/14 14:20:02
Tweaked, but isn't it < 80?
rossberg
2014/02/18 17:05:58
Looked like 83 in the code review...
|
+ |
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])) |
rossberg
2014/02/14 10:55:30
We should keep the original tests, by replacing "n
sof
2014/02/14 14:20:02
Done.
|
- 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]) |
+ assertTrue(Object(symbols[i]) !== Object(symbols[i])) |
+ 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() }) |
rossberg
2014/02/14 10:55:30
Check result
sof
2014/02/14 14:20:02
Done, using isValidSymbolString().
|
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() }) |
rossberg
2014/02/14 10:55:30
Check result
sof
2014/02/14 14:20:02
Done.
|
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("") }) |
rossberg
2014/02/14 10:55:30
Like above, you can remove the assertDoesNotThrow
sof
2014/02/14 14:20:02
Done.
|
+ assertTrue(isValidSymbolString(stringValue)) |
+ } |
+} |
+TestCall() |
+ |
+ |
function TestCollections() { |
var set = new Set |
var map = new Map |
@@ -309,7 +332,7 @@ function TestGetOwnPropertySymbols(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) |