1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

Bind this to console methods (#873)

Fixes #872
This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2018-10-01 09:41:37 -07:00 committed by Ryan Dahl
parent 1fcc11a19d
commit 3a6d4e6260
2 changed files with 28 additions and 10 deletions

View file

@ -181,29 +181,29 @@ export class Console {
constructor(private printFunc: PrintFunc) {}
// tslint:disable-next-line:no-any
log(...args: any[]): void {
log = (...args: any[]): void => {
this.printFunc(stringifyArgs(args));
}
};
debug = this.log;
info = this.log;
// tslint:disable-next-line:no-any
dir(obj: any, options: ConsoleOptions = {}) {
dir = (obj: any, options: ConsoleOptions = {}) => {
this.printFunc(stringifyArgs([obj], options));
}
};
// tslint:disable-next-line:no-any
warn(...args: any[]): void {
warn = (...args: any[]): void => {
this.printFunc(stringifyArgs(args), true);
}
};
error = this.warn;
// tslint:disable-next-line:no-any
assert(condition: boolean, ...args: any[]): void {
assert = (condition: boolean, ...args: any[]): void => {
if (!condition) {
throw new Error(`Assertion failed: ${stringifyArgs(args)}`);
}
}
};
}

View file

@ -1,6 +1,6 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { test, assertEqual } from "./test_util.ts";
import { test, assert, assertEqual } from "./test_util.ts";
import { stringifyArgs } from "./console.ts";
// tslint:disable-next-line:no-any
@ -88,7 +88,7 @@ test(function consoleTestStringifyCircular() {
assertEqual(stringify(JSON), "{}");
assertEqual(
stringify(console),
"Console { printFunc: [Function], debug: [Function: log], info: [Function: log], error: [Function: warn] }"
"Console { printFunc: [Function], log: [Function], debug: [Function], info: [Function], dir: [Function], warn: [Function], error: [Function], assert: [Function] }"
);
});
@ -122,3 +122,21 @@ test(function consoleTestError() {
assertEqual(stringify(e).split("\n")[0], "MyError: This is an error");
}
});
// Test bound this issue
test(function consoleDetachedLog() {
const log = console.log;
const dir = console.dir;
const debug = console.debug;
const info = console.info;
const warn = console.warn;
const error = console.error;
const consoleAssert = console.assert;
log("Hello world");
dir("Hello world");
debug("Hello world");
info("Hello world");
warn("Hello world");
error("Hello world");
consoleAssert(true);
});