@assertive-ts/core
Version:
A type-safe fluent assertion library
254 lines • 7.91 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StringAssertion = void 0;
const Assertion_1 = require("./Assertion");
const assert_1 = require("assert");
/**
* Encapsulates assertion methods applicable to values of type string
*/
class StringAssertion extends Assertion_1.Assertion {
constructor(actual) {
super(actual);
}
/**
* Check if the string is empty. That is, when the string does not contain
* any characters.
*
* @example
* ```
* expect("").toBeEmpty();
* ```
*
* @returns the assertion instance
*/
toBeEmpty() {
const error = new assert_1.AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> to be empty`,
});
const invertedError = new assert_1.AssertionError({
actual: this.actual,
message: "Expected the value NOT to be empty",
});
return this.execute({
assertWhen: this.actual === "",
error,
invertedError,
});
}
/**
* Check if the string is blank. That is, when the string consists of one or
* more whitespaces only.
*
* @example
* ```
* expect(" ").toBeBlank();
* ```
*
* @returns the assertion instance
*/
toBeBlank() {
const error = new assert_1.AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> to be blank`,
});
const invertedError = new assert_1.AssertionError({
actual: this.actual,
message: "Expected the value NOT to be blank",
});
return this.execute({
assertWhen: this.actual.trimStart().trimEnd() === "",
error,
invertedError,
});
}
/**
* Check if the string value is equal to another string. The comparison is
* not case sensitive, i.e. it ignores the cases of both string values.
*
* @example
* ```
* expect("hello world").toBeEqualIgnoringCase("HELLO WORLD");
* ```
*
* @returns the assertion instance
*/
toBeEqualIgnoringCase(text) {
const error = new assert_1.AssertionError({
actual: this.actual,
expected: text,
message: "Expected both string to be equal ignoring case",
});
const invertedError = new assert_1.AssertionError({
actual: this.actual,
message: "Expected both strings NOT to be equal ignoring case",
});
return this.execute({
assertWhen: this.actual.toLowerCase() === text.toLowerCase(),
error,
invertedError,
});
}
/**
* A convenience alias of `.toBeEqualIgnoringCase(..)` assertion.
*
* @example
* ```
* expect("hello world").toBeEqualCaseInsensitive("HELLO WORLD");
* ```
*
* @see {@link StringAssertion.toBeEqualIgnoringCase toBeEqualIgnoringCase}
*/
toBeEqualCaseInsensitive(text) {
return this.toBeEqualIgnoringCase(text);
}
/**
* Check if the string value contains the passed string. This check compares
* both strings in a case sensitive fashion.
*
* @example
* ```
* expect("Hello World").toContain("World");
* ```
*
* @param text the text the value should contain
* @returns the assertion instance
*/
toContain(text) {
const error = new assert_1.AssertionError({
actual: this.actual,
expected: text,
message: `Expected <${this.actual}> to contain <${text}>`,
});
const invertedError = new assert_1.AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> NOT to contain <${text}>`,
});
return this.execute({
assertWhen: this.actual.includes(text),
error,
invertedError,
});
}
/**
* Check if the string value contains the passed string. This check compares
* both strings ignoring their cases.
*
* @example
* ```
* expect("HELLO WORLD").toContainIgnoringCase("World");
* ```
*
* @param text the text the value should contain (ignoring case)
* @returns the assertion instance
*/
toContainIgnoringCase(text) {
const error = new assert_1.AssertionError({
actual: this.actual,
expected: text,
message: `Expected <${this.actual}> to contain <${text}> (ignoring case)`,
});
const invertedError = new assert_1.AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> NOT to contain <${text}> (ignoring case)`,
});
return this.execute({
assertWhen: this.actual.toLowerCase().includes(text.toLowerCase()),
error,
invertedError,
});
}
/**
* A convenience alias of `.toContainIgnoringCase(..)` assertion.
*
* @example
* ```
* expect("HELLO WORLD").toContainCaseInsensitive("World");
* ```
*
* @see {@link StringAssertion.toContainIgnoringCase toContainIgnoringCase}
*/
toContainCaseInsensitive(text) {
return this.toContainIgnoringCase(text);
}
/**
* Check if the string value starts with the passed string
*
* @example
* ```
* expect("Lorem Ipsum").toStartWith("Lorem");
* ```
*
* @param text the text he value should start with
* @returns the assertion instance
*/
toStartWith(text) {
const error = new assert_1.AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> to start with <${text}>`,
});
const invertedError = new assert_1.AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> NOT to start with <${text}>`,
});
return this.execute({
assertWhen: this.actual.startsWith(text),
error,
invertedError,
});
}
/**
* Check if the string value ends with the passed string
*
* @example
* ```
* expect("Lorem Ipsum").toEndWith("Ipsum");
* ```
*
* @param text the text he value should end with
* @returns the assertion instance
*/
toEndWith(text) {
const error = new assert_1.AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> to end with <${text}>`,
});
const invertedError = new assert_1.AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> NOT to end with <${text}>`,
});
return this.execute({
assertWhen: this.actual.endsWith(text),
error,
invertedError,
});
}
/**
* Check if the string value matches a regular expression.
*
* @example
* ```
* expect("www.helloworld.com").toMatchRegex(/www\..*\.com/);
* ```
*
* @param regex the regular expression to match
* @returns the assertion instance
*/
toMatchRegex(regex) {
const error = new assert_1.AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> to match the regular expression <${regex.source}>`,
});
const invertedError = new assert_1.AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> NOT to match the regular expression <${regex.source}>`,
});
return this.execute({
assertWhen: regex.test(this.actual),
error,
invertedError,
});
}
}
exports.StringAssertion = StringAssertion;
//# sourceMappingURL=StringAssertion.js.map