-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecode_test.go
114 lines (102 loc) · 2.25 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
111
112
113
114
package ltsv
import (
"reflect"
"testing"
)
func TestData2Map(t *testing.T) {
l, _ := data2map([]byte("hoge: fuga\tpiyo: piyo"))
expect := ltsvMap{
"hoge": "fuga",
"piyo": "piyo",
}
if !reflect.DeepEqual(l, expect) {
t.Errorf("result of data2map not expected: %#v", l)
}
_, err := data2map([]byte("hoge"))
if err.Error() != "not a ltsv: hoge" {
t.Errorf("something went wrong")
}
}
type ss struct {
User string `ltsv:"user"`
Age uint8 `ltsv:"age"`
Height *float64 `ltsv:"height"`
Weight float32
EmailVerified bool `ltsv:"email_verified"`
Memo string `ltsv:"-"`
}
func pfloat64(f float64) *float64 {
return &f
}
var decodeTests = []struct {
Name string
Input string
Output *ss
}{
{
Name: "Simple",
Input: "user:songmu\tage:36\theight:169.1\tweight:66.6\temail_verified:true",
Output: &ss{
User: "songmu",
Age: 36,
Height: pfloat64(169.1),
Weight: 66.6,
EmailVerified: true,
},
},
{
Name: "Default values",
Input: "user:songmu\tage:36",
Output: &ss{
User: "songmu",
Age: 36,
Height: nil,
Weight: 0.0,
EmailVerified: false,
},
},
{
Name: "Hyphen and empty string as null number",
Input: "user:songmu\tage:\theight:-",
Output: &ss{
User: "songmu",
Age: 0,
Height: nil,
Weight: 0.0,
EmailVerified: false,
},
},
}
func TestUnmarshal(t *testing.T) {
m := make(map[string]string)
Unmarshal([]byte("hoge: fuga\tpiyo: piyo"), &m)
expect := map[string]string{
"hoge": "fuga",
"piyo": "piyo",
}
if !reflect.DeepEqual(m, expect) {
t.Errorf("unmarsharl error:\n out: %+v\n want: %+v", m, expect)
}
for _, tt := range decodeTests {
t.Logf("testing: %s\n", tt.Name)
s := &ss{}
err := Unmarshal([]byte(tt.Input), s)
if err != nil {
t.Errorf("%s(err): error should be nil but: %+v", tt.Name, err)
}
if !reflect.DeepEqual(s, tt.Output) {
t.Errorf("%s:\n out: %+v\n want: %+v", tt.Name, s, tt.Output)
}
}
}
func BenchmarkUnmarshalStruct(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tt := range decodeTests {
s := &ss{}
err := Unmarshal([]byte(tt.Input), s)
if err != nil {
b.Error(err)
}
}
}
}