60 lines
1.6 KiB
TypeScript
Raw Normal View History

import { Assertion } from '../smartexpect.classes.assertion.js';
import type { TExecutionType } from '../types.js';
/**
* Namespace for string-specific matchers
*/
export class StringMatchers<M extends TExecutionType> {
constructor(private assertion: Assertion<string, M>) {}
toStartWith(prefix: string) {
return this.assertion.customAssertion(
(value) => (value as string).startsWith(prefix),
`Expected string to start with "${prefix}"`
);
}
toEndWith(suffix: string) {
return this.assertion.customAssertion(
(value) => (value as string).endsWith(suffix),
`Expected string to end with "${suffix}"`
);
}
toInclude(substring: string) {
return this.assertion.customAssertion(
(value) => (value as string).includes(substring),
`Expected string to include "${substring}"`
);
}
toMatch(regex: RegExp) {
return this.assertion.customAssertion(
(value) => regex.test(value as string),
`Expected string to match ${regex}`
);
}
toBeOneOf(values: string[]) {
return this.assertion.customAssertion(
(value) => (values as string[]).includes(value as string),
`Expected string to be one of ${JSON.stringify(values)}`
);
}
/** Length check for strings */
toHaveLength(length: number) {
return this.assertion.customAssertion(
(value) => (value as string).length === length,
`Expected string to have length ${length}`
);
}
/**
* Alias for empty string check
*/
toBeEmpty() {
return this.assertion.customAssertion(
(value) => (value as string).length === 0,
`Expected string to be empty`
);
}
}