-
Notifications
You must be signed in to change notification settings - Fork 1
/
hooks_test.v
62 lines (45 loc) · 1007 Bytes
/
hooks_test.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module treplo
import io
// Output for tests
struct TestOut {
mut:
output string
}
fn (mut to TestOut) write(data []byte) ?int {
to.output += data.bytestr()
return data.len
}
fn (mut to TestOut) clear() {
to.output = ""
}
fn (mut to TestOut) str() string {
return to.output.trim_space()
}
// Test formatter, outputs "$level $message" with newline byte at end
struct SimpleFormatter {}
fn (f SimpleFormatter) format(entry Entry) ?[]byte{
message := entry.message.trim_suffix("\n")
return "${message}".bytes()
}
struct CoolHook {
mut:
out io.Writer
}
fn (hook CoolHook) levels() []Level {
return [.debug, .info, .fatal, .panic]
}
fn (hook CoolHook) fire(entry Entry) ? {
hook.out.write("Nice".bytes()) ?
}
fn test_hooks() {
mut llog := new()
mut out := TestOut{}
llog.set_output(out)
mut formatter := SimpleFormatter{}
llog.set_formatter(formatter)
mut hooks := LevelHooks{}
hooks.add(&CoolHook{out})
llog.replace_hooks(hooks)
llog.println()
assert out.str() == "Nice"
}