-
Notifications
You must be signed in to change notification settings - Fork 11
/
level_test.go
57 lines (42 loc) · 1.12 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
// SPDX-FileCopyrightText: 2014-2024 caixw
//
// SPDX-License-Identifier: MIT
package logs
import (
"encoding"
"encoding/json"
"fmt"
"testing"
"github.com/issue9/assert/v4"
)
var (
testLevel = LevelInfo
_ encoding.TextMarshaler = LevelDebug
_ encoding.TextUnmarshaler = &testLevel
_ fmt.Stringer = LevelError
)
func TestIsValidLevel(t *testing.T) {
a := assert.New(t, false)
a.False(IsValidLevel(-1)).
False(IsValidLevel(LevelFatal + 1)).
True(IsValidLevel(LevelError))
}
func TestParseLevel(t *testing.T) {
a := assert.New(t, false)
lv, err := ParseLevel("INFO")
a.NotError(err).Equal(lv, LevelInfo)
lv, err = ParseLevel("erro")
a.NotError(err).Equal(lv, LevelError)
lv, err = ParseLevel("not-exists")
a.ErrorString(err, "无效的值").Equal(lv, -1)
lv, err = ParseLevel(levelStrings[-1])
a.ErrorString(err, "无效的值").Equal(lv, -1)
}
func TestLevel_UnmarshalText(t *testing.T) {
a := assert.New(t, false)
l := LevelWarn
a.NotError(json.Unmarshal([]byte(`"inFo"`), &l))
a.Equal(l, LevelInfo)
l = LevelWarn
a.ErrorString(json.Unmarshal([]byte(`"not-exists"`), &l), "无效的值")
}