-
Notifications
You must be signed in to change notification settings - Fork 528
Add upsert tests #4454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add upsert tests #4454
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2804e48
Add upsert tests
dminor c5fe2f9
Fixup placement of copyright line
dminor d16339d
Fixup a few more misplaced copyrights
dminor ed3b39a
Add feature for upsert
dminor 9ca2d33
Replace assertThrowsInstanceOf with assert.throws
dminor c081cdb
Remove cross-compartment tests
dminor 9e97e4b
Use verifyProperty
dminor fa26717
Fix feature for Map/getOrInsertComputed/returns-value-if-key-is-not-p…
dminor d491a97
Merge branch 'main' into add-upsert-tests
dminor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
test/staging/upsert/Map/getOrInsert-this-is-a-cross-compartment.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Copyright (C) 2025 Mozilla Corporation. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
|
|
||
| /*--- | ||
| flags: [noStrict] | ||
| description: | | ||
| pending | ||
| esid: proposal-upsert | ||
| ---*/ | ||
|
|
||
| const g = createNewGlobal({ newCompartment: true }); | ||
|
|
||
| var map = g.eval("new Map()"); | ||
|
|
||
| Map.prototype.getOrInsert.call(map, 1, 2); | ||
| assert.sameValue(map.get(1), 2); | ||
|
|
||
| map.getOrInsert(2, 3); | ||
| assert.sameValue(map.get(2), 3); | ||
|
|
28 changes: 28 additions & 0 deletions
28
test/staging/upsert/Map/getOrInsert/append-new-values-normalizes-zero-key.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright (C) 2024 Jonas Haukenes. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: proposal-upsert | ||
| description: | | ||
| Append a new value in the map normalizing +0 and -0. | ||
| info: | | ||
| Map.prototype.getOrInsert ( key , value ) | ||
|
|
||
| ... | ||
| 3. Set key to CanonicalizeKeyedCollectionKey(key). | ||
| 4. For each Record { [[Key]], [[Value]] } p of M.[[MapData]], do | ||
| a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]]. | ||
| 5. Let p be the Record { [[Key]]: key, [[Value]]: value }. | ||
| 6. Append p to M.[[MapData]]. | ||
| ... | ||
| features: [Symbol] | ||
| flags: [noStrict] | ||
| ---*/ | ||
| // Copyright (C) 2015 the V8 project authors. All rights reserved. | ||
| var map = new Map(); | ||
| map.getOrInsert(-0, 42); | ||
| assert.sameValue(map.get(0), 42); | ||
|
|
||
| map = new Map(); | ||
| map.getOrInsert(+0, 43); | ||
| assert.sameValue(map.get(0), 43); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright (C) 2024 Jonas Haukenes. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: proposal-upsert | ||
| description: | | ||
| Append a new value as the last element of entries. | ||
| info: | | ||
| Map.prototype.getOrInsert ( key , value ) | ||
|
|
||
| ... | ||
| 3. Set key to CanonicalizeKeyedCollectionKey(key). | ||
| 4. For each Record { [[Key]], [[Value]] } p of M.[[MapData]], do | ||
| a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]]. | ||
| 5. Let p be the Record { [[Key]]: key, [[Value]]: value }. | ||
| 6. Append p to M.[[MapData]]. | ||
| ... | ||
| features: [Symbol] | ||
| flags: [noStrict] | ||
| ---*/ | ||
| // Copyright (C) 2015 the V8 project authors. All rights reserved. | ||
| var s = Symbol(2); | ||
| var map = new Map([[4, 4], ['foo3', 3], [s, 2]]); | ||
|
|
||
| map.getOrInsert(null, 42); | ||
| map.getOrInsert(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); | ||
|
|
49 changes: 49 additions & 0 deletions
49
.../staging/upsert/Map/getOrInsert/append-value-if-key-is-not-present-different-key-types.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: proposal-upsert | ||
| description: | | ||
| Inserts the value for the specified key on different types, when key not present. | ||
| info: | | ||
| Map.prototype.getOrInsert ( key , value ) | ||
|
|
||
| ... | ||
| 5. Let p be the Record { [[Key]]: key, [[Value]]: value }. | ||
| 6. Append p to M.[[MapData]]. | ||
| ... | ||
| features: | ||
| - Symbol | ||
| flags: [noStrict] | ||
| ---*/ | ||
| // Copyright (C) 2015 the V8 project authors. All rights reserved. | ||
| var map = new Map(); | ||
|
|
||
| map.getOrInsert('bar', 0); | ||
| assert.sameValue(map.get('bar'), 0); | ||
|
|
||
| map.getOrInsert(1, 42); | ||
| assert.sameValue(map.get(1), 42); | ||
|
|
||
| map.getOrInsert(NaN, 1); | ||
| assert.sameValue(map.get(NaN), 1); | ||
|
|
||
| var item = {}; | ||
| map.getOrInsert(item, 2); | ||
| assert.sameValue(map.get(item), 2); | ||
|
|
||
| item = []; | ||
| map.getOrInsert(item, 3); | ||
| assert.sameValue(map.get(item), 3); | ||
|
|
||
| item = Symbol('item'); | ||
| map.getOrInsert(item, 4); | ||
| assert.sameValue(map.get(item), 4); | ||
|
|
||
| item = null; | ||
| map.getOrInsert(item, 5); | ||
| assert.sameValue(map.get(item), 5); | ||
|
|
||
| item = undefined; | ||
| map.getOrInsert(item, 6); | ||
| assert.sameValue(map.get(item), 6); | ||
|
|
26 changes: 26 additions & 0 deletions
26
test/staging/upsert/Map/getOrInsert/does-not-have-mapdata-internal-slot-set.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: proposal-upsert | ||
| description: | | ||
| Throws a TypeError if `this` is a Set Object | ||
| info: | | ||
| Map.prototype.getOrInsert ( key , value ) | ||
|
|
||
| ... | ||
| 1. Let M be the this value. | ||
| 2. Perform ? RequireInternalSlot(M, [[MapData]]) | ||
| ... | ||
| features: [Set] | ||
| flags: [noStrict] | ||
| ---*/ | ||
| // Copyright (C) 2015 the V8 project authors. All rights reserved. | ||
| assertThrowsInstanceOf(function () { | ||
| Map.prototype.getOrInsert.call(new Set(), 1, 1); | ||
| }, TypeError); | ||
|
|
||
| assertThrowsInstanceOf(function () { | ||
| var map = new Map(); | ||
| map.getOrInsert.call(new Set(), 1, 1); | ||
| }, TypeError); | ||
|
|
26 changes: 26 additions & 0 deletions
26
test/staging/upsert/Map/getOrInsert/does-not-have-mapdata-internal-slot-weakmap.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: proposal-upsert | ||
| description: | | ||
| Throws a TypeError if `this` is a WeakMap object. | ||
| info: | | ||
| Map.prototype.getOrInsert ( key , value ) | ||
|
|
||
| ... | ||
| 1. Let M be the this value. | ||
| 2. Perform ? RequireInternalSlot(M, [[MapData]]). | ||
| ... | ||
| features: [WeakMap] | ||
| flags: [noStrict] | ||
| ---*/ | ||
| // Copyright (C) 2015 the V8 project authors. All rights reserved. | ||
| assertThrowsInstanceOf(function() { | ||
| Map.prototype.getOrInsert.call(new WeakMap(), 1, 1); | ||
| }, TypeError); | ||
|
|
||
| assertThrowsInstanceOf(function() { | ||
| var map = new Map(); | ||
| map.getOrInsert.call(new WeakMap(), 1, 1); | ||
| }, TypeError); | ||
|
|
34 changes: 34 additions & 0 deletions
34
test/staging/upsert/Map/getOrInsert/does-not-have-mapdata-internal-slot.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: proposal-upsert | ||
| description: | | ||
| Throws a TypeError if `this` object does not have a [[MapData]] internal slot. | ||
| info: | | ||
| Map.getOrInsert ( key , value ) | ||
|
|
||
| ... | ||
| 1. Let M be the this value. | ||
| 2. Perform ? RequireInternalSLot(M, [[MapData]]) | ||
| ... | ||
| flags: [noStrict] | ||
| ---*/ | ||
| // Copyright (C) 2015 the V8 project authors. All rights reserved. | ||
| var map = new Map(); | ||
|
|
||
| assertThrowsInstanceOf(function () { | ||
| Map.prototype.getOrInsert.call([], 1, 1); | ||
| }, TypeError); | ||
|
|
||
| assertThrowsInstanceOf(function () { | ||
| map.getOrInsert.call([], 1, 1); | ||
| }, TypeError); | ||
|
|
||
| assertThrowsInstanceOf(function () { | ||
| Map.prototype.getOrInsert.call({}, 1, 1); | ||
| }, TypeError); | ||
|
|
||
| assertThrowsInstanceOf(function () { | ||
| map.getOrInsert.call({}, 1, 1); | ||
| }, TypeError); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (C) 2024 Jonas Haukenes. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: proposal-upsert | ||
| description: | | ||
| Property type and descriptor. | ||
| info: | | ||
| Map.prototype.getOrInsert ( key , value ) | ||
|
|
||
| 17 ECMAScript Standard Built-in Objects | ||
| includes: [deepEqual.js] | ||
| flags: [noStrict] | ||
| ---*/ | ||
| // Copyright (C) 2015 the V8 project authors. All rights reserved. | ||
| assert.sameValue( | ||
| typeof Map.prototype.getOrInsert, | ||
| 'function', | ||
| '`typeof Map.prototype.getOrInsert` is `function`' | ||
| ); | ||
|
|
||
|
|
||
| assert.deepEqual(Object.getOwnPropertyDescriptor(Map.prototype, "getOrInsert"), { | ||
| value: Map.prototype.getOrInsert, | ||
| writable: true, | ||
| enumerable: false, | ||
| configurable: true | ||
| }); | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Copyright (C) 2024 Jonas Haukenes. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: proposal-upsert | ||
| description: | | ||
| Map.prototype.getOrInsert.length value and descriptor. | ||
| info: | | ||
| Map.prototype.getOrInsert ( key , value ) | ||
|
|
||
| 17 ECMAScript Standard Built-in Objects | ||
| includes: [deepEqual.js] | ||
| flags: [noStrict] | ||
| ---*/ | ||
| // Copyright (C) 2015 the V8 project authors. All rights reserved. | ||
| assert.deepEqual(Object.getOwnPropertyDescriptor(Map.prototype.getOrInsert, "length"), { | ||
| value: 2, | ||
| writable: false, | ||
| enumerable: false, | ||
| configurable: true | ||
| }); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Copyright (C) 2024 Jonas Haukenes. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: proposal-upsert | ||
| description: | | ||
| Map.prototype.getOrInsert.name value and descriptor. | ||
| info: | | ||
| Map.prototype.getOrInsert ( key , value ) | ||
|
|
||
| 17 ECMAScript Standard Built-in Objects | ||
| includes: [deepEqual.js] | ||
| flags: [noStrict] | ||
| ---*/ | ||
| // Copyright (C) 2015 the V8 project authors. All rights reserved. | ||
| assert.deepEqual(Object.getOwnPropertyDescriptor(Map.prototype.getOrInsert, "name"), { | ||
| value: "getOrInsert", | ||
| writable: false, | ||
| enumerable: false, | ||
| configurable: true | ||
| }); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright (C) 2024 Jonas Haukenes. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
|
|
||
| /*--- | ||
| esid: proposal-upsert | ||
| description: | | ||
| Map.prototype.getOrInsert does not implement [[Construct]], is not new-able | ||
| info: | | ||
| ECMAScript Function Objects | ||
|
|
||
| Built-in function objects that are not identified as constructors do not | ||
| implement the [[Construct]] internal method unless otherwise specified in | ||
| the description of a particular function. | ||
|
|
||
| sec-evaluatenew | ||
|
|
||
| ... | ||
| 7. If IsConstructor(constructor) is false, throw a TypeError exception. | ||
| ... | ||
| includes: [isConstructor.js] | ||
| features: [Map, Reflect.construct, arrow-function] | ||
| flags: [noStrict] | ||
| ---*/ | ||
| // Copyright (C) 2020 Rick Waldron. All rights reserved. | ||
| assert.sameValue(isConstructor(Map.prototype.getOrInsert), false, 'isConstructor(Map.prototype.getOrInsert) must return false'); | ||
|
|
||
| assertThrowsInstanceOf(() => { | ||
| let m = new Map(); new m.getOrInsert(); | ||
| }, TypeError); | ||
|
|
42 changes: 42 additions & 0 deletions
42
...staging/upsert/Map/getOrInsert/returns-value-if-key-is-not-present-different-key-types.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright (C) 2024 Jonas Haukenes. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: proposal-upsert | ||
| description: | | ||
| Returns the value from the specified key on different types, when key not present. | ||
| info: | | ||
| Map.prototype.getOrInsert ( key , value ) | ||
|
|
||
| ... | ||
| 5. Let p be the Record { [[Key]]: key, [[Value]]: value }. | ||
| 6. Append p to M.[[MapData]]. | ||
| 7. Return p.[[Value]]. | ||
| ... | ||
| features: | ||
| - Symbol | ||
| flags: [noStrict] | ||
| ---*/ | ||
| // Copyright (C) 2015 the V8 project authors. All rights reserved. | ||
| var map = new Map(); | ||
|
|
||
| assert.sameValue(map.getOrInsert('bar', 0), 0); | ||
|
|
||
| assert.sameValue(map.getOrInsert(1, 42), 42); | ||
|
|
||
| assert.sameValue(map.getOrInsert(NaN, 1), 1); | ||
|
|
||
| var item = {}; | ||
| assert.sameValue(map.getOrInsert(item, 2), 2); | ||
|
|
||
| item = []; | ||
| assert.sameValue(map.getOrInsert(item, 3), 3); | ||
|
|
||
| item = Symbol('item'); | ||
| assert.sameValue(map.getOrInsert(item, 4), 4); | ||
|
|
||
| item = null; | ||
| assert.sameValue(map.getOrInsert(item, 5), 5); | ||
|
|
||
| item = undefined; | ||
| assert.sameValue(map.getOrInsert(item, 6), 6); | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.