mirror of
https://github.com/denoland/deno.git
synced 2025-01-22 06:09:25 -05:00
console.assert
should not throw error (#1335)
This commit is contained in:
parent
0bb43ebbfc
commit
769994bd4e
2 changed files with 23 additions and 6 deletions
|
@ -383,11 +383,27 @@ export class Console {
|
||||||
|
|
||||||
/** Writes an error message to stdout if the assertion is `false`. If the
|
/** Writes an error message to stdout if the assertion is `false`. If the
|
||||||
* assertion is `true`, nothing happens.
|
* assertion is `true`, nothing happens.
|
||||||
|
*
|
||||||
|
* ref: https://console.spec.whatwg.org/#assert
|
||||||
*/
|
*/
|
||||||
// tslint:disable-next-line:no-any
|
// tslint:disable-next-line:no-any
|
||||||
assert = (condition: boolean, ...args: any[]): void => {
|
assert = (condition?: boolean, ...args: any[]): void => {
|
||||||
if (!condition) {
|
if (condition) {
|
||||||
throw new Error(`Assertion failed: ${stringifyArgs(args)}`);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (args.length === 0) {
|
||||||
|
this.error("Assertion failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [first, ...rest] = args;
|
||||||
|
|
||||||
|
if (typeof first === "string") {
|
||||||
|
this.error(`Assertion failed: ${first}`, ...rest);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.error(`Assertion failed:`, ...args);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,16 +7,17 @@ function stringify(...args: any[]): string {
|
||||||
return stringifyArgs(args);
|
return stringifyArgs(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
test(function consoleTestAssert() {
|
test(function consoleTestAssertShouldNotThrowError() {
|
||||||
console.assert(true);
|
console.assert(true);
|
||||||
|
|
||||||
let hasThrown = false;
|
let hasThrown = undefined;
|
||||||
try {
|
try {
|
||||||
console.assert(false);
|
console.assert(false);
|
||||||
|
hasThrown = false;
|
||||||
} catch {
|
} catch {
|
||||||
hasThrown = true;
|
hasThrown = true;
|
||||||
}
|
}
|
||||||
assertEqual(hasThrown, true);
|
assertEqual(hasThrown, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test(function consoleTestStringifyComplexObjects() {
|
test(function consoleTestStringifyComplexObjects() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue