fix(testing): support array values in assertObjectMatch (#906)

This commit is contained in:
Simon Lecoq 2021-05-13 10:04:22 +02:00 committed by GitHub
parent 07be23ea9e
commit 71b6522513
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 2 deletions

View File

@ -498,9 +498,24 @@ export function assertObjectMatch(
]
.filter((key) => key in b)
.map((key) => [key, a[key as string]]) as Array<[string, unknown]>;
// Build filtered object and filter recursively on nested objects references
for (const [key, value] of entries) {
if (typeof value === "object") {
// On array references, build a filtered array and filter nested objects inside
if (Array.isArray(value)) {
const subset = (b as loose)[key];
if (Array.isArray(subset)) {
filtered[key] = value
.slice(0, subset.length)
.map((element, index) => {
const subsetElement = subset[index];
if ((typeof subsetElement === "object") && (subsetElement)) {
return filter(element, subsetElement);
}
return element;
});
continue;
}
} // On nested objects references, build a filtered object recursively
else if (typeof value === "object") {
const subset = (b as loose)[key];
if ((typeof subset === "object") && (subset)) {
filtered[key] = filter(value as loose, subset as loose);

View File

@ -321,6 +321,8 @@ Deno.test("testingAssertObjectMatching", function (): void {
bar: boolean;
}
const g: r = { foo: true, bar: false };
const h = { foo: [1, 2, 3], bar: true };
const i = { foo: [a, e], bar: true };
// Simple subset
assertObjectMatch(a, {
@ -367,6 +369,17 @@ Deno.test("testingAssertObjectMatching", function (): void {
assertObjectMatch(f, {
[sym]: true,
});
// Subset with array inside
assertObjectMatch(h, { foo: [] });
assertObjectMatch(h, { foo: [1, 2] });
assertObjectMatch(h, { foo: [1, 2, 3] });
assertObjectMatch(i, { foo: [{ bar: false }] });
assertObjectMatch(i, {
foo: [
{ bar: false },
{ bar: { bar: { bar: { foo: true } } } },
],
});
// Missing key
{
let didThrow;
@ -487,6 +500,33 @@ Deno.test("testingAssertObjectMatching", function (): void {
}
assertEquals(didThrow, true);
}
// Subset with array inside but doesn't match key subset
{
let didThrow;
try {
assertObjectMatch(i, {
foo: [1, 2, 3, 4],
});
didThrow = false;
} catch (e) {
assert(e instanceof AssertionError);
didThrow = true;
}
assertEquals(didThrow, true);
}
{
let didThrow;
try {
assertObjectMatch(i, {
foo: [{ bar: true }, { foo: false }],
});
didThrow = false;
} catch (e) {
assert(e instanceof AssertionError);
didThrow = true;
}
assertEquals(didThrow, true);
}
});
Deno.test("testingAssertsUnimplemented", function (): void {