fix(assertion-matchers): Refactor matcher implementations to consistently use customAssertion for improved consistency and clarity.

This commit is contained in:
2025-04-28 19:58:32 +00:00
parent 4eac4544a5
commit 91a3dc43d3
11 changed files with 265 additions and 43 deletions

View File

@ -1,4 +1,5 @@
import { Assertion } from '../smartexpect.classes.assertion.js';
import * as plugins from '../plugins.js';
/**
* Namespace for array-specific matchers
@ -7,38 +8,65 @@ export class ArrayMatchers<T> {
constructor(private assertion: Assertion<T[]>) {}
toBeArray() {
return this.assertion.toBeArray();
return this.assertion.customAssertion(
(value) => Array.isArray(value),
`Expected value to be array`
);
}
toHaveLength(length: number) {
return this.assertion.toHaveLength(length);
return this.assertion.customAssertion(
(value) => (value as T[]).length === length,
`Expected array to have length ${length}`
);
}
toContain(item: T) {
return this.assertion.toContain(item);
return this.assertion.customAssertion(
(value) => (value as T[]).includes(item),
`Expected array to contain ${JSON.stringify(item)}`
);
}
toContainEqual(item: T) {
return this.assertion.toContainEqual(item);
return this.assertion.customAssertion(
(value) => (value as T[]).some((e) => plugins.fastDeepEqual(e, item)),
`Expected array to contain equal to ${JSON.stringify(item)}`
);
}
toContainAll(items: T[]) {
return this.assertion.toContainAll(items);
return this.assertion.customAssertion(
(value) => items.every((i) => (value as T[]).includes(i)),
`Expected array to contain all ${JSON.stringify(items)}`
);
}
toExclude(item: T) {
return this.assertion.toExclude(item);
return this.assertion.customAssertion(
(value) => !(value as T[]).includes(item),
`Expected array to exclude ${JSON.stringify(item)}`
);
}
toBeEmptyArray() {
return this.assertion.toBeEmptyArray();
return this.assertion.customAssertion(
(value) => Array.isArray(value) && (value as T[]).length === 0,
`Expected array to be empty`
);
}
toHaveLengthGreaterThan(length: number) {
return this.assertion.toHaveLengthGreaterThan(length);
return this.assertion.customAssertion(
(value) => (value as T[]).length > length,
`Expected array to have length greater than ${length}`
);
}
toHaveLengthLessThan(length: number) {
return this.assertion.toHaveLengthLessThan(length);
return this.assertion.customAssertion(
(value) => (value as T[]).length < length,
`Expected array to have length less than ${length}`
);
}
}