Skip to content
Snippets Groups Projects
Commit 30d4c041 authored by jugglinmike's avatar jugglinmike Committed by Leo Balter
Browse files

Improve test coverage for Symbol (#645)

parent a12e2712
No related branches found
No related tags found
No related merge requests found
Showing
with 333 additions and 0 deletions
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.for
es6id: 19.4.2.1
description: Creation of a unique Symbol value
info: >
1. Let stringKey be ? ToString(key).
2. For each element e of the GlobalSymbolRegistry List,
a. If SameValue(e.[[Key]], stringKey) is true, return e.[[Symbol]].
3. Assert: GlobalSymbolRegistry does not currently contain an entry for
stringKey.
4. Let newSymbol be a new unique Symbol value whose [[Description]] value
is stringKey.
5. Append the Record { [[Key]]: stringKey, [[Symbol]]: newSymbol } to the
GlobalSymbolRegistry List.
6. Return newSymbol.
---*/
var canonical = Symbol.for('s');
assert.sameValue(typeof canonical, 'symbol');
assert.notSameValue(canonical, Symbol('s'));
assert.notSameValue(canonical, Symbol.for('y'));
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.for
es6id: 19.4.2.1
description: Property descriptor
info: >
Every other data property described in clauses 18 through 26 and in Annex
B.2 has the attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true } unless otherwise specified.
includes: [propertyHelper.js]
---*/
assert.sameValue(typeof Symbol.for, 'function');
verifyNotEnumerable(Symbol, 'for');
verifyWritable(Symbol, 'for');
verifyConfigurable(Symbol, 'for');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.for
es6id: 19.4.2.1
description: Retrieval of previously-created value
info: >
1. Let stringKey be ? ToString(key).
2. For each element e of the GlobalSymbolRegistry List,
a. If SameValue(e.[[Key]], stringKey) is true, return e.[[Symbol]].
3. Assert: GlobalSymbolRegistry does not currently contain an entry for
stringKey.
4. Let newSymbol be a new unique Symbol value whose [[Description]] value
is stringKey.
5. Append the Record { [[Key]]: stringKey, [[Symbol]]: newSymbol } to the
GlobalSymbolRegistry List.
6. Return newSymbol.
---*/
var canonical = Symbol.for('s');
assert.sameValue(typeof canonical, 'symbol');
assert.sameValue(canonical, Symbol.for('s'));
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.for
es6id: 19.4.2.1
description: Error resulting from string coercion of first argument
info: >
1. Let stringKey be ? ToString(key).
---*/
var subject = {
toString: function() {
throw new Test262Error();
}
};
assert.throws(Test262Error, function() {
Symbol.for(subject);
});
subject = Symbol('s');
assert.throws(TypeError, function() {
Symbol.for(subject);
});
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.keyfor
es6id: 19.4.2.5
description: Called with a non-symbol argument
info: >
1. If Type(sym) is not Symbol, throw a TypeError exception.
---*/
assert.sameValue(typeof Symbol.keyFor, 'function');
assert.throws(TypeError, function() {
Symbol.keyFor(null);
}, 'null');
assert.throws(TypeError, function() {
Symbol.keyFor(undefined);
}, 'undefined');
assert.throws(TypeError, function() {
Symbol.keyFor('1');
}, 'number');
assert.throws(TypeError, function() {
Symbol.keyFor('');
}, 'string');
assert.throws(TypeError, function() {
Symbol.keyFor({});
}, 'ordinary object');
assert.throws(TypeError, function() {
Symbol.keyFor([]);
}, 'array exotic object');
assert.throws(TypeError, function() {
Symbol.keyFor(arguments);
}, 'arguments exotic object');
var subject = Object(Symbol('s'));
assert.throws(TypeError, function() {
Symbol.keyFor(subject);
}, 'symbol object');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.keyfor
es6id: 19.4.2.5
description: Called with Symbol value that exists in the global symbol registry
info: >
1. If Type(sym) is not Symbol, throw a TypeError exception.
2. For each element e of the GlobalSymbolRegistry List (see 19.4.2.1),
a. If SameValue(e.[[Symbol]], sym) is true, return e.[[Key]].
---*/
var canonical = Symbol.for('s');
assert.sameValue(Symbol.keyFor(canonical), 's');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.keyfor
es6id: 19.4.2.5
description: >
Called with Symbol value that does not exist in the global symbol registry
info: >
1. If Type(sym) is not Symbol, throw a TypeError exception.
2. For each element e of the GlobalSymbolRegistry List (see 19.4.2.1),
a. If SameValue(e.[[Symbol]], sym) is true, return e.[[Key]].
3. Assert: GlobalSymbolRegistry does not currently contain an entry for
sym.
4. Return undefined.
---*/
var constructed = Symbol('Symbol.iterator');
assert.sameValue(Symbol.keyFor(constructed), undefined, 'constructed symbol');
assert.sameValue(
Symbol.keyFor(Symbol.iterator), undefined, 'well-known symbol'
);
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.keyfor
es6id: 19.4.2.5
description: Property descriptor
info: >
Every other data property described in clauses 18 through 26 and in Annex
B.2 has the attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true } unless otherwise specified.
includes: [propertyHelper.js]
---*/
assert.sameValue(typeof Symbol.keyFor, 'function');
verifyNotEnumerable(Symbol, 'keyFor');
verifyWritable(Symbol, 'keyFor');
verifyConfigurable(Symbol, 'keyFor');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.prototype.constructor
es6id: 19.4.3.1
description: Property descriptor
info: >
Every other data property described in clauses 18 through 26 and in Annex
B.2 has the attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true } unless otherwise specified.
includes: [propertyHelper.js]
---*/
assert.sameValue(Symbol.prototype.constructor, Symbol);
verifyNotEnumerable(Symbol.prototype, 'constructor');
verifyWritable(Symbol.prototype, 'constructor');
verifyConfigurable(Symbol.prototype, 'constructor');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.prototype.tostring
es6id: 19.4.3.2
description: Property descriptor
info: >
Every other data property described in clauses 18 through 26 and in Annex
B.2 has the attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true } unless otherwise specified.
includes: [propertyHelper.js]
---*/
assert.sameValue(typeof Symbol.prototype.toString, 'function');
verifyNotEnumerable(Symbol.prototype, 'toString');
verifyWritable(Symbol.prototype, 'toString');
verifyConfigurable(Symbol.prototype, 'toString');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.prototype.valueof
es6id: 19.4.3.3
description: Property descriptor
info: >
Every other data property described in clauses 18 through 26 and in Annex
B.2 has the attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true } unless otherwise specified.
includes: [propertyHelper.js]
---*/
assert.sameValue(typeof Symbol.prototype.valueOf, 'function');
verifyNotEnumerable(Symbol.prototype, 'valueOf');
verifyWritable(Symbol.prototype, 'valueOf');
verifyConfigurable(Symbol.prototype, 'valueOf');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.prototype.valueof
es6id: 19.4.3.3
description: Called on a value that is neither a Symbol nor an Object
info: |
1. Let s be the this value.
2. If Type(s) is Symbol, return s.
3. If Type(s) is not Object, throw a TypeError exception.
---*/
var valueOf = Symbol.prototype.valueOf;
assert.throws(TypeError, function() {
valueOf.call(null);
}, 'null');
assert.throws(TypeError, function() {
valueOf.call(undefined);
}, 'undefined');
assert.throws(TypeError, function() {
valueOf.call(0);
}, 'number');
assert.throws(TypeError, function() {
valueOf.call('');
}, 'string');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.prototype.valueof
es6id: 19.4.3.3
description: Called on an Object value that is not a Symbol object
info: |
1. Let s be the this value.
2. If Type(s) is Symbol, return s.
3. If Type(s) is not Object, throw a TypeError exception.
4. If s does not have a [[SymbolData]] internal slot, throw a TypeError exception.
---*/
var valueOf = Symbol.prototype.valueOf;
assert.throws(TypeError, function() {
valueOf.call({});
}, 'ordinary object');
assert.throws(TypeError, function() {
valueOf.call([]);
}, 'array exotic object');
assert.throws(TypeError, function() {
valueOf.call(arguments);
}, 'arguments exotic object');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.prototype.valueof
es6id: 19.4.3.3
description: Called on a Symbol Object value
info: |
1. Let s be the this value.
2. If Type(s) is Symbol, return s.
3. If Type(s) is not Object, throw a TypeError exception.
4. If s does not have a [[SymbolData]] internal slot, throw a TypeError exception.
5. Return the value of s's [[SymbolData]] internal slot.
---*/
var valueOf = Symbol.prototype.valueOf;
var symbol = Symbol('s');
var symbolObject = Object(symbol);
assert.sameValue(valueOf.call(symbolObject), symbol);
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-symbol.prototype.valueof
es6id: 19.4.3.3
description: Called on a Symbol value
info: |
1. Let s be the this value.
2. If Type(s) is Symbol, return s.
---*/
var valueOf = Symbol.prototype.valueOf;
var subject = Symbol('s');
assert.sameValue(valueOf.call(subject), subject);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment