-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkv_test.go
76 lines (67 loc) · 1.49 KB
/
kv_test.go
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
package log
import (
"testing"
"time"
)
type foo struct {
I int
S string
R rune
B bool
T time.Time
D time.Duration
foo bool // unexported, so won't be logged
Foo bool `kv:"-"` // "-" means that the field won't be logged
Bar bool `kv:"foobar"` // change the key
}
type bar struct {
Bar int
Foo *foo
}
var f = &foo{
I: 34,
S: "all aboard!",
R: 'b',
T: time.Date(2006, 8, 24, 02, 30, 0, 0, time.UTC),
D: 30 * time.Millisecond}
var b = &bar{Bar: 0, Foo: f}
func TestMarshalStruct(t *testing.T) {
m, err := Marshal(f)
if err != nil {
t.Error("marshal failed", err)
}
ex := "I=34\tS=\"all aboard!\"\tR='b'\tB=false\tD=30ms\tfoobar=false"
if string(m) != ex {
t.Error("Marhshal returned a wrong result")
}
m, err = Marshal(*f)
if err != nil {
t.Error("marshal failed", err)
}
if string(m) != ex {
t.Error("Marhshal returned a wrong result")
}
}
func TestMarshalDeepStruct(t *testing.T) {
m, err := Marshal(b)
if err != nil {
t.Error("marshal failed", err)
}
ex := "Bar=0\tFoo.I=34\tFoo.S=\"all aboard!\"\tFoo.R='b'\tFoo.B=false\tFoo.D=30ms\tFoo.foobar=false"
if string(m) != ex {
t.Error("Marhshal returned a wrong result")
}
}
func TestMarshalMap(t *testing.T) {
m, err := Marshal(map[string]interface{}{
"foo": true,
"rune": 'c',
"bar": time.Date(2006, 8, 24, 02, 30, 0, 0, time.UTC),
})
if err != nil {
t.Error("marshal failed.", err)
}
if string(m) != "foo=true\trune='c'\tbar=2006-08-24T02:30:00Z" {
t.Error("marshalisation is wrong")
}
}