forked from op/go-logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
level_test.go
76 lines (66 loc) · 1.62 KB
/
level_test.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2013, Örjan Persson. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package logging
import "testing"
func TestLevelString(t *testing.T) {
// Make sure all levels can be converted from string -> constant -> string
for _, name := range levelNames {
level, err := LogLevel(name)
if err != nil {
t.Errorf("failed to get level: %v", err)
continue
}
if level.String() != name {
t.Errorf("invalid level conversion: %v != %v", level, name)
}
}
}
func TestLevelLogLevel(t *testing.T) {
tests := []struct {
expected Level
level string
}{
{-1, "bla"},
{INFO, "iNfO"},
{ERROR, "error"},
{WARNING, "warninG"},
}
for _, test := range tests {
level, err := LogLevel(test.level)
if err != nil {
if test.expected == -1 {
continue
} else {
t.Errorf("failed to convert %s: %s", test.level, err)
}
}
if test.expected != level {
t.Errorf("failed to convert %s to level: %s != %s", test.level, test.expected, level)
}
}
}
func TestLevelModuleLevel(t *testing.T) {
backend := NewMemoryBackend(128)
leveled := AddModuleLevel(backend)
leveled.SetLevel(NOTICE, "")
leveled.SetLevel(ERROR, "foo")
leveled.SetLevel(INFO, "foo.bar")
leveled.SetLevel(WARNING, "bar")
expected := []struct {
level Level
module string
}{
{NOTICE, ""},
{NOTICE, "something"},
{ERROR, "foo"},
{INFO, "foo.bar"},
{WARNING, "bar"},
}
for _, e := range expected {
actual := leveled.GetLevel(e.module)
if e.level != actual {
t.Errorf("unexpected level in %s: %s != %s", e.module, e.level, actual)
}
}
}