File: levels_test.go

package info (click to toggle)
golang-github-bep-logg 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 256 kB
  • sloc: makefile: 9
file content (58 lines) | stat: -rw-r--r-- 1,263 bytes parent folder | download
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
package logg

import (
	"encoding/json"
	"testing"

	qt "github.com/frankban/quicktest"
)

func TestParseLevel(t *testing.T) {
	cases := []struct {
		String string
		Level  Level
	}{
		{"trace", LevelTrace},
		{"debug", LevelDebug},
		{"info", LevelInfo},
		{"warn", LevelWarn},
		{"warning", LevelWarn},
		{"error", LevelError},
	}

	for _, c := range cases {
		t.Run(c.String, func(t *testing.T) {
			l, err := ParseLevel(c.String)
			qt.Assert(t, err, qt.IsNil, qt.Commentf("parse"))
			qt.Assert(t, l, qt.Equals, c.Level)
		})
	}

	t.Run("invalid", func(t *testing.T) {
		l, err := ParseLevel("something")
		qt.Assert(t, err, qt.Equals, ErrInvalidLevel)
		qt.Assert(t, l, qt.Equals, LevelInvalid)
	})
}

func TestLevel_MarshalJSON(t *testing.T) {
	e := Entry{
		Message: "hello",
		Level:   LevelInfo,
	}

	expect := `{"level":"info","timestamp":"0001-01-01T00:00:00Z","message":"hello"}`

	b, err := json.Marshal(e)
	qt.Assert(t, err, qt.IsNil)
	qt.Assert(t, string(b), qt.Equals, expect)
}

func TestLevel_UnmarshalJSON(t *testing.T) {
	s := `{"fields":[],"level":"info","timestamp":"0001-01-01T00:00:00Z","message":"hello"}`
	e := new(Entry)

	err := json.Unmarshal([]byte(s), e)
	qt.Assert(t, err, qt.IsNil)
	qt.Assert(t, e.Level, qt.Equals, LevelInfo)
}