Skip to content

Commit

Permalink
updates, simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell committed Feb 27, 2024
1 parent b18c69d commit 0f51da4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 47 deletions.
51 changes: 16 additions & 35 deletions lib/assert.js
Expand Up @@ -799,86 +799,67 @@ export class Assertions {
});

this.unorderedEqual = withSkip((actual, expected, message) => {
if (!checkMessage('unorderedEqual', message)) {
return false;
}
assertMessage(message, 't.unorderedEqual()');

const actualInfo = checkValueForUnorderedEqual(actual);
const expectedInfo = checkValueForUnorderedEqual(expected);

if (!actualInfo.isValid || !expectedInfo.isValid) {
fail(new AssertionError({
assertion: 'unorderedEqual',
throw fail(new AssertionError('`t.unorderedEqual()` only compares arrays, maps, and sets', {
assertion: 't.unorderedEqual()',
improperUsage: true,
message: '`t.unorderedEqual` only compares Maps, Sets, and arrays',
values: [
!actualInfo.isValid && formatWithLabel('Called with:', actual),
!expectedInfo.isValid && formatWithLabel('Called with:', expected),
].filter(Boolean),
}));
return false;
}

if (
actualInfo.type !== expectedInfo.type
&& (actualInfo.type === 'map' || expectedInfo.type === 'map')
) {
fail(new AssertionError({
assertion: 'unorderedEqual',
throw fail(new AssertionError('types of actual and expected must be comparable', {
assertion: 't.unorderedEqual()',
improperUsage: true,
message: 'types of actual and expected must be comparable',
}));
return false;
}

if (actualInfo.size !== expectedInfo.size) {
fail(new AssertionError({
assertion: 'unorderedEqual',
message: 'size must be equal',
throw fail(new AssertionError('size must be equal', {
assertion: 't.unorderedEqual()',
}));
return false;
}

if (actualInfo.type === 'map') {
const comparedKeysResult = concordance.compare(actual.keys, expected.keys, concordanceOptions);
if (!comparedKeysResult.pass) {
fail(new AssertionError({
assertion: 'unorderedEqual',
message: 'keys must be equal',
}));
return false;
}
// Keys are unique - if actual and expected are the same size,
// and expected has a value for every key in actual, then the two are equal.

for (const [key, value] of actual.entries()) {
const result = concordance.compare(value, expected.get(key), concordanceOptions);
if (!result.pass) {
fail(new AssertionError({
assertion: 'unorderedEqual',
message: 'all values must be equal - map',
// TODO: allow passing custom messages
throw fail(new AssertionError('all values must be equal - map', {
assertion: 't.unorderedEqual()',
}));
return false;
}
}

pass();
return true;
return pass();
}

const setActual = actualInfo.type === 'set' ? actual : new Set(actual);
const setExpected = expectedInfo.type === 'set' ? expected : new Set(expected);

for (const value of setActual) {
if (!setExpected.has(value)) {
fail(new AssertionError({
assertion: 'unorderedEqual',
message: 'all values must be equal - set',
throw fail(new AssertionError('all values must be equal - array/set', {
assertion: 't.unorderedEqual()',
}));
return false;
}
}

pass();
return true;
return pass();
});
}
}
17 changes: 9 additions & 8 deletions lib/unordered-equal.js
@@ -1,12 +1,13 @@
export const checkValueForUnorderedEqual = value => {
/* eslint-disable indent, operator-linebreak, unicorn/no-nested-ternary */
const type = (
value instanceof Map ? 'map' :
value instanceof Set ? 'set' :
Array.isArray(value) ? 'array' :
'invalid'
);
/* eslint-enable indent, operator-linebreak, unicorn/no-nested-ternary */
let type = 'invalid';

if (value instanceof Map) {
type = 'map';
} else if (value instanceof Set) {
type = 'set';
} else if (Array.isArray(value)) {
type = 'array';
}

if (type === 'invalid') {
return {isValid: false};
Expand Down
8 changes: 4 additions & 4 deletions types/assertions.d.cts
Expand Up @@ -413,7 +413,7 @@ export type UnorderedEqualAssertion = {
* The size of `actual` and `expected` must be equal. For `Map`s, each key-value pair in `actual` must be in
* `expected`, and vice-versa. For `Set`s/arrays, each value in `actual` must be in `expected`.
*
* Returns a boolean indicating whether the assertion passed.
* Returns `true` if the assertion passed and throws otherwise.
*/
<Actual, Expected extends Actual>(actual: Actual, expected: Expected, message?: string): actual is Expected;

Expand All @@ -424,7 +424,7 @@ export type UnorderedEqualAssertion = {
* The size of `actual` and `expected` must be equal. For `Map`s, each key-value pair in `actual` must be in
* `expected`, and vice-versa. For `Set`s/arrays, each value in `actual` must be in `expected`.
*
* Returns a boolean indicating whether the assertion passed.
* Returns `true` if the assertion passed and throws otherwise.
*/
<Actual extends Expected, Expected>(actual: Actual, expected: Expected, message?: string): expected is Actual;

Expand All @@ -435,9 +435,9 @@ export type UnorderedEqualAssertion = {
* The size of `actual` and `expected` must be equal. For `Map`s, each key-value pair in `actual` must be in
* `expected`, and vice-versa. For `Set`s/arrays, each value in `actual` must be in `expected`.
*
* Returns a boolean indicating whether the assertion passed.
* Returns `true` if the assertion passed and throws otherwise.
*/
<Actual, Expected>(actual: Actual, expected: Expected, message?: string): boolean;
<Actual, Expected>(actual: Actual, expected: Expected, message?: string): true;

/** Skip this assertion. */
skip(actual: any, expected: any, message?: string): void;
Expand Down

0 comments on commit 0f51da4

Please sign in to comment.