81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
import { Assertion } from '../smartexpect.classes.assertion.js';
|
|
import * as plugins from '../plugins.js';
|
|
import type { TExecutionType } from '../types.js';
|
|
|
|
/**
|
|
* Namespace for array-specific matchers
|
|
*/
|
|
export class ArrayMatchers<T, M extends TExecutionType> {
|
|
constructor(private assertion: Assertion<T[], M>) {}
|
|
|
|
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)),
|
|
(value) =>
|
|
`Expected array to contain equal to ${JSON.stringify(item)}` +
|
|
`\nReceived: ${JSON.stringify(value, null, 2)}`
|
|
);
|
|
}
|
|
|
|
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`
|
|
);
|
|
}
|
|
/**
|
|
* Alias for empty array check
|
|
*/
|
|
toBeEmpty() {
|
|
return this.toBeEmptyArray();
|
|
}
|
|
|
|
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}`
|
|
);
|
|
}
|
|
} |