-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add t.unorderedEqual()
assertion
#3234
base: main
Are you sure you want to change the base?
Changes from 2 commits
4fc75ca
a39dab0
af206f6
f0b4b80
b18c69d
0f51da4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ import isPromise from 'is-promise'; | |||||
import concordanceOptions from './concordance-options.js'; | ||||||
import {CIRCULAR_SELECTOR, isLikeSelector, selectComparable} from './like-selector.js'; | ||||||
import {SnapshotError, VersionMismatchError} from './snapshot-manager.js'; | ||||||
import {checkValueForUnorderedEqual} from './unordered-equal.js'; | ||||||
|
||||||
function formatDescriptorDiff(actualDescriptor, expectedDescriptor, options) { | ||||||
options = {...options, ...concordanceOptions}; | ||||||
|
@@ -958,5 +959,100 @@ export class Assertions { | |||||
pass(); | ||||||
return true; | ||||||
}); | ||||||
|
||||||
this.unorderedEqual = withSkip((actual, expected, message) => { | ||||||
if (!checkMessage('unorderedEqual', message)) { | ||||||
return false; | ||||||
} | ||||||
|
||||||
const actualInfo = checkValueForUnorderedEqual(actual); | ||||||
|
||||||
if (!actualInfo.isValid) { | ||||||
fail(new AssertionError({ | ||||||
assertion: 'unorderedEqual', | ||||||
improperUsage: true, | ||||||
message: '`t.unorderedEqual` only compares Maps, Sets, and arrays', | ||||||
tommy-mitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
values: [formatWithLabel('Called with:', actual)], | ||||||
})); | ||||||
return false; | ||||||
} | ||||||
|
||||||
const expectedInfo = checkValueForUnorderedEqual(expected); | ||||||
|
||||||
if (!expectedInfo.isValid) { | ||||||
fail(new AssertionError({ | ||||||
assertion: 'unorderedEqual', | ||||||
improperUsage: true, | ||||||
message: '`t.unorderedEqual` only compares Maps, Sets, and arrays', | ||||||
tommy-mitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
values: [formatWithLabel('Called with:', expected)], | ||||||
})); | ||||||
return false; | ||||||
} | ||||||
|
||||||
if (actualInfo.size !== expectedInfo.size) { | ||||||
fail(new AssertionError({ | ||||||
assertion: 'unorderedEqual', | ||||||
message: 'size must be equal', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be the Lines 332 to 333 in a39dab0
|
||||||
})); | ||||||
return false; | ||||||
} | ||||||
|
||||||
if (actualInfo.type === 'map') { | ||||||
if (expectedInfo.type !== 'map') { | ||||||
fail(new AssertionError({ | ||||||
assertion: 'unorderedEqual', | ||||||
message: 'both must be maps', | ||||||
})); | ||||||
return false; | ||||||
} | ||||||
|
||||||
const comparedKeysResult = concordance.compare(actual.keys, expected.keys, concordanceOptions); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean:
Suggested change
Of course that returns an iterator, which Concordance may walk over (can't recall), but the keys won't be in the same order. If both values have the same size, then you probably don't need this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agh - the joys of plain JavaScript.
Good catch, I think you're right as well. |
||||||
if (!comparedKeysResult.pass) { | ||||||
fail(new AssertionError({ | ||||||
assertion: 'unorderedEqual', | ||||||
message: 'keys must be equal', | ||||||
})); | ||||||
return false; | ||||||
} | ||||||
|
||||||
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', | ||||||
})); | ||||||
return false; | ||||||
} | ||||||
} | ||||||
|
||||||
pass(); | ||||||
return true; | ||||||
} | ||||||
|
||||||
if (expectedInfo.type === 'map') { | ||||||
fail(new AssertionError({ | ||||||
assertion: 'unorderedEqual', | ||||||
message: 'both must be set-likes', | ||||||
})); | ||||||
return false; | ||||||
} | ||||||
|
||||||
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', | ||||||
})); | ||||||
return false; | ||||||
} | ||||||
} | ||||||
|
||||||
pass(); | ||||||
return true; | ||||||
}); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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 */ | ||
tommy-mitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (type === 'invalid') { | ||
return {isValid: false}; | ||
} | ||
|
||
return { | ||
isValid: true, | ||
type, | ||
size: type === 'array' | ||
? value.length | ||
: value.size, | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,6 +123,9 @@ export type Assertions = { | |
* indicating whether the assertion passed. | ||
*/ | ||
truthy: TruthyAssertion; | ||
|
||
/** Assert that all values in `actual` are in `expected`, returning a boolean indicating whether the assertion passed. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too let's clarify the differences with |
||
unorderedEqual: UnorderedEqualAssertion; | ||
}; | ||
|
||
export type AssertAssertion = { | ||
|
@@ -342,3 +345,18 @@ export type TruthyAssertion = { | |
/** Skip this assertion. */ | ||
skip(actual: any, message?: string): void; | ||
}; | ||
|
||
// TODO: limit to Map | Set | Array | ||
export type UnorderedEqualAssertion = { | ||
/** Assert that all values in `actual` are in `expected`, returning a boolean indicating whether the assertion passed. */ | ||
<Actual, Expected extends Actual>(actual: Actual, expected: Expected, message?: string): actual is Expected; | ||
|
||
/** Assert that all values in `actual` are in `expected`, returning a boolean indicating whether the assertion passed. */ | ||
<Actual extends Expected, Expected>(actual: Actual, expected: Expected, message?: string): expected is Actual; | ||
|
||
/** Assert that all values in `actual` are in `expected`, returning a boolean indicating whether the assertion passed. */ | ||
<Actual, Expected>(actual: Actual, expected: Expected, message?: string): boolean; | ||
|
||
/** Skip this assertion. */ | ||
skip(actual: any, expected: any, message?: string): void; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you clarify the differences with
deepEqual
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts?