Skip to content

Commit 93d6396

Browse files
authored
Add upsert tests (#4454)
These tests are currently being executed in SpiderMonkey as `non262` tests, and were exported using the `test262-export.py` script. Linting errors were then corrected.
1 parent 27622d7 commit 93d6396

File tree

72 files changed

+2355
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+2355
-0
lines changed

features.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ iterator-sequencing
9696
# https://github.com/tc39/proposal-canonical-tz
9797
canonical-tz
9898

99+
# Upsert
100+
# https://github.com/tc39/proposal-upsert
101+
upsert
102+
99103
## Standard language features
100104
#
101105
# Language features that have been included in a published version of the
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (C) 2015 the V8 project authors. All rights reserved.
2+
// Copyright (C) 2024 Jonas Haukenes. All rights reserved.
3+
// This code is governed by the BSD license found in the LICENSE file.
4+
/*---
5+
esid: proposal-upsert
6+
description: |
7+
Append a new value in the map normalizing +0 and -0.
8+
info: |
9+
Map.prototype.getOrInsert ( key , value )
10+
11+
...
12+
3. Set key to CanonicalizeKeyedCollectionKey(key).
13+
4. For each Record { [[Key]], [[Value]] } p of M.[[MapData]], do
14+
a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]].
15+
5. Let p be the Record { [[Key]]: key, [[Value]]: value }.
16+
6. Append p to M.[[MapData]].
17+
...
18+
features: [Symbol, upsert]
19+
flags: [noStrict]
20+
---*/
21+
var map = new Map();
22+
map.getOrInsert(-0, 42);
23+
assert.sameValue(map.get(0), 42);
24+
25+
map = new Map();
26+
map.getOrInsert(+0, 43);
27+
assert.sameValue(map.get(0), 43);
28+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (C) 2015 the V8 project authors. All rights reserved.
2+
// Copyright (C) 2024 Jonas Haukenes. All rights reserved.
3+
// This code is governed by the BSD license found in the LICENSE file.
4+
/*---
5+
esid: proposal-upsert
6+
description: |
7+
Append a new value as the last element of entries.
8+
info: |
9+
Map.prototype.getOrInsert ( key , value )
10+
11+
...
12+
3. Set key to CanonicalizeKeyedCollectionKey(key).
13+
4. For each Record { [[Key]], [[Value]] } p of M.[[MapData]], do
14+
a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]].
15+
5. Let p be the Record { [[Key]]: key, [[Value]]: value }.
16+
6. Append p to M.[[MapData]].
17+
...
18+
features: [Symbol, upsert]
19+
flags: [noStrict]
20+
---*/
21+
var s = Symbol(2);
22+
var map = new Map([[4, 4], ['foo3', 3], [s, 2]]);
23+
24+
map.getOrInsert(null, 42);
25+
map.getOrInsert(1, 'valid');
26+
27+
assert.sameValue(map.size, 5);
28+
assert.sameValue(map.get(1), 'valid');
29+
30+
var results = [];
31+
32+
map.forEach(function(value, key) {
33+
results.push({
34+
value: value,
35+
key: key
36+
});
37+
});
38+
39+
var result = results.pop();
40+
assert.sameValue(result.value, 'valid');
41+
assert.sameValue(result.key, 1);
42+
43+
result = results.pop();
44+
assert.sameValue(result.value, 42);
45+
assert.sameValue(result.key, null);
46+
47+
result = results.pop();
48+
assert.sameValue(result.value, 2);
49+
assert.sameValue(result.key, s);
50+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (C) 2015 the V8 project authors. All rights reserved.
2+
// Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved.
3+
// This code is governed by the BSD license found in the LICENSE file.
4+
/*---
5+
esid: proposal-upsert
6+
description: |
7+
Inserts the value for the specified key on different types, when key not present.
8+
info: |
9+
Map.prototype.getOrInsert ( key , value )
10+
11+
...
12+
5. Let p be the Record { [[Key]]: key, [[Value]]: value }.
13+
6. Append p to M.[[MapData]].
14+
...
15+
features: [Symbol, upsert]
16+
flags: [noStrict]
17+
---*/
18+
var map = new Map();
19+
20+
map.getOrInsert('bar', 0);
21+
assert.sameValue(map.get('bar'), 0);
22+
23+
map.getOrInsert(1, 42);
24+
assert.sameValue(map.get(1), 42);
25+
26+
map.getOrInsert(NaN, 1);
27+
assert.sameValue(map.get(NaN), 1);
28+
29+
var item = {};
30+
map.getOrInsert(item, 2);
31+
assert.sameValue(map.get(item), 2);
32+
33+
item = [];
34+
map.getOrInsert(item, 3);
35+
assert.sameValue(map.get(item), 3);
36+
37+
item = Symbol('item');
38+
map.getOrInsert(item, 4);
39+
assert.sameValue(map.get(item), 4);
40+
41+
item = null;
42+
map.getOrInsert(item, 5);
43+
assert.sameValue(map.get(item), 5);
44+
45+
item = undefined;
46+
map.getOrInsert(item, 6);
47+
assert.sameValue(map.get(item), 6);
48+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (C) 2015 the V8 project authors. All rights reserved.
2+
// Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved.
3+
// This code is governed by the BSD license found in the LICENSE file.
4+
/*---
5+
esid: proposal-upsert
6+
description: |
7+
Throws a TypeError if `this` is a Set Object
8+
info: |
9+
Map.prototype.getOrInsert ( key , value )
10+
11+
...
12+
1. Let M be the this value.
13+
2. Perform ? RequireInternalSlot(M, [[MapData]])
14+
...
15+
features: [Set, upsert]
16+
flags: [noStrict]
17+
---*/
18+
assert.throws(TypeError, function () {
19+
Map.prototype.getOrInsert.call(new Set(), 1, 1);
20+
});
21+
22+
assert.throws(TypeError, function () {
23+
var map = new Map();
24+
map.getOrInsert.call(new Set(), 1, 1);
25+
});
26+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (C) 2015 the V8 project authors. All rights reserved.
2+
// Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved.
3+
// This code is governed by the BSD license found in the LICENSE file.
4+
/*---
5+
esid: proposal-upsert
6+
description: |
7+
Throws a TypeError if `this` is a WeakMap object.
8+
info: |
9+
Map.prototype.getOrInsert ( key , value )
10+
11+
...
12+
1. Let M be the this value.
13+
2. Perform ? RequireInternalSlot(M, [[MapData]]).
14+
...
15+
features: [WeakMap, upsert]
16+
flags: [noStrict]
17+
---*/
18+
assert.throws(TypeError, function() {
19+
Map.prototype.getOrInsert.call(new WeakMap(), 1, 1);
20+
});
21+
22+
assert.throws(TypeError, function() {
23+
var map = new Map();
24+
map.getOrInsert.call(new WeakMap(), 1, 1);
25+
});
26+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2015 the V8 project authors. All rights reserved.
2+
// Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved.
3+
// This code is governed by the BSD license found in the LICENSE file.
4+
/*---
5+
esid: proposal-upsert
6+
description: |
7+
Throws a TypeError if `this` object does not have a [[MapData]] internal slot.
8+
info: |
9+
Map.getOrInsert ( key , value )
10+
11+
...
12+
1. Let M be the this value.
13+
2. Perform ? RequireInternalSLot(M, [[MapData]])
14+
...
15+
features: [upsert]
16+
flags: [noStrict]
17+
---*/
18+
var map = new Map();
19+
20+
assert.throws(TypeError, function () {
21+
Map.prototype.getOrInsert.call([], 1, 1);
22+
});
23+
24+
assert.throws(TypeError, function () {
25+
map.getOrInsert.call([], 1, 1);
26+
});
27+
28+
assert.throws(TypeError, function () {
29+
Map.prototype.getOrInsert.call({}, 1, 1);
30+
});
31+
32+
assert.throws(TypeError, function () {
33+
map.getOrInsert.call({}, 1, 1);
34+
});
35+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (C) 2015 the V8 project authors. All rights reserved.
2+
// Copyright (C) 2024 Jonas Haukenes. All rights reserved.
3+
// This code is governed by the BSD license found in the LICENSE file.
4+
/*---
5+
esid: proposal-upsert
6+
description: |
7+
Property type and descriptor.
8+
info: |
9+
Map.prototype.getOrInsert ( key , value )
10+
11+
17 ECMAScript Standard Built-in Objects
12+
includes: [propertyHelper.js]
13+
features: [upsert]
14+
flags: [noStrict]
15+
---*/
16+
assert.sameValue(
17+
typeof Map.prototype.getOrInsert,
18+
'function',
19+
'`typeof Map.prototype.getOrInsert` is `function`'
20+
);
21+
22+
23+
verifyProperty(Map.prototype, "getOrInsert", {
24+
value: Map.prototype.getOrInsert,
25+
writable: true,
26+
enumerable: false,
27+
configurable: true
28+
});
29+
30+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (C) 2015 the V8 project authors. All rights reserved.
2+
// Copyright (C) 2024 Jonas Haukenes. All rights reserved.
3+
// This code is governed by the BSD license found in the LICENSE file.
4+
/*---
5+
esid: proposal-upsert
6+
description: |
7+
Map.prototype.getOrInsert.length value and descriptor.
8+
info: |
9+
Map.prototype.getOrInsert ( key , value )
10+
11+
17 ECMAScript Standard Built-in Objects
12+
includes: [propertyHelper.js]
13+
features: [upsert]
14+
flags: [noStrict]
15+
---*/
16+
verifyProperty(Map.prototype.getOrInsert, "length", {
17+
value: 2,
18+
writable: false,
19+
enumerable: false,
20+
configurable: true
21+
});
22+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (C) 2015 the V8 project authors. All rights reserved.
2+
// Copyright (C) 2024 Jonas Haukenes. All rights reserved.
3+
// This code is governed by the BSD license found in the LICENSE file.
4+
/*---
5+
esid: proposal-upsert
6+
description: |
7+
Map.prototype.getOrInsert.name value and descriptor.
8+
info: |
9+
Map.prototype.getOrInsert ( key , value )
10+
11+
17 ECMAScript Standard Built-in Objects
12+
includes: [propertyHelper.js]
13+
features: [upsert]
14+
flags: [noStrict]
15+
---*/
16+
verifyProperty(Map.prototype.getOrInsert, "name", {
17+
value: "getOrInsert",
18+
writable: false,
19+
enumerable: false,
20+
configurable: true
21+
});
22+

0 commit comments

Comments
 (0)