-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal_test.go
150 lines (115 loc) · 3.19 KB
/
marshal_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package querystring
import (
"net/url"
"testing"
)
func TestEncode(t *testing.T) {
type Item struct {
ItemName string `json:"item_name" url:"item_name"`
ItemValue string `json:"item_value" url:"item_value"`
}
type Person struct {
Name string `json:"name"`
NickName string `json:"nick_ame" url:"nick_name"`
Items []Item `json:"items" url:"items"`
}
p := Person{
Name: "Json",
NickName: "J",
}
p.Items = append(p.Items, Item{ItemName: "hello", ItemValue: "world"})
p.Items = append(p.Items, Item{ItemName: "hello", ItemValue: "world"})
myMap := make(map[string]interface{})
myMap["test1"] = "asdfasfd"
myMap["test2"] = 2
myMap["test3"] = "my_test"
vs := make(url.Values)
encode(p, "", vs)
str := vs.Encode()
t.Logf("result===:%v", str)
vs = make(url.Values)
encode(myMap, "", vs)
t.Logf("result2:%v", vs.Encode())
}
func TestEncode2(t *testing.T) {
type Item struct {
ItemName string `json:"item_name" url:"item_name"`
ItemValue string `json:"item_value" url:"item_value"`
}
type Person struct {
Name string `json:"name"`
NickName string `json:"nick_ame" url:"nick_name"`
Items []Item `json:"items" url:"items"`
MItems map[string]interface{} `json:"m_items" url:"m_items"`
}
p := Person{
Name: "Jason",
NickName: "J",
}
p.Items = append(p.Items, Item{ItemName: "hello", ItemValue: "world"})
p.Items = append(p.Items, Item{ItemName: "hello", ItemValue: "world"})
myMap := make(map[string]interface{})
myMap["test1"] = "asdfasfd"
myMap["test2"] = 2
myMap["test3"] = "my_test"
p.MItems = myMap
vs := make(url.Values)
encode(p, "", vs)
t.Logf("result:%+v", vs.Encode())
}
func TestMarshal1(t *testing.T) {
type Item struct {
ItemName string `json:"item_name" url:"item_name"`
ItemValue string `json:"item_value" url:"item_value"`
}
type Person struct {
Name string `json:"name"`
NickName string `json:"nick_ame" url:"nick_name"`
Items []Item `json:"items" url:"items"`
MItems map[string]interface{} `json:"m_items" url:"m_items"`
}
p := Person{
Name: "Jason",
NickName: "J",
}
p.Items = append(p.Items, Item{ItemName: "hello", ItemValue: "world"})
p.Items = append(p.Items, Item{ItemName: "hello", ItemValue: "world"})
myMap := make(map[string]interface{})
myMap["test1"] = "asdfasfd"
myMap["test2"] = 2
myMap["test3"] = "my_test"
p.MItems = myMap
result, err := Marshal(p)
t.Logf("result:%+v ,error:%+v", result, err)
}
func TestMarshal2(t *testing.T) {
type Person struct {
Name string `json:"name"`
NickName string `json:"nick_name" url:"nick_name"`
}
p := Person{
Name: "Json",
NickName: "J",
}
result, err := Marshal(p)
if result != "name=Json&nick_name=J" {
t.Errorf("marshal error")
}
t.Logf("result:%+v ,error:%+v", result, err)
result, err = Marshal(123)
if result != "" {
t.Errorf("marshal error")
}
t.Logf("result:%+v ,error:%+v", result, err)
type Test struct {
A string `url:"a"`
}
te := Test{
A: "",
}
result, err = Marshal(te)
if result != "a=%EE%80%80" {
t.Errorf("marshal t error")
}
t.Logf("marshal t result :%+v ,error:%+v", result, err)
}