5ce26eb91a
Co-authored-by: Peter Thatcher <peter@signal.org> Co-authored-by: Jim Gustafson <jim@signal.org> Co-authored-by: Josh Perez <60019601+josh-signal@users.noreply.github.com>
85 lines
2.6 KiB
TypeScript
85 lines
2.6 KiB
TypeScript
// Copyright 2021-2022 Signal Messenger, LLC
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
import { assert } from 'chai';
|
|
|
|
import { isEqual, remove, toggle } from '../../util/setUtil';
|
|
|
|
describe('set utilities', () => {
|
|
const original = new Set([1, 2, 3]);
|
|
|
|
describe('isEqual', () => {
|
|
it('returns false if the sets are different', () => {
|
|
const sets = [
|
|
new Set([1, 2, 3]),
|
|
new Set([1, 2, 3, 4]),
|
|
new Set([1, 2]),
|
|
new Set([4, 5, 6]),
|
|
];
|
|
|
|
for (const a of sets) {
|
|
for (const b of sets) {
|
|
if (a !== b) {
|
|
assert.isFalse(isEqual(a, b));
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
it('returns true if both arguments are the same set', () => {
|
|
const set = new Set([1, 2, 3]);
|
|
assert.isTrue(isEqual(set, set));
|
|
});
|
|
|
|
it('returns true if the sets have the same values', () => {
|
|
assert.isTrue(isEqual(new Set(), new Set()));
|
|
assert.isTrue(isEqual(new Set([1, 2]), new Set([2, 1])));
|
|
});
|
|
});
|
|
|
|
describe('remove', () => {
|
|
it('accepts zero arguments, returning a new set', () => {
|
|
const result = remove(original);
|
|
assert.deepStrictEqual(result, original);
|
|
assert.notStrictEqual(result, original);
|
|
});
|
|
|
|
it('accepts 1 argument, returning a new set', () => {
|
|
const result = remove(original, 2);
|
|
assert.deepStrictEqual(result, new Set([1, 3]));
|
|
assert.deepStrictEqual(original, new Set([1, 2, 3]));
|
|
});
|
|
|
|
it('accepts multiple arguments, returning a new set', () => {
|
|
const result = remove(original, 1, 2, 99);
|
|
assert.deepStrictEqual(result, new Set([3]));
|
|
assert.deepStrictEqual(original, new Set([1, 2, 3]));
|
|
});
|
|
});
|
|
|
|
describe('toggle', () => {
|
|
it('returns a clone if trying to remove an item that was never there', () => {
|
|
const result = toggle(original, 99, false);
|
|
assert.deepStrictEqual(result, new Set([1, 2, 3]));
|
|
assert.notStrictEqual(result, original);
|
|
});
|
|
|
|
it('returns a clone if trying to add an item that was already there', () => {
|
|
const result = toggle(original, 3, true);
|
|
assert.deepStrictEqual(result, new Set([1, 2, 3]));
|
|
assert.notStrictEqual(result, original);
|
|
});
|
|
|
|
it('can add an item to a set', () => {
|
|
const result = toggle(original, 4, true);
|
|
assert.deepStrictEqual(result, new Set([1, 2, 3, 4]));
|
|
assert.deepStrictEqual(original, new Set([1, 2, 3]));
|
|
});
|
|
|
|
it('can remove an item from a set', () => {
|
|
const result = toggle(original, 2, false);
|
|
assert.deepStrictEqual(result, new Set([1, 3]));
|
|
assert.deepStrictEqual(original, new Set([1, 2, 3]));
|
|
});
|
|
});
|
|
});
|