1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 func TestLevelString(t *testing.T) {
14 for _, test := range []struct {
19 {LevelError, "ERROR"},
20 {LevelError + 2, "ERROR+2"},
21 {LevelError - 2, "WARN+2"},
23 {LevelWarn - 1, "INFO+3"},
25 {LevelInfo + 1, "INFO+1"},
26 {LevelInfo - 3, "DEBUG+1"},
27 {LevelDebug, "DEBUG"},
28 {LevelDebug - 2, "DEBUG-2"},
30 got := test.in.String()
32 t.Errorf("%d: got %s, want %s", test.in, got, test.want)
37 func TestLevelVar(t *testing.T) {
39 if got, want := al.Level(), LevelInfo; got != want {
40 t.Errorf("got %v, want %v", got, want)
43 if got, want := al.Level(), LevelWarn; got != want {
44 t.Errorf("got %v, want %v", got, want)
47 if got, want := al.Level(), LevelInfo; got != want {
48 t.Errorf("got %v, want %v", got, want)
53 func TestMarshalJSON(t *testing.T) {
55 data, err := want.MarshalJSON()
60 if err := got.UnmarshalJSON(data); err != nil {
64 t.Errorf("got %s, want %s", got, want)
68 func TestLevelMarshalText(t *testing.T) {
70 data, err := want.MarshalText()
75 if err := got.UnmarshalText(data); err != nil {
79 t.Errorf("got %s, want %s", got, want)
83 func TestLevelParse(t *testing.T) {
84 for _, test := range []struct {
88 {"DEBUG", LevelDebug},
91 {"ERROR", LevelError},
92 {"debug", LevelDebug},
94 {"INFO+87", LevelInfo + 87},
95 {"Error-18", LevelError - 18},
96 {"Error-8", LevelInfo},
99 if err := got.parse(test.in); err != nil {
100 t.Fatalf("%q: %v", test.in, err)
102 if got != test.want {
103 t.Errorf("%q: got %s, want %s", test.in, got, test.want)
108 func TestLevelParseError(t *testing.T) {
109 for _, test := range []struct {
111 want string // error string should contain this
113 {"", "unknown name"},
114 {"dbg", "unknown name"},
115 {"INFO+", "invalid syntax"},
116 {"INFO-", "invalid syntax"},
117 {"ERROR+23x", "invalid syntax"},
120 err := l.parse(test.in)
121 if err == nil || !strings.Contains(err.Error(), test.want) {
122 t.Errorf("%q: got %v, want string containing %q", test.in, err, test.want)
127 func TestLevelFlag(t *testing.T) {
128 fs := flag.NewFlagSet("test", flag.ContinueOnError)
130 fs.TextVar(&lf, "level", lf, "set level")
131 err := fs.Parse([]string{"-level", "WARN+3"})
135 if g, w := lf, LevelWarn+3; g != w {
136 t.Errorf("got %v, want %v", g, w)
140 func TestLevelVarMarshalText(t *testing.T) {
143 data, err := v.MarshalText()
148 if err := v2.UnmarshalText(data); err != nil {
151 if g, w := v2.Level(), LevelWarn; g != w {
152 t.Errorf("got %s, want %s", g, w)
156 func TestLevelVarFlag(t *testing.T) {
157 fs := flag.NewFlagSet("test", flag.ContinueOnError)
160 fs.TextVar(v, "level", v, "set level")
161 err := fs.Parse([]string{"-level", "WARN+3"})
165 if g, w := v.Level(), LevelWarn+3; g != w {
166 t.Errorf("got %v, want %v", g, w)