72 lines
1.9 KiB
TypeScript

import { Assertion } from '../smartexpect.classes.assertion.js';
import * as plugins from '../plugins.js';
/**
* Namespace for array-specific matchers
*/
export class ArrayMatchers<T> {
constructor(private assertion: Assertion<T[]>) {}
toBeArray() {
return this.assertion.customAssertion(
(value) => Array.isArray(value),
`Expected value to be array`
);
}
toHaveLength(length: number) {
return this.assertion.customAssertion(
(value) => (value as T[]).length === length,
`Expected array to have length ${length}`
);
}
toContain(item: T) {
return this.assertion.customAssertion(
(value) => (value as T[]).includes(item),
`Expected array to contain ${JSON.stringify(item)}`
);
}
toContainEqual(item: T) {
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.customAssertion(
(value) => items.every((i) => (value as T[]).includes(i)),
`Expected array to contain all ${JSON.stringify(items)}`
);
}
toExclude(item: T) {
return this.assertion.customAssertion(
(value) => !(value as T[]).includes(item),
`Expected array to exclude ${JSON.stringify(item)}`
);
}
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.customAssertion(
(value) => (value as T[]).length > length,
`Expected array to have length greater than ${length}`
);
}
toHaveLengthLessThan(length: number) {
return this.assertion.customAssertion(
(value) => (value as T[]).length < length,
`Expected array to have length less than ${length}`
);
}
}