-
Notifications
You must be signed in to change notification settings - Fork 511
More set methods tests #3966
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
More set methods tests #3966
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3314139
add tests for difference, intersection, symmetricDifference
bakkot e28b8e1
add tests for predicates
bakkot 7852211
more tests for ordering of calls
bakkot 32f5024
fix copyrights
bakkot 943ad0e
add missed -0 test
bakkot 32534dc
pass argument to methods, not just this
bakkot bfc02de
assert methods return new objects
bakkot 965899a
typo in filename
bakkot 43b2940
fix parens
bakkot 405bcdd
Update test/built-ins/Set/prototype/difference/add-not-called.js
bakkot 2069de5
fix isSupersetOf/set-like-array.js
bakkot b3d8f0c
add tests for negative zero with isDisjointFrom and isSupersetOf
bakkot 2265898
fix message in isSupersetOf/converts-negative-zero.js
bakkot 30a6a68
address comments from review
bakkot 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
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,27 @@ | ||
| // Copyright (C) 2023 Anthony Frehner and Kevin Gibbons. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: sec-set.prototype.difference | ||
| description: Set.prototype.difference should not call Set.prototype.add | ||
| features: [set-methods] | ||
| includes: [compareArray.js] | ||
| ---*/ | ||
|
|
||
| const s1 = new Set([1, 2]); | ||
| const s2 = new Set([2, 3]); | ||
| const expected = [1]; | ||
|
|
||
| const originalAdd = Set.prototype.add; | ||
| let count = 0; | ||
| Set.prototype.add = function (...rest) { | ||
| count++; | ||
| return originalAdd.apply(this, rest); | ||
| }; | ||
|
|
||
| const combined = s1.difference(s2); | ||
|
|
||
| assert.compareArray([...combined], expected); | ||
| assert.sameValue(combined instanceof Set, true, "The returned object is a Set"); | ||
| assert.sameValue(count, 0, "Add is never called"); | ||
|
|
||
| Set.prototype.add = originalAdd; |
35 changes: 35 additions & 0 deletions
35
test/built-ins/Set/prototype/difference/allows-set-like-class.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,35 @@ | ||
| // Copyright (C) 2023 Anthony Frehner and Kevin Gibbons. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: sec-set.prototype.difference | ||
| description: GetSetRecord allows instances of Set-like classes | ||
| info: | | ||
| 1. If obj is not an Object, throw a TypeError exception. | ||
| 2. Let rawSize be ? Get(obj, "size"). | ||
| ... | ||
| 7. Let has be ? Get(obj, "has"). | ||
| ... | ||
| 9. Let keys be ? Get(obj, "keys"). | ||
| features: [set-methods] | ||
| includes: [compareArray.js] | ||
| ---*/ | ||
|
|
||
| const s1 = new Set([1, 2]); | ||
| const s2 = new class { | ||
| get size() { | ||
| return 2; | ||
| } | ||
| has(v) { | ||
| if (v === 1) return false; | ||
| if (v === 2) return true; | ||
| throw new Test262Error("Set.prototype.difference should only call its argument's has method with contents of this"); | ||
| } | ||
| * keys() { | ||
| throw new Test262Error("Set.prototype.difference should not call its argument's keys iterator when this.size ≤ arg.size"); | ||
| } | ||
| }; | ||
| const expected = [1]; | ||
| const combined = s1.difference(s2); | ||
|
|
||
| assert.compareArray([...combined], expected); | ||
| assert.sameValue(combined instanceof Set, true, "The returned object is a Set"); |
33 changes: 33 additions & 0 deletions
33
test/built-ins/Set/prototype/difference/allows-set-like-object.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,33 @@ | ||
| // Copyright (C) 2023 Anthony Frehner and Kevin Gibbons. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: sec-set.prototype.difference | ||
| description: GetSetRecord allows Set-like objects | ||
| info: | | ||
| 1. If obj is not an Object, throw a TypeError exception. | ||
| 2. Let rawSize be ? Get(obj, "size"). | ||
| ... | ||
| 7. Let has be ? Get(obj, "has"). | ||
| ... | ||
| 9. Let keys be ? Get(obj, "keys"). | ||
| features: [set-methods] | ||
| includes: [compareArray.js] | ||
| ---*/ | ||
|
|
||
| const s1 = new Set([1, 2]); | ||
| const s2 = { | ||
| size: 2, | ||
| has: (v) => { | ||
| if (v === 1) return false; | ||
| if (v === 2) return true; | ||
| throw new Test262Error("Set.prototype.difference should only call its argument's has method with contents of this"); | ||
| }, | ||
| keys: function* keys() { | ||
| throw new Test262Error("Set.prototype.difference should not call its argument's keys iterator when this.size ≤ arg.size"); | ||
| }, | ||
| }; | ||
| const expected = [1]; | ||
| const combined = s1.difference(s2); | ||
|
|
||
| assert.compareArray([...combined], expected); | ||
| assert.sameValue(combined instanceof Set, true, "The returned object is a Set"); |
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,17 @@ | ||
| // Copyright (C) 2023 Anthony Frehner and Kevin Gibbons. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: sec-set.prototype.difference | ||
| description: Set.prototype.difference doesn't work with arrays | ||
| features: [set-methods] | ||
| ---*/ | ||
|
|
||
| const s1 = new Set([1, 2]); | ||
| const s2 = [3]; | ||
| assert.throws( | ||
| TypeError, | ||
| function () { | ||
| s1.difference(s2); | ||
| }, | ||
| "Throws an error when an array is used" | ||
| ); |
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,25 @@ | ||
| // Copyright (C) 2023 Anthony Frehner and Kevin Gibbons. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: sec-set.prototype.difference | ||
| description: Tests that Set.prototype.difference meets the requirements for built-in objects | ||
| features: [set-methods] | ||
| ---*/ | ||
|
|
||
| assert.sameValue( | ||
| Object.isExtensible(Set.prototype.difference), | ||
| true, | ||
| "Built-in objects must be extensible." | ||
| ); | ||
|
|
||
| assert.sameValue( | ||
| Object.prototype.toString.call(Set.prototype.difference), | ||
| "[object Function]", | ||
| "Object.prototype.toString" | ||
| ); | ||
|
|
||
| assert.sameValue( | ||
| Object.getPrototypeOf(Set.prototype.difference), | ||
| Function.prototype, | ||
| "prototype" | ||
| ); |
66 changes: 66 additions & 0 deletions
66
test/built-ins/Set/prototype/difference/called-with-object.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,66 @@ | ||
| // Copyright (C) 2023 Anthony Frehner and Kevin Gibbons. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: sec-getsetrecord | ||
| description: GetSetRecord throws if obj is not an object | ||
| info: | | ||
| 1. If obj is not an Object, throw a TypeError exception. | ||
| features: [set-methods] | ||
| ---*/ | ||
|
|
||
| let s1 = new Set([1]); | ||
| assert.throws( | ||
| TypeError, | ||
| function () { | ||
| s1.difference(1); | ||
| }, | ||
| "number" | ||
| ); | ||
|
|
||
| assert.throws( | ||
| TypeError, | ||
| function () { | ||
| s1.difference(""); | ||
| }, | ||
| "string" | ||
| ); | ||
|
|
||
| assert.throws( | ||
| TypeError, | ||
| function () { | ||
| s1.difference(1n); | ||
| }, | ||
| "bigint" | ||
| ); | ||
|
|
||
| assert.throws( | ||
| TypeError, | ||
| function () { | ||
| s1.difference(false); | ||
| }, | ||
| "boolean" | ||
| ); | ||
|
|
||
| assert.throws( | ||
| TypeError, | ||
| function () { | ||
| s1.difference(undefined); | ||
| }, | ||
| "undefined" | ||
| ); | ||
|
|
||
| assert.throws( | ||
| TypeError, | ||
| function () { | ||
| s1.difference(null); | ||
| }, | ||
| "null" | ||
| ); | ||
|
|
||
| assert.throws( | ||
| TypeError, | ||
| function () { | ||
| s1.difference(Symbol("test")); | ||
| }, | ||
| "symbol" | ||
| ); |
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,19 @@ | ||
| // Copyright (C) 2023 Anthony Frehner and Kevin Gibbons. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: sec-set.prototype.difference | ||
| description: Set.prototype.difference combines with Map | ||
| features: [set-methods] | ||
| includes: [compareArray.js] | ||
| ---*/ | ||
|
|
||
| const s1 = new Set([1, 2]); | ||
| const m1 = new Map([ | ||
| [2, "two"], | ||
| [3, "three"], | ||
| ]); | ||
| const expected = [1]; | ||
| const combined = s1.difference(m1); | ||
|
|
||
| assert.compareArray([...combined], expected); | ||
| assert.sameValue(combined instanceof Set, true, "The returned object is a Set"); |
32 changes: 32 additions & 0 deletions
32
test/built-ins/Set/prototype/difference/combines-empty-sets.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,32 @@ | ||
| // Copyright (C) 2023 Anthony Frehner and Kevin Gibbons. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: sec-set.prototype.difference | ||
| description: Set.prototype.difference can combine empty Sets | ||
| features: [set-methods] | ||
| includes: [compareArray.js] | ||
| ---*/ | ||
|
|
||
| const s1 = new Set([]); | ||
| const s2 = new Set([1, 2]); | ||
| let expected = []; | ||
| let combined = s1.difference(s2); | ||
|
|
||
| assert.compareArray([...combined], expected); | ||
| assert.sameValue(combined instanceof Set, true, "The returned object is a Set"); | ||
|
|
||
| const s3 = new Set([1, 2]); | ||
| const s4 = new Set([]); | ||
| expected = [1, 2]; | ||
| combined = s3.difference(s4); | ||
|
|
||
| assert.compareArray([...combined], expected); | ||
| assert.sameValue(combined instanceof Set, true, "The returned object is a Set"); | ||
|
|
||
| const s5 = new Set([]); | ||
| const s6 = new Set([]); | ||
| expected = []; | ||
| combined = s5.difference(s6); | ||
|
|
||
| assert.compareArray([...combined], expected); | ||
| assert.sameValue(combined instanceof Set, true, "The returned object is a Set"); |
16 changes: 16 additions & 0 deletions
16
test/built-ins/Set/prototype/difference/combines-itself.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,16 @@ | ||
| // Copyright (C) 2023 Anthony Frehner and Kevin Gibbons. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: sec-set.prototype.difference | ||
| description: Set.prototype.difference is successful when called on itself | ||
| features: [set-methods] | ||
| includes: [compareArray.js] | ||
| ---*/ | ||
|
|
||
| const s1 = new Set([1, 2]); | ||
| const expected = []; | ||
| const combined = s1.difference(s1); | ||
|
|
||
| assert.compareArray([...combined], expected); | ||
| assert.sameValue(combined instanceof Set, true, "The returned object is a Set"); | ||
| assert.sameValue(combined === s1, false, "The returned object is a new object"); |
18 changes: 18 additions & 0 deletions
18
test/built-ins/Set/prototype/difference/combines-same-sets.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,18 @@ | ||
| // Copyright (C) 2023 Anthony Frehner and Kevin Gibbons. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: sec-set.prototype.difference | ||
| description: Set.prototype.difference can combine Sets that have the same content | ||
| features: [set-methods] | ||
| includes: [compareArray.js] | ||
| ---*/ | ||
|
|
||
| const s1 = new Set([1, 2]); | ||
| const s2 = new Set([1, 2]); | ||
| const expected = []; | ||
| const combined = s1.difference(s2); | ||
|
|
||
| assert.compareArray([...combined], expected); | ||
| assert.sameValue(combined instanceof Set, true, "The returned object is a Set"); | ||
| assert.sameValue(combined === s1, false, "The returned object is a new object"); | ||
| assert.sameValue(combined === s2, false, "The returned object is a new object"); |
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,16 @@ | ||
| // Copyright (C) 2023 Anthony Frehner and Kevin Gibbons. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: sec-set.prototype.difference | ||
| description: Set.prototype.difference combines Sets | ||
| features: [set-methods] | ||
| includes: [compareArray.js] | ||
| ---*/ | ||
|
|
||
| const s1 = new Set([1, 2]); | ||
| const s2 = new Set([2, 3]); | ||
| const expected = [1]; | ||
| const combined = s1.difference(s2); | ||
|
|
||
| assert.compareArray([...combined], expected); | ||
| assert.sameValue(combined instanceof Set, true, "The returned object is a Set"); |
28 changes: 28 additions & 0 deletions
28
test/built-ins/Set/prototype/difference/converts-negative-zero.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) 2023 Anthony Frehner and Kevin Gibbons. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: sec-set.prototype.difference | ||
| description: Set.prototype.difference converts -0𝔽 to +0𝔽 | ||
| info: | | ||
| 7.b.ii If nextValue is -0𝔽, set nextValue to +0𝔽. | ||
| features: [set-methods] | ||
| includes: [compareArray.js] | ||
| ---*/ | ||
|
|
||
| const setlikeWithMinusZero = { | ||
| size: 1, | ||
| has: function () { | ||
| throw new Test262Error("Set.prototype.difference should not call its argument's has method when this.size > arg.size"); | ||
| }, | ||
| keys: function () { | ||
| // we use an array here because the Set constructor would normalize away -0 | ||
| return [-0].values(); | ||
| }, | ||
| }; | ||
|
|
||
| const s1 = new Set([+0, 1]); | ||
| let expected = [1]; | ||
| let combined = s1.difference(setlikeWithMinusZero); | ||
|
|
||
| assert.compareArray([...combined], expected); | ||
| assert.sameValue(combined instanceof Set, true, "The returned object is a Set"); | ||
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) 2023 Anthony Frehner and Kevin Gibbons. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| esid: sec-set.prototype.difference | ||
| description: Set.prototype.difference properties | ||
| includes: [propertyHelper.js] | ||
| features: [set-methods] | ||
| ---*/ | ||
|
|
||
| assert.sameValue( | ||
| typeof Set.prototype.difference, | ||
| "function", | ||
| "`typeof Set.prototype.difference` is `'function'`" | ||
| ); | ||
|
|
||
| verifyProperty(Set.prototype, "difference", { | ||
| enumerable: false, | ||
| writable: true, | ||
| configurable: true, | ||
| }); |
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.