| @@ -1,86 +1,86 | |||||
| 1 | import { TraceSource, DebugLevel } from "@implab/core/log/TraceSource"; |
|
1 | import { TraceSource, DebugLevel } from "@implab/core/log/TraceSource"; | |
| 2 | import * as tape from "tape"; |
|
2 | import * as tape from "tape"; | |
| 3 | import { TapeWriter, test } from "./TestTraits"; |
|
3 | import { TapeWriter, test } from "./TestTraits"; | |
| 4 | import { MockConsole } from "./mock/MockConsole"; |
|
4 | import { MockConsole } from "./mock/MockConsole"; | |
| 5 | import { ConsoleLog } from "@implab/core/log/writers/ConsoleLog"; |
|
5 | import { ConsoleLogger } from "@implab/core/log/writers/ConsoleLogger"; | |
| 6 | import { ConsoleWriter } from "@implab/core/log/ConsoleWriter"; |
|
6 | import { ConsoleWriter } from "@implab/core/log/ConsoleWriter"; | |
| 7 |
|
7 | |||
| 8 | const sourceId = "test/TraceSourceTests"; |
|
8 | const sourceId = "test/TraceSourceTests"; | |
| 9 |
|
9 | |||
| 10 | tape("trace message", t => { |
|
10 | tape("trace message", t => { | |
| 11 | const trace = TraceSource.get(sourceId); |
|
11 | const trace = TraceSource.get(sourceId); | |
| 12 |
|
12 | |||
| 13 | trace.level = DebugLevel; |
|
13 | trace.level = DebugLevel; | |
| 14 |
|
14 | |||
| 15 | const h = trace.events.on(ev => { |
|
15 | const h = trace.events.on(ev => { | |
| 16 | t.equal(ev.source, trace, "sender should be the current trace source"); |
|
16 | t.equal(ev.source, trace, "sender should be the current trace source"); | |
| 17 | t.equal(ev.level, DebugLevel, "level should be debug level"); |
|
17 | t.equal(ev.level, DebugLevel, "level should be debug level"); | |
| 18 | t.equal(ev.toString(), "Hello, World!", "The message should be a formatted message"); |
|
18 | t.equal(ev.toString(), "Hello, World!", "The message should be a formatted message"); | |
| 19 |
|
19 | |||
| 20 | t.end(); |
|
20 | t.end(); | |
| 21 | }); |
|
21 | }); | |
| 22 |
|
22 | |||
| 23 | trace.debug("Hello, {0}!", "World"); |
|
23 | trace.debug("Hello, {0}!", "World"); | |
| 24 |
|
24 | |||
| 25 | h.destroy(); |
|
25 | h.destroy(); | |
| 26 | }); |
|
26 | }); | |
| 27 |
|
27 | |||
| 28 | tape("trace event", t => { |
|
28 | tape("trace event", t => { | |
| 29 | const trace = TraceSource.get(sourceId); |
|
29 | const trace = TraceSource.get(sourceId); | |
| 30 |
|
30 | |||
| 31 | trace.level = DebugLevel; |
|
31 | trace.level = DebugLevel; | |
| 32 |
|
32 | |||
| 33 | const event = { |
|
33 | const event = { | |
| 34 | name: "custom event" |
|
34 | name: "custom event" | |
| 35 | }; |
|
35 | }; | |
| 36 |
|
36 | |||
| 37 | const h = trace.events.on(ev => { |
|
37 | const h = trace.events.on(ev => { | |
| 38 | t.equal(ev.source, trace, "sender should be the current trace source"); |
|
38 | t.equal(ev.source, trace, "sender should be the current trace source"); | |
| 39 | t.equal(ev.level, DebugLevel, "level should be debug level"); |
|
39 | t.equal(ev.level, DebugLevel, "level should be debug level"); | |
| 40 | t.equal(ev.message, event, "The message should be the specified object"); |
|
40 | t.equal(ev.message, event, "The message should be the specified object"); | |
| 41 |
|
41 | |||
| 42 | t.end(); |
|
42 | t.end(); | |
| 43 | }); |
|
43 | }); | |
| 44 |
|
44 | |||
| 45 | trace.traceEvent(DebugLevel, event); |
|
45 | trace.traceEvent(DebugLevel, event); | |
| 46 |
|
46 | |||
| 47 | h.destroy(); |
|
47 | h.destroy(); | |
| 48 | }); |
|
48 | }); | |
| 49 |
|
49 | |||
| 50 | tape("tape comment writer", async t => { |
|
50 | tape("tape comment writer", async t => { | |
| 51 | const writer = new TapeWriter(t); |
|
51 | const writer = new TapeWriter(t); | |
| 52 |
|
52 | |||
| 53 | TraceSource.on(ts => { |
|
53 | TraceSource.on(ts => { | |
| 54 | writer.writeEvents(ts.events); |
|
54 | writer.writeEvents(ts.events); | |
| 55 | }); |
|
55 | }); | |
| 56 |
|
56 | |||
| 57 | const trace = TraceSource.get(sourceId); |
|
57 | const trace = TraceSource.get(sourceId); | |
| 58 | trace.level = DebugLevel; |
|
58 | trace.level = DebugLevel; | |
| 59 |
|
59 | |||
| 60 | trace.log("Hello, {0}!", "World"); |
|
60 | trace.log("Hello, {0}!", "World"); | |
| 61 | trace.log("Multi\n line"); |
|
61 | trace.log("Multi\n line"); | |
| 62 | trace.warn("Look at me!"); |
|
62 | trace.warn("Look at me!"); | |
| 63 | trace.error("DIE!"); |
|
63 | trace.error("DIE!"); | |
| 64 |
|
64 | |||
| 65 | writer.destroy(); |
|
65 | writer.destroy(); | |
| 66 |
|
66 | |||
| 67 | trace.log("You shouldn't see it!"); |
|
67 | trace.log("You shouldn't see it!"); | |
| 68 |
|
68 | |||
| 69 | t.comment("DONE"); |
|
69 | t.comment("DONE"); | |
| 70 |
|
70 | |||
| 71 | t.end(); |
|
71 | t.end(); | |
| 72 | }); |
|
72 | }); | |
| 73 |
|
73 | |||
| 74 | test("console writer", (t, trace) => { |
|
74 | test("console writer", (t, trace) => { | |
| 75 |
|
75 | |||
| 76 | const mockConsole = new MockConsole(); |
|
76 | const mockConsole = new MockConsole(); | |
| 77 | const writer = new ConsoleWriter(mockConsole); |
|
77 | const writer = new ConsoleWriter(mockConsole); | |
| 78 | const consoleLog = new ConsoleLog(writer); |
|
78 | const consoleLog = new ConsoleLogger(writer); | |
| 79 | consoleLog.writeEvents(trace.events); |
|
79 | consoleLog.writeEvents(trace.events); | |
| 80 |
|
80 | |||
| 81 | trace.log("Hello, world!"); |
|
81 | trace.log("Hello, world!"); | |
| 82 | t.deepEqual(mockConsole.getLine(0), ["console writer: Hello, world!"], "Log one string"); |
|
82 | t.deepEqual(mockConsole.getLine(0), ["console writer: Hello, world!"], "Log one string"); | |
| 83 |
|
83 | |||
| 84 | trace.log({ foo: "bar" }); |
|
84 | trace.log({ foo: "bar" }); | |
| 85 | t.deepEqual(mockConsole.getLine(1), ["console writer: ", { foo: "bar" }], "Log an object"); |
|
85 | t.deepEqual(mockConsole.getLine(1), ["console writer: ", { foo: "bar" }], "Log an object"); | |
| 86 | }); |
|
86 | }); | |
General Comments 0
You need to be logged in to leave comments.
Login now
