-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add more tests to CustomSet; fix issues
- Loading branch information
1 parent
faa1d5b
commit 3e60a81
Showing
2 changed files
with
151 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,28 +1,142 @@ | ||
import { it, expect } from 'vitest' | ||
import { it, expect, describe } from 'vitest' | ||
import { CustomSet } from './custom-set' | ||
|
||
describe('CustomSet', () => { | ||
it('does not contain the same item twice', () => { | ||
const set = new CustomSet([1, 1, 1]) | ||
expect(set.size).toBe(1) | ||
}) | ||
describe.concurrent('CustomSet', () => { | ||
describe.concurrent('sets of primitives', () => { | ||
it('does not contain the same item twice', () => { | ||
const set = new CustomSet([1, 1, 1]) | ||
expect(set.size).toBe(1) | ||
}) | ||
|
||
it('uses the provided equality function', () => { | ||
const set = new CustomSet([1, 2, 3], { | ||
compare: (a, b) => Math.floor(a / 2) === Math.floor(b / 2), | ||
it('uses the provided equality function', () => { | ||
const set = new CustomSet([1, 2, 3], { | ||
compare: (a, b) => Math.floor(a / 2) === Math.floor(b / 2), | ||
}) | ||
expect(set.size).toBe(2) | ||
}) | ||
|
||
it('is subset of another set with more items', () => { | ||
const set = new CustomSet([1, 2]) | ||
const other = new CustomSet([1, 2, 3]) | ||
expect(set.isSubsetOf(other)).toBe(true) | ||
}) | ||
|
||
it('is not subset of another set with less items', () => { | ||
const set = new CustomSet([1, 2, 3]) | ||
const other = new CustomSet([1, 2]) | ||
expect(set.isSubsetOf(other)).toBe(false) | ||
}) | ||
expect(set.size).toBe(2) | ||
}) | ||
|
||
it('is subset of another set with more items', () => { | ||
const set = new CustomSet([1, 2]) | ||
const other = new CustomSet([1, 2, 3]) | ||
expect(set.isSubsetOf(other)).toBe(true) | ||
it('is subset of another identical set', () => { | ||
const set = new CustomSet([1, 2, 3]) | ||
const other = new CustomSet([1, 2, 3]) | ||
expect(set.isSubsetOf(other)).toBe(true) | ||
expect(other.isSubsetOf(set)).toBe(true) | ||
}) | ||
}) | ||
|
||
it('is not subset of another set with less items', () => { | ||
const set = new CustomSet([1, 2, 3]) | ||
const other = new CustomSet([1, 2]) | ||
expect(set.isSubsetOf(other)).toBe(false) | ||
describe.concurrent('sets of reference types', () => { | ||
describe.concurrent('class instances', () => { | ||
class Foo { | ||
constructor(public id: number) {} | ||
} | ||
|
||
it('does not contain the same item twice', () => { | ||
const set = new CustomSet([new Foo(1), new Foo(1), new Foo(1)]) | ||
expect(set.size).toBe(1) | ||
}) | ||
|
||
it('uses the provided equality function', () => { | ||
const set = new CustomSet([new Foo(1), new Foo(2), new Foo(3)], { | ||
compare: (a, b) => Math.floor(a.id / 2) === Math.floor(b.id / 2), | ||
}) | ||
expect(set.size).toBe(2) | ||
}) | ||
|
||
it('is subset of another set with more items', () => { | ||
const set = new CustomSet([new Foo(1), new Foo(2)]) | ||
const other = new CustomSet([new Foo(1), new Foo(2), new Foo(3)]) | ||
expect(set.isSubsetOf(other)).toBe(true) | ||
}) | ||
|
||
it('is not subset of another set with less items', () => { | ||
const set = new CustomSet([new Foo(1), new Foo(2), new Foo(3)]) | ||
const other = new CustomSet([new Foo(1), new Foo(2)]) | ||
expect(set.isSubsetOf(other)).toBe(false) | ||
}) | ||
|
||
it('is subset of another identical set', () => { | ||
const set = new CustomSet([new Foo(1), new Foo(2), new Foo(3)]) | ||
const other = new CustomSet([new Foo(1), new Foo(2), new Foo(3)]) | ||
expect(set.isSubsetOf(other)).toBe(true) | ||
expect(other.isSubsetOf(set)).toBe(true) | ||
}) | ||
}) | ||
|
||
describe.concurrent('plain objects', () => { | ||
it('does not contain the same item twice', () => { | ||
const set = new CustomSet([{ id: 1 }, { id: 1 }, { id: 1 }]) | ||
expect(set.size).toBe(1) | ||
}) | ||
|
||
it('uses the provided equality function', () => { | ||
const set = new CustomSet([{ id: 1 }, { id: 2 }, { id: 3 }], { | ||
compare: (a, b) => Math.floor(a.id / 2) === Math.floor(b.id / 2), | ||
}) | ||
expect(set.size).toBe(2) | ||
}) | ||
|
||
it('is subset of another set with more items', () => { | ||
const set = new CustomSet([{ id: 1 }, { id: 2 }]) | ||
const other = new CustomSet([{ id: 1 }, { id: 2 }, { id: 3 }]) | ||
expect(set.isSubsetOf(other)).toBe(true) | ||
}) | ||
|
||
it('is not subset of another set with less items', () => { | ||
const set = new CustomSet([{ id: 1 }, { id: 2 }, { id: 3 }]) | ||
const other = new CustomSet([{ id: 1 }, { id: 2 }]) | ||
expect(set.isSubsetOf(other)).toBe(false) | ||
}) | ||
|
||
it('is subset of another identical set', () => { | ||
const set = new CustomSet([{ id: 1 }, { id: 2 }, { id: 3 }]) | ||
const other = new CustomSet([{ id: 1 }, { id: 2 }, { id: 3 }]) | ||
expect(set.isSubsetOf(other)).toBe(true) | ||
expect(other.isSubsetOf(set)).toBe(true) | ||
}) | ||
}) | ||
|
||
describe.concurrent('plain objects with some undefined values', () => { | ||
it('does not contain the same item twice', () => { | ||
const set = new CustomSet([{ id: 1 }, { id: 1, foo: undefined }, { id: 1 }]) | ||
expect(set.size).toBe(1) | ||
}) | ||
|
||
it('uses the provided equality function', () => { | ||
const set = new CustomSet([{ id: 1 }, { id: 2, foo: undefined }, { id: 3 }], { | ||
compare: (a, b) => Math.floor(a.id / 2) === Math.floor(b.id / 2), | ||
}) | ||
expect(set.size).toBe(2) | ||
}) | ||
|
||
it('is subset of another set with more items', () => { | ||
const set = new CustomSet([{ id: 1 }, { id: 2 }]) | ||
const other = new CustomSet([{ id: 1, foo: undefined }, { id: 2 }, { id: 3 }]) | ||
expect(set.isSubsetOf(other)).toBe(true) | ||
}) | ||
|
||
it('is not subset of another set with less items', () => { | ||
const set = new CustomSet([{ id: 1 }, { id: 2 }, { id: 3 }]) | ||
const other = new CustomSet([{ id: 1, foo: undefined }, { id: 2 }]) | ||
expect(set.isSubsetOf(other)).toBe(false) | ||
}) | ||
|
||
it('is subset of another identical set', () => { | ||
const set = new CustomSet([{ id: 1 }, { id: 2 }, { id: 3 }]) | ||
const other = new CustomSet([{ id: 1 }, { id: 2, foo: undefined }, { id: 3 }]) | ||
expect(set.isSubsetOf(other)).toBe(true) | ||
expect(other.isSubsetOf(set)).toBe(true) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains 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