Browse Source
- add TextUnmarshaler - add TextMarshaler - update json.Marshaler - update json.Unmarshaler - update string name Emergency and Critical - add test json Unmarshaler and Marshaler Reviewed-on: https://gitoa.ru/go-4devs/log/pulls/7 Co-authored-by: andrey <andrey@4devs.io> Co-committed-by: andrey <andrey@4devs.io>pull/8/head
andrey
11 months ago
3 changed files with 99 additions and 24 deletions
@ -0,0 +1,59 @@ |
|||||
|
package level_test |
||||
|
|
||||
|
import ( |
||||
|
"testing" |
||||
|
|
||||
|
"gitoa.ru/go-4devs/log/level" |
||||
|
) |
||||
|
|
||||
|
func TestMarshalJSON(t *testing.T) { |
||||
|
levels := map[level.Level]string{ |
||||
|
level.Emergency: `"emerg"`, |
||||
|
level.Alert: `"alert"`, |
||||
|
level.Critical: `"crit"`, |
||||
|
level.Error: `"error"`, |
||||
|
level.Warning: `"warning"`, |
||||
|
level.Notice: `"notice"`, |
||||
|
level.Info: `"info"`, |
||||
|
level.Debug: `"debug"`, |
||||
|
} |
||||
|
|
||||
|
for level, expect := range levels { |
||||
|
actual, err := level.MarshalJSON() |
||||
|
if err != nil { |
||||
|
t.Errorf("%s got err: %s", level, err) |
||||
|
continue |
||||
|
} |
||||
|
if string(actual) != expect { |
||||
|
t.Errorf("%s got: %s expect: %s", level, actual, expect) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func TestUnmarshalJSON(t *testing.T) { |
||||
|
levels := map[level.Level][]string{ |
||||
|
level.Emergency: {`"emerg"`, `"Emerg"`}, |
||||
|
level.Alert: {`"alert"`, `"ALERT"`}, |
||||
|
level.Critical: {`"crit"`, `"critical"`}, |
||||
|
level.Error: {`"error"`, `"ERR"`}, |
||||
|
level.Warning: {`"warning"`, `"Warning"`}, |
||||
|
level.Notice: {`"notice"`}, |
||||
|
level.Info: {`"info"`}, |
||||
|
level.Debug: {`"debug"`, `"DEBUG"`}, |
||||
|
} |
||||
|
|
||||
|
for expect, actuals := range levels { |
||||
|
for _, actual := range actuals { |
||||
|
var level level.Level |
||||
|
if err := level.UnmarshalJSON([]byte(actual)); err != nil { |
||||
|
t.Errorf("%s got err: %s", level, err) |
||||
|
continue |
||||
|
} |
||||
|
|
||||
|
if !level.Is(expect) { |
||||
|
t.Errorf("%s got: %s expect: %s", actual, level, expect) |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue