Repository navigation
Expand file tree
/
Copy pathfields_test.go
More file actions
101 lines (85 loc) · 2.27 KB
/
Copy pathfields_test.go
File metadata and controls
101 lines (85 loc) · 2.27 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package logg
import (
"bytes"
stdjson "encoding/json"
"strings"
"testing"
)
func TestEntry_Pretty(t *testing.T) {
buf := new(bytes.Buffer)
l := New(buf)
l.SetFlags(0)
l.ToggleColor(false)
l.With(F("user", "bob"), F("count", 42)).Info("logged in")
got := strings.TrimRight(buf.String(), "\n")
want := "INF logged in user=bob count=42"
if got != want {
t.Errorf("pretty entry: want %q, got %q", want, got)
}
}
func TestEntry_PrettyQuoting(t *testing.T) {
buf := new(bytes.Buffer)
l := New(buf)
l.SetFlags(0)
l.ToggleColor(false)
l.With(F("msg", "has spaces"), F("empty", "")).Warn("x")
got := strings.TrimRight(buf.String(), "\n")
want := `WRN x msg="has spaces" empty=""`
if got != want {
t.Errorf("pretty quoting: want %q, got %q", want, got)
}
}
func TestEntry_JSON(t *testing.T) {
buf := new(bytes.Buffer)
l := New(buf)
l.SetFlags(0)
l.ToggleColor(false)
l.SetFormat(Json)
l.With(F("user", `b"b`), F("count", 42), F("ok", true)).Error("failed")
out := bytes.TrimRight(buf.Bytes(), "\n")
var decoded map[string]interface{}
if err := stdjson.Unmarshal(out, &decoded); err != nil {
t.Fatalf("entry JSON is not valid: %v\noutput: %s", err, out)
}
if decoded["level"] != "ERR" {
t.Errorf("level: got %v", decoded["level"])
}
if decoded["message"] != "failed" {
t.Errorf("message: got %v", decoded["message"])
}
if decoded["user"] != `b"b` {
t.Errorf("user: got %v", decoded["user"])
}
if decoded["count"] != float64(42) {
t.Errorf("count: got %v (%T)", decoded["count"], decoded["count"])
}
if decoded["ok"] != true {
t.Errorf("ok: got %v", decoded["ok"])
}
}
func TestEntry_WithChaining(t *testing.T) {
buf := new(bytes.Buffer)
l := New(buf)
l.SetFlags(0)
l.ToggleColor(false)
base := l.With(F("a", 1))
base.With(F("b", 2)).Info("x")
// base must be unchanged by the chained call.
buf.Reset()
base.Info("y")
got := strings.TrimRight(buf.String(), "\n")
if got != "INF y a=1" {
t.Errorf("chaining mutated base entry: got %q", got)
}
}
func TestEntry_EmptyKeySkipped(t *testing.T) {
buf := new(bytes.Buffer)
l := New(buf)
l.SetFlags(0)
l.ToggleColor(false)
l.With(F("", "ignored"), F("kept", 1)).Info("x")
got := strings.TrimRight(buf.String(), "\n")
if got != "INF x kept=1" {
t.Errorf("empty key not skipped: got %q", got)
}
}