Skip to content

assert.equal: Always print arguments, even when msg is provided #20655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,20 @@ namespace assert {
assert(!expr, msg);
}
export function equal<T>(a: T, b: T, msg?: string): void {
assert(a === b, msg || (() => `Expected to be equal:\nExpected:\n${JSON.stringify(a)}\nActual:\n${JSON.stringify(b)}`));
assert(a === b, () => `${msg ? msg + "\n" : ""}Expected to be equal:\nExpected:\n${stringify(a)}\nActual:\n${stringify(b)}`);
}
export function notEqual<T>(a: T, b: T, msg = "Expected values to not be equal."): void {
assert(a !== b, msg);
assert(a !== b, () => `${msg ? msg + "\n" : ""}Expected to not be equal:\n${JSON.stringify(a)}`);
}
export function isDefined(x: {} | null | undefined, msg = "Expected value to be defined."): void {
assert(x !== undefined && x !== null, msg);
export function isDefined(x: {} | null | undefined, msg?: string): void {
assert(x !== undefined && x !== null, `${msg ? msg + "\n" : ""}Expected value to be defined.`);
}
export function isUndefined(x: {} | null | undefined, msg = "Expected value to be undefined."): void {
assert(x === undefined, msg);
export function isUndefined(x: {} | null | undefined, msg?: string): void {
assert(x === undefined, () => `${msg ? msg + "\n" : ""}Expected value to be undefined:\n${stringify(x)}`);
}
export function deepEqual<T>(a: T, b: T, msg?: string): void {
assert(isDeepEqual(a, b), msg || (() => `Expected values to be deeply equal:\nExpected:\n${JSON.stringify(a, undefined, 4)}\nActual:\n${JSON.stringify(b, undefined, 4)}`));
assert(isDeepEqual(a, b), (() =>
`${msg ? msg + "\n" : ""}Expected values to be deeply equal:\nExpected:\n${stringify(a)}\nActual:\n${stringify(b)}`));
}
export function lengthOf(a: ReadonlyArray<{}>, length: number, msg = "Expected length to match."): void {
assert(a.length === length, msg);
Expand All @@ -76,6 +77,10 @@ namespace assert {
const bKeys = Object.keys(b).sort();
return aKeys.length === bKeys.length && aKeys.every((key, i) => bKeys[i] === key && isDeepEqual((a as any)[key], (b as any)[key]));
}

function stringify(value: {} | null | undefined): string {
return JSON.stringify(value, undefined, 4);
}
}
declare var __dirname: string; // Node-specific
var global: NodeJS.Global = <any>Function("return this").call(undefined);
Expand Down