-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdecode_test.go
110 lines (93 loc) · 1.89 KB
/
decode_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
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
102
103
104
105
106
107
108
109
110
package logfmt
import (
"reflect"
"testing"
"time"
)
type pair struct {
k, v string
}
type coll struct {
a []pair
}
func (c *coll) HandleLogfmt(key, val []byte) error {
c.a = append(c.a, pair{string(key), string(val)})
return nil
}
func TestDecodeCustom(t *testing.T) {
data := []byte(`a=foo b=10ms c=cat E="123" d foo= emp=`)
g := new(coll)
if err := Unmarshal(data, g); err != nil {
t.Fatal(err)
}
w := []pair{
{"a", "foo"},
{"b", "10ms"},
{"c", "cat"},
{"E", "123"},
{"d", ""},
{"foo", ""},
{"emp", ""},
}
if !reflect.DeepEqual(w, g.a) {
t.Errorf("\nwant %v\n got %v", w, g)
}
}
func TestDecodeDefault(t *testing.T) {
var g struct {
Float float64
NFloat *float64
String string
Int int
D time.Duration
NB *[]byte
Here bool
This int `logfmt:"that"`
}
em, err := NewStructHandler(&g)
if err != nil {
t.Fatal(err)
}
var tests = []struct {
name string
val string
want interface{}
}{
{"float", "3.14", 3.14},
{"nfloat", "123", float64(123)},
{"string", "foobar", "foobar"},
{"inT", "10", 10},
{"d", "1h", 1 * time.Hour},
{"nb", "bytes!", []byte("bytes!")},
{"here", "", true},
{"that", "5", 5},
}
rv := reflect.Indirect(reflect.ValueOf(&g))
for i, test := range tests {
err = em.HandleLogfmt([]byte(test.name), []byte(test.val))
if err != nil {
t.Error(err)
continue
}
fv := reflect.Indirect(rv.Field(i))
if !fv.IsValid() {
ft := rv.Type().Field(i)
t.Errorf("%s is invalid", ft.Name)
continue
}
gv := fv.Interface()
if !reflect.DeepEqual(gv, test.want) {
t.Errorf("want %T %#v, got %T %#v", test.want, test.want, gv, gv)
}
}
if g.Float != 3.14 {
t.Errorf("want %v, got %v", 3.14, g.Float)
}
err = em.HandleLogfmt([]byte("nfloat"), []byte("123"))
if err != nil {
t.Fatal(err)
}
if g.NFloat == nil || *g.NFloat != 123 {
t.Errorf("want %v, got %v", 123, *g.NFloat)
}
}