5
package xmlrpc
5
package xmlrpc
6
6
7
import (
7
import (
8
- "encoding/xml"
9
"testing"
8
"testing"
10
9
11
"github.com/shuLhan/share/lib/test"
10
"github.com/shuLhan/share/lib/test"
@@ -17,22 +16,16 @@ type testStruct struct {
17
}
16
}
18
17
19
func TestRequest_MarshalText (t * testing.T ) {
18
func TestRequest_MarshalText (t * testing.T ) {
20
- cases := [] struct {
19
+ type testCase struct {
21
methodName string
20
methodName string
22
params []interface {}
21
params []interface {}
23
- exp string
22
+ }
24
- }{{
23
+
24
+ var cases = []testCase {{
25
methodName : "method.name" ,
25
methodName : "method.name" ,
26
params : []interface {}{
26
params : []interface {}{
27
"param-string" ,
27
"param-string" ,
28
},
28
},
29
- exp : xml .Header + "<methodCall><methodName>method.name</methodName>" +
30
- "<params>" +
31
- "<param>" +
32
- "<value><string>param-string</string></value>" +
33
- "</param>" +
34
- "</params>" +
35
- "</methodCall>" ,
36
}, {
29
}, {
37
methodName : "test.struct" ,
30
methodName : "test.struct" ,
38
params : []interface {}{
31
params : []interface {}{
@@ -41,154 +34,81 @@ func TestRequest_MarshalText(t *testing.T) {
41
Y : true ,
34
Y : true ,
42
},
35
},
43
},
36
},
44
- exp : xml .Header + "<methodCall><methodName>test.struct</methodName>" +
45
- "<params><param><value><struct>" +
46
- "<member>" +
47
- "<name>X</name><value><int>1</int></value>" +
48
- "</member>" +
49
- "<member>" +
50
- "<name>Y</name><value><boolean>true</boolean></value>" +
51
- "</member>" +
52
- "</struct></value></param></params>" +
53
- "</methodCall>" ,
54
}, {
37
}, {
55
methodName : "test.array" ,
38
methodName : "test.array" ,
56
params : []interface {}{
39
params : []interface {}{
57
[]string {"a" , "b" },
40
[]string {"a" , "b" },
58
},
41
},
59
- exp : xml .Header + "<methodCall><methodName>test.array</methodName>" +
60
- "<params><param><value><array><data>" +
61
- "<value><string>a</string></value>" +
62
- "<value><string>b</string></value>" +
63
- "</data></array></value></param></params>" +
64
- "</methodCall>" ,
65
}}
42
}}
66
43
67
- for _ , c := range cases {
44
+ var (
68
- req , err := NewRequest (c .methodName , c .params )
45
+ c testCase
46
+ req Request
47
+ tdata * test.Data
48
+ got []byte
49
+ exp []byte
50
+ err error
51
+ )
52
+
53
+ tdata , err = test .LoadData ("testdata/marshal_test.txt" )
54
+ if err != nil {
55
+ t .Fatal (err )
56
+ }
57
+
58
+ for _ , c = range cases {
59
+ req , err = NewRequest (c .methodName , c .params )
69
if err != nil {
60
if err != nil {
70
t .Fatal (err )
61
t .Fatal (err )
71
}
62
}
72
63
73
- got , err : = req .MarshalText ()
64
+ got , err = req .MarshalText ()
74
if err != nil {
65
if err != nil {
75
t .Fatal (err )
66
t .Fatal (err )
76
}
67
}
77
68
78
- test .Assert (t , "Pack" , c .exp , string (got ))
69
+ exp = tdata .Output [c .methodName ]
70
+ test .Assert (t , "Pack" , string (exp ), string (got ))
79
}
71
}
80
}
72
}
81
73
82
func TestRequest_UnmarshalText (t * testing.T ) {
74
func TestRequest_UnmarshalText (t * testing.T ) {
83
- cases := []struct {
75
+ var (
84
- desc string
76
+ tdata * test.Data
85
- in string
77
+ name string
86
- exp * Request
78
+ xmlInput []byte
87
- expError string
79
+ err error
88
- }{{
80
+ )
89
- desc : "Multiple param" ,
81
+
90
- in : `<?xml version="1.0"?>
82
+ tdata , err = test .LoadData ("testdata/unmarshal_test.txt" )
91
- <methodCall>
83
+ if err != nil {
92
- <methodName>method.name</methodName>
84
+ t .Fatal (err )
93
- <params>
85
+ }
94
- <param>
95
- <value>
96
- <string>
97
- param-string
98
- </string>
99
- </value>
100
- </param>
101
- <param>
102
- <value>
103
- <int>
104
- 1
105
- </int>
106
- </value>
107
- </param>
108
- </params>
109
- </methodCall>` ,
110
- exp : & Request {
111
- MethodName : "method.name" ,
112
- Params : []* Value {{
113
- Kind : String ,
114
- In : "param-string" ,
115
- }, {
116
- Kind : Integer ,
117
- In : int32 (1 ),
118
- }},
119
- },
120
- }, {
121
- desc : "Param as struct" ,
122
- in : `<?xml version="1.0"?>
123
- <methodCall>
124
- <methodName>test.struct</methodName>
125
- <params>
126
- <param>
127
- <value>
128
- <struct>
129
- <member>
130
- <name>X</name>
131
- <value><int>1</int></value>
132
- </member>
133
- <member>
134
- <name>Y</name>
135
- <value><boolean>true</boolean></value>
136
- </member>
137
- </struct>
138
- </value>
139
- </param>
140
- </params>
141
- </methodCall>` ,
142
- exp : & Request {
143
- MethodName : "test.struct" ,
144
- Params : []* Value {{
145
- Kind : Struct ,
146
- StructMembers : map [string ]* Value {
147
- "X" : & Value {
148
- Kind : Integer ,
149
- In : int32 (1 ),
150
- },
151
- "Y" : & Value {
152
- Kind : Boolean ,
153
- In : true ,
154
- },
155
- },
156
- }},
157
- },
158
- }, {
159
- desc : "Param as array" ,
160
- in : `<?xml version="1.0"?>
161
- <methodCall><methodName>test.array</methodName>
162
- <params><param><value><array><data>
163
- <value><string>a</string></value>
164
- <value><string>b</string></value>
165
- </data></array></value></param></params>
166
- </methodCall>` ,
167
- exp : & Request {
168
- MethodName : "test.array" ,
169
- Params : []* Value {{
170
- Kind : Array ,
171
- ArrayValues : []* Value {{
172
- Kind : String ,
173
- In : "a" ,
174
- }, {
175
- Kind : String ,
176
- In : "b" ,
177
- }},
178
- }},
179
- },
180
- }}
181
86
182
- for _ , c := range cases {
87
+ for name , xmlInput = range tdata .Input {
183
- t .Logf (c .desc )
88
+ t .Run (name , func (t * testing.T ) {
89
+ var (
90
+ req * Request
91
+ exp string
92
+ got string
93
+ xmlb []byte
94
+ err error
95
+ )
184
96
185
- got := & Request {}
97
+ exp = string (tdata .Output [name ])
186
- err := got .UnmarshalText ([]byte (c .in ))
98
+
187
- if err != nil {
99
+ req = & Request {}
188
- test .Assert (t , "Unmarshal" , c .expError , err .Error ())
100
+ err = req .UnmarshalText (xmlInput )
189
- continue
101
+ if err != nil {
190
- }
102
+ got = err .Error ()
103
+ } else {
104
+ xmlb , err = req .MarshalText ()
105
+ if err != nil {
106
+ t .Fatal (err )
107
+ }
108
+ got = string (xmlb )
109
+ }
191
110
192
- test .Assert (t , "Unmarshal" , c .exp , got )
111
+ test .Assert (t , name , exp , got )
112
+ })
193
}
113
}
194
}
114
}
0 commit comments