| 1 | package logg |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | |
| 7 | qt "github.com/frankban/quicktest" |
| 8 | ) |
| 9 | |
| 10 | func TestParseLevel(t *testing.T) { |
| 11 | cases := []struct { |
| 12 | String string |
| 13 | Level Level |
| 14 | }{ |
| 15 | {"trace", LevelTrace}, |
| 16 | {"debug", LevelDebug}, |
| 17 | {"info", LevelInfo}, |
| 18 | {"warn", LevelWarn}, |
| 19 | {"warning", LevelWarn}, |
| 20 | {"error", LevelError}, |
| 21 | } |
| 22 | |
| 23 | for _, c := range cases { |
| 24 | t.Run(c.String, func(t *testing.T) { |
| 25 | l, err := ParseLevel(c.String) |
| 26 | qt.Assert(t, err, qt.IsNil, qt.Commentf("parse")) |
| 27 | qt.Assert(t, l, qt.Equals, c.Level) |
| 28 | }) |
| 29 | } |
| 30 | |
| 31 | t.Run("invalid", func(t *testing.T) { |
| 32 | l, err := ParseLevel("something") |
| 33 | qt.Assert(t, err, qt.Equals, ErrInvalidLevel) |
| 34 | qt.Assert(t, l, qt.Equals, LevelInvalid) |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | func TestLevel_MarshalJSON(t *testing.T) { |
| 39 | e := Entry{ |
| 40 | Message: "hello", |
| 41 | Level: LevelInfo, |
| 42 | } |
| 43 | |
| 44 | expect := `{"level":"info","timestamp":"0001-01-01T00:00:00Z","message":"hello"}` |
| 45 | |
| 46 | b, err := json.Marshal(e) |
| 47 | qt.Assert(t, err, qt.IsNil) |
| 48 | qt.Assert(t, string(b), qt.Equals, expect) |
| 49 | } |
| 50 | |
| 51 | func TestLevel_UnmarshalJSON(t *testing.T) { |
| 52 | s := `{"fields":[],"level":"info","timestamp":"0001-01-01T00:00:00Z","message":"hello"}` |
| 53 | e := new(Entry) |
| 54 | |
| 55 | err := json.Unmarshal([]byte(s), e) |
| 56 | qt.Assert(t, err, qt.IsNil) |
| 57 | qt.Assert(t, e.Level, qt.Equals, LevelInfo) |
| 58 | } |