mirror of
https://github.com/denoland/deno.git
synced 2024-11-23 15:16:54 -05:00
caa383a583
After some discussion it was found that assertEquals is more common
in JS (vs assertEqual, assertEq) and sounds better in the negated form:
assertNotEquals vs assertNE.
Original: 4cf39d4a14
97 lines
2.1 KiB
TypeScript
97 lines
2.1 KiB
TypeScript
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
import { test } from "../testing/mod.ts";
|
|
import { assertEquals } from "../testing/asserts.ts";
|
|
import { LogRecord, Logger } from "./logger.ts";
|
|
import { LogLevel } from "./levels.ts";
|
|
import { BaseHandler } from "./handlers.ts";
|
|
|
|
class TestHandler extends BaseHandler {
|
|
public messages: string[] = [];
|
|
public records: LogRecord[] = [];
|
|
|
|
handle(record: LogRecord): void {
|
|
this.records.push({ ...record, datetime: null });
|
|
super.handle(record);
|
|
}
|
|
|
|
public log(str: string): void {
|
|
this.messages.push(str);
|
|
}
|
|
}
|
|
|
|
test(function simpleLogger() {
|
|
const handler = new TestHandler("DEBUG");
|
|
let logger = new Logger("DEBUG");
|
|
|
|
assertEquals(logger.level, LogLevel.DEBUG);
|
|
assertEquals(logger.levelName, "DEBUG");
|
|
assertEquals(logger.handlers, []);
|
|
|
|
logger = new Logger("DEBUG", [handler]);
|
|
|
|
assertEquals(logger.handlers, [handler]);
|
|
});
|
|
|
|
test(function customHandler() {
|
|
const handler = new TestHandler("DEBUG");
|
|
const logger = new Logger("DEBUG", [handler]);
|
|
|
|
logger.debug("foo", 1, 2);
|
|
|
|
assertEquals(handler.records, [
|
|
{
|
|
msg: "foo",
|
|
args: [1, 2],
|
|
datetime: null,
|
|
level: LogLevel.DEBUG,
|
|
levelName: "DEBUG"
|
|
}
|
|
]);
|
|
|
|
assertEquals(handler.messages, ["DEBUG foo"]);
|
|
});
|
|
|
|
test(function logFunctions() {
|
|
let handler: TestHandler;
|
|
|
|
const doLog = (level: string): void => {
|
|
handler = new TestHandler(level);
|
|
let logger = new Logger(level, [handler]);
|
|
logger.debug("foo");
|
|
logger.info("bar");
|
|
logger.warning("baz");
|
|
logger.error("boo");
|
|
logger.critical("doo");
|
|
};
|
|
|
|
doLog("DEBUG");
|
|
|
|
assertEquals(handler.messages, [
|
|
"DEBUG foo",
|
|
"INFO bar",
|
|
"WARNING baz",
|
|
"ERROR boo",
|
|
"CRITICAL doo"
|
|
]);
|
|
|
|
doLog("INFO");
|
|
|
|
assertEquals(handler.messages, [
|
|
"INFO bar",
|
|
"WARNING baz",
|
|
"ERROR boo",
|
|
"CRITICAL doo"
|
|
]);
|
|
|
|
doLog("WARNING");
|
|
|
|
assertEquals(handler.messages, ["WARNING baz", "ERROR boo", "CRITICAL doo"]);
|
|
|
|
doLog("ERROR");
|
|
|
|
assertEquals(handler.messages, ["ERROR boo", "CRITICAL doo"]);
|
|
|
|
doLog("CRITICAL");
|
|
|
|
assertEquals(handler.messages, ["CRITICAL doo"]);
|
|
});
|