-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathencoder_test.go
129 lines (112 loc) · 3.05 KB
/
encoder_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package encoder
import (
"encoding/json"
// "encoding/xml"
"errors"
"reflect"
"testing"
)
type TestCase struct {
Name string
Encoder interface{}
Unmarshaller func([]byte, interface{}) error
Ref string
}
var Cases = []TestCase{
{
Name: "JsonEncoderTest",
Encoder: &JsonEncoder{},
Unmarshaller: json.Unmarshal,
},
// TODO.
// Problem with slice, it needs a root element for xml.
//
// {
// Name: "XmlEncoderTest",
// Encoder: &XmlEncoder{},
// Unmarshaller: xml.Unmarshal,
// },
}
type Profile struct {
Id string `json:"id"`
Name string `json:"name"`
Password string `json:"password,omitempty" xml:",omitempty"`
Avatar string `json:"avatar"`
}
func (this Profile) Filter() interface{} {
this.Password = ""
this.Avatar = "//origin/" + this.Avatar
return this
}
type testData struct {
name string
src interface{}
expected interface{}
}
var data = []testData{
{
name: "byVal",
src: Profile{Id: "1", Name: "Buster", Password: "hideme", Avatar: "xxx"},
expected: Profile{Id: "1", Name: "Buster", Password: "", Avatar: "//origin/xxx"},
},
{
name: "byPtr",
src: &Profile{Id: "1", Name: "Buster", Password: "hideme", Avatar: "xxx"},
expected: Profile{Id: "1", Name: "Buster", Password: "", Avatar: "//origin/xxx"},
},
{
name: "slice",
src: []Profile{
{Id: "1", Name: "Buster", Password: "hideme", Avatar: "xxx"},
{Id: "2", Name: "SomeOne", Password: "hideme", Avatar: "yyy"},
},
expected: []Profile{
{Id: "1", Name: "Buster", Password: "", Avatar: "//origin/xxx"},
{Id: "2", Name: "SomeOne", Password: "", Avatar: "//origin/yyy"},
},
},
}
func callEncode(i reflect.Value, arg interface{}) (data []byte, err error) {
method := i.MethodByName("Encode")
if !method.IsValid() {
return nil, errors.New("method 'Encode' not found")
}
result := method.Call([]reflect.Value{reflect.ValueOf(arg)})
if len(result) != 2 {
return nil, errors.New("unexpected emelents count in result")
}
if result[0].Interface() != nil {
data = result[0].Interface().([]byte)
}
if result[1].Interface() != nil {
err = result[1].Interface().(error)
}
return data, err
}
func TestAll(t *testing.T) {
for _, test := range Cases {
for _, d := range data {
result, err := callEncode(reflect.ValueOf(test.Encoder), d.src)
if err != nil {
t.Fatal(err)
}
if reflect.TypeOf(d.src).Kind() == reflect.Slice {
dst := make([]Profile, 0)
if err := test.Unmarshaller(result, &dst); err != nil {
t.Fatal("Test:", test.Name, d.name, "raised Unmarshaling error:", err)
}
if !reflect.DeepEqual(dst, d.expected) {
t.Fatalf("%s:%s fails!\nExpected:%v\nGot: %v\n", test.Name, d.name, d.expected, dst)
}
} else {
dst := Profile{}
if err := test.Unmarshaller(result, &dst); err != nil {
t.Fatal("Test:", test.Name, d.name, "raised Unmarshaling error:", err)
}
if !reflect.DeepEqual(dst, d.expected) {
t.Fatalf("%s:%s fails!\nExpected:%v\nGot: %v\n", test.Name, d.name, d.expected, dst)
}
}
}
}
}