Map.prototype.set

This commit is contained in:
Leonardo Balter 2015-06-29 17:59:54 -04:00
parent 48f4131007
commit 7ee11aae4e
13 changed files with 370 additions and 0 deletions

View File

@ -0,0 +1,25 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.1.3.9
description: >
Appends new value in the map normalizing +0 and -0.
info: >
Map.prototype.set ( key , value )
...
6. If key is 0, let key be +0.
7. Let p be the Record {[[key]]: key, [[value]]: value}.
8. Append p as the last element of entries.
9. Return M.
...
---*/
var map = new Map();
map.set(-0, 42);
assert.sameValue(map.get(0), 42);
map = new Map();
map.set(+0, 43);
assert.sameValue(map.get(0), 43);

View File

@ -0,0 +1,30 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.1.3.9
description: >
Map.prototype.set returns the given `this` object.
info: >
Map.prototype.set ( key , value )
...
6. If key is 0, let key be +0.
7. Let p be the Record {[[key]]: key, [[value]]: value}.
8. Append p as the last element of entries.
9. Return M.
...
---*/
var map = new Map();
var result = map.set(1, 1);
assert.sameValue(result, map);
result = map.set(1,1).set(2,2).set(3,3);
assert.sameValue(result, map, 'Map.prototype.set is chainable');
var map2 = new Map();
result = map2.set.call(map, 4, 4);
assert.sameValue(result, map);

View File

@ -0,0 +1,47 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.1.3.9
description: >
Append a new value as the last element of entries.
info: >
Map.prototype.set ( key , value )
...
6. If key is 0, let key be +0.
7. Let p be the Record {[[key]]: key, [[value]]: value}.
8. Append p as the last element of entries.
9. Return M.
...
features: [Symbol]
---*/
var s = Symbol(2);
var map = new Map([[4, 4], ['foo3', 3], [s, 2]]);
map.set(null, 42);
map.set(1, 'valid');
assert.sameValue(map.size, 5);
assert.sameValue(map.get(1), 'valid');
var results = [];
map.forEach(function(value, key) {
results.push({
value: value,
key: key
});
});
var result = results.pop();
assert.sameValue(result.value, 'valid');
assert.sameValue(result.key, 1);
result = results.pop();
assert.sameValue(result.value, 42);
assert.sameValue(result.key, null);
result = results.pop();
assert.sameValue(result.value, 2);
assert.sameValue(result.key, s);

View File

@ -0,0 +1,24 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.1.3.9
description: >
Throws a TypeError if `this` is a Set object.
info: >
Map.prototype.set ( key , value )
...
3. If M does not have a [[MapData]] internal slot, throw a TypeError
exception.
...
features: [Set]
---*/
assert.throws(TypeError, function() {
Map.prototype.set.call(new Set(), 1, 1);
});
assert.throws(TypeError, function() {
var m = new Map();
m.set.call(new Set(), 1, 1);
});

View File

@ -0,0 +1,24 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.1.3.9
description: >
Throws a TypeError if `this` is a WeakMap object.
info: >
Map.prototype.set ( key , value )
...
3. If M does not have a [[MapData]] internal slot, throw a TypeError
exception.
...
features: [WeakMap]
---*/
assert.throws(TypeError, function() {
Map.prototype.set.call(new WeakMap(), 1, 1);
});
assert.throws(TypeError, function() {
var m = new Map();
m.set.call(new WeakMap(), 1, 1);
});

View File

@ -0,0 +1,32 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.1.3.9
description: >
Throws a TypeError if `this` object does not have a [[MapData]] internal slot.
info: >
Map.prototype.set ( key , value )
...
3. If M does not have a [[MapData]] internal slot, throw a TypeError
exception.
...
---*/
var m = new Map();
assert.throws(TypeError, function() {
Map.prototype.set.call([], 1, 1);
});
assert.throws(TypeError, function() {
m.set.call([], 1, 1);
});
assert.throws(TypeError, function() {
Map.prototype.set.call({}, 1, 1);
});
assert.throws(TypeError, function() {
m.set.call({}, 1, 1);
});

View File

@ -0,0 +1,22 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.1.3.9
description: >
Map.prototype.set.length value and descriptor.
info: >
Map.prototype.set ( key , value )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
Map.prototype.set.length, 2,
'The value of `Map.prototype.set.length` is `2`'
);
verifyNotEnumerable(Map.prototype.set, 'length');
verifyNotWritable(Map.prototype.set, 'length');
verifyConfigurable(Map.prototype.set, 'length');

View File

@ -0,0 +1,22 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.1.3.9
description: >
Map.prototype.set.name value and descriptor.
info: >
Map.prototype.set ( key , value )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
Map.prototype.set.name, 'set',
'The value of `Map.prototype.set.name` is `"set"`'
);
verifyNotEnumerable(Map.prototype.set, 'name');
verifyNotWritable(Map.prototype.set, 'name');
verifyConfigurable(Map.prototype.set, 'name');

View File

@ -0,0 +1,26 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.1.3.9
description: >
Replaces a value in the map normalizing +0 and -0.
info: >
Map.prototype.set ( key , value )
...
5. Repeat for each Record {[[key]], [[value]]} p that is an element of
entries,
a. If p.[[key]] is not empty and SameValueZero(p.[[key]], key) is true, then
i. Set p.[[value]] to value.
ii. Return M.
...
---*/
var map = new Map([[+0, 1]]);
map.set(-0, 42);
assert.sameValue(map.get(+0), 42, 'zero key is normalized in SameValueZero');
map = new Map([[-0, 1]]);
map.set(+0, 42);
assert.sameValue(map.get(-0), 42, 'zero key is normalized in SameValueZero');

View File

@ -0,0 +1,30 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.1.3.9
description: >
Map.prototype.set returns the given `this` map object.
info: >
Map.prototype.set ( key , value )
1. Let M be the this value.
...
5. Repeat for each Record {[[key]], [[value]]} p that is an element of
entries,
a. If p.[[key]] is not empty and SameValueZero(p.[[key]], key) is true, then
i. Set p.[[value]] to value.
ii. Return M.
...
---*/
var map = new Map([['item', 0]]);
var map2 = new Map();
var x = map.set('item', 42);
assert.sameValue(x, map);
x = Map.prototype.set.call(map, 'item', 0);
assert.sameValue(x, map);
x = map2.set.call(map, 'item', 0);
assert.sameValue(x, map, 'Map#set returns the map `this` value');

View File

@ -0,0 +1,23 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.1.3.9
description: >
Replaces a value in the map.
info: >
Map.prototype.set ( key , value )
...
5. Repeat for each Record {[[key]], [[value]]} p that is an element of
entries,
a. If p.[[key]] is not empty and SameValueZero(p.[[key]], key) is true, then
i. Set p.[[value]] to value.
ii. Return M.
...
---*/
var m = new Map([['item', 1]]);
m.set('item', 42);
assert.sameValue(m.get('item'), 42);
assert.sameValue(m.size, 1);

22
test/built-ins/Map/prototype/set/set.js vendored Normal file
View File

@ -0,0 +1,22 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.1.3.9
description: >
Property type and descriptor.
info: >
Map.prototype.set ( key , value )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
typeof Map.prototype.set,
'function',
'`typeof Map.prototype.set` is `function`'
);
verifyNotEnumerable(Map.prototype, 'set');
verifyWritable(Map.prototype, 'set');
verifyConfigurable(Map.prototype, 'set');

View File

@ -0,0 +1,43 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.1.3.9
description: >
Throws a TypeError if `this` is not an Object.
info: >
Map.prototype.set ( key , value )
1. Let M be the this value.
2. If Type(M) is not Object, throw a TypeError exception.
...
features: [Symbol]
---*/
assert.throws(TypeError, function() {
Map.prototype.set.call(false, 1, 1);
});
assert.throws(TypeError, function() {
Map.prototype.set.call(1, 1, 1);
});
assert.throws(TypeError, function() {
Map.prototype.set.call('', 1, 1);
});
assert.throws(TypeError, function() {
Map.prototype.set.call(undefined, 1, 1);
});
assert.throws(TypeError, function() {
Map.prototype.set.call(null, 1, 1);
});
assert.throws(TypeError, function() {
Map.prototype.set.call(Symbol(), 1, 1);
});
assert.throws(TypeError, function() {
var map = new Map();
map.set.call(false, 1, 1);
});