forked from btcsuite/btcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdinfo_test.go
430 lines (407 loc) · 9.67 KB
/
cmdinfo_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Copyright (c) 2015 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcjson_test
import (
"reflect"
"testing"
"github.com/btcsuite/btcd/btcjson"
)
// TestCmdMethod tests the CmdMethod function to ensure it retunrs the expected
// methods and errors.
func TestCmdMethod(t *testing.T) {
t.Parallel()
tests := []struct {
name string
cmd interface{}
method string
err error
}{
{
name: "unregistered type",
cmd: (*int)(nil),
err: btcjson.Error{ErrorCode: btcjson.ErrUnregisteredMethod},
},
{
name: "nil pointer of registered type",
cmd: (*btcjson.GetBlockCmd)(nil),
method: "getblock",
},
{
name: "nil instance of registered type",
cmd: &btcjson.GetBlockCountCmd{},
method: "getblockcount",
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
method, err := btcjson.CmdMethod(test.cmd)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("Test #%d (%s) wrong error - got %T (%[3]v), "+
"want %T", i, test.name, err, test.err)
continue
}
if err != nil {
gotErrorCode := err.(btcjson.Error).ErrorCode
if gotErrorCode != test.err.(btcjson.Error).ErrorCode {
t.Errorf("Test #%d (%s) mismatched error code "+
"- got %v (%v), want %v", i, test.name,
gotErrorCode, err,
test.err.(btcjson.Error).ErrorCode)
continue
}
continue
}
// Ensure method matches the expected value.
if method != test.method {
t.Errorf("Test #%d (%s) mismatched method - got %v, "+
"want %v", i, test.name, method, test.method)
continue
}
}
}
// TestMethodUsageFlags tests the MethodUsage function ensure it returns the
// expected flags and errors.
func TestMethodUsageFlags(t *testing.T) {
t.Parallel()
tests := []struct {
name string
method string
err error
flags btcjson.UsageFlag
}{
{
name: "unregistered type",
method: "bogusmethod",
err: btcjson.Error{ErrorCode: btcjson.ErrUnregisteredMethod},
},
{
name: "getblock",
method: "getblock",
flags: 0,
},
{
name: "walletpassphrase",
method: "walletpassphrase",
flags: btcjson.UFWalletOnly,
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
flags, err := btcjson.MethodUsageFlags(test.method)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("Test #%d (%s) wrong error - got %T (%[3]v), "+
"want %T", i, test.name, err, test.err)
continue
}
if err != nil {
gotErrorCode := err.(btcjson.Error).ErrorCode
if gotErrorCode != test.err.(btcjson.Error).ErrorCode {
t.Errorf("Test #%d (%s) mismatched error code "+
"- got %v (%v), want %v", i, test.name,
gotErrorCode, err,
test.err.(btcjson.Error).ErrorCode)
continue
}
continue
}
// Ensure flags match the expected value.
if flags != test.flags {
t.Errorf("Test #%d (%s) mismatched flags - got %v, "+
"want %v", i, test.name, flags, test.flags)
continue
}
}
}
// TestMethodUsageText tests the MethodUsageText function ensure it returns the
// expected text.
func TestMethodUsageText(t *testing.T) {
t.Parallel()
tests := []struct {
name string
method string
err error
expected string
}{
{
name: "unregistered type",
method: "bogusmethod",
err: btcjson.Error{ErrorCode: btcjson.ErrUnregisteredMethod},
},
{
name: "getblockcount",
method: "getblockcount",
expected: "getblockcount",
},
{
name: "getblock",
method: "getblock",
expected: `getblock "hash" (verbose=true verbosetx=false)`,
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
usage, err := btcjson.MethodUsageText(test.method)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("Test #%d (%s) wrong error - got %T (%[3]v), "+
"want %T", i, test.name, err, test.err)
continue
}
if err != nil {
gotErrorCode := err.(btcjson.Error).ErrorCode
if gotErrorCode != test.err.(btcjson.Error).ErrorCode {
t.Errorf("Test #%d (%s) mismatched error code "+
"- got %v (%v), want %v", i, test.name,
gotErrorCode, err,
test.err.(btcjson.Error).ErrorCode)
continue
}
continue
}
// Ensure usage matches the expected value.
if usage != test.expected {
t.Errorf("Test #%d (%s) mismatched usage - got %v, "+
"want %v", i, test.name, usage, test.expected)
continue
}
// Get the usage again to excerise caching.
usage, err = btcjson.MethodUsageText(test.method)
if err != nil {
t.Errorf("Test #%d (%s) unexpected error: %v", i,
test.name, err)
continue
}
// Ensure usage still matches the expected value.
if usage != test.expected {
t.Errorf("Test #%d (%s) mismatched usage - got %v, "+
"want %v", i, test.name, usage, test.expected)
continue
}
}
}
// TestFieldUsage tests the internal fieldUsage function ensure it returns the
// expected text.
func TestFieldUsage(t *testing.T) {
t.Parallel()
tests := []struct {
name string
field reflect.StructField
defValue *reflect.Value
expected string
}{
{
name: "jsonrpcusage tag override",
field: func() reflect.StructField {
type s struct {
Test int `jsonrpcusage:"testvalue"`
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: "testvalue",
},
{
name: "generic interface",
field: func() reflect.StructField {
type s struct {
Test interface{}
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `test`,
},
{
name: "string without default value",
field: func() reflect.StructField {
type s struct {
Test string
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `"test"`,
},
{
name: "string with default value",
field: func() reflect.StructField {
type s struct {
Test string
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: func() *reflect.Value {
value := "default"
rv := reflect.ValueOf(&value)
return &rv
}(),
expected: `test="default"`,
},
{
name: "array of strings",
field: func() reflect.StructField {
type s struct {
Test []string
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `["test",...]`,
},
{
name: "array of strings with plural field name 1",
field: func() reflect.StructField {
type s struct {
Keys []string
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `["key",...]`,
},
{
name: "array of strings with plural field name 2",
field: func() reflect.StructField {
type s struct {
Addresses []string
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `["address",...]`,
},
{
name: "array of strings with plural field name 3",
field: func() reflect.StructField {
type s struct {
Capabilities []string
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `["capability",...]`,
},
{
name: "array of structs",
field: func() reflect.StructField {
type s2 struct {
Txid string
}
type s struct {
Capabilities []s2
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `[{"txid":"value"},...]`,
},
{
name: "array of ints",
field: func() reflect.StructField {
type s struct {
Test []int
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `[test,...]`,
},
{
name: "sub struct with jsonrpcusage tag override",
field: func() reflect.StructField {
type s2 struct {
Test string `jsonrpcusage:"testusage"`
}
type s struct {
Test s2
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `{testusage}`,
},
{
name: "sub struct with string",
field: func() reflect.StructField {
type s2 struct {
Txid string
}
type s struct {
Test s2
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `{"txid":"value"}`,
},
{
name: "sub struct with int",
field: func() reflect.StructField {
type s2 struct {
Vout int
}
type s struct {
Test s2
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `{"vout":n}`,
},
{
name: "sub struct with float",
field: func() reflect.StructField {
type s2 struct {
Amount float64
}
type s struct {
Test s2
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `{"amount":n.nnn}`,
},
{
name: "sub struct with sub struct",
field: func() reflect.StructField {
type s3 struct {
Amount float64
}
type s2 struct {
Template s3
}
type s struct {
Test s2
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `{"template":{"amount":n.nnn}}`,
},
{
name: "sub struct with slice",
field: func() reflect.StructField {
type s2 struct {
Capabilities []string
}
type s struct {
Test s2
}
return reflect.TypeOf((*s)(nil)).Elem().Field(0)
}(),
defValue: nil,
expected: `{"capabilities":["capability",...]}`,
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Ensure usage matches the expected value.
usage := btcjson.TstFieldUsage(test.field, test.defValue)
if usage != test.expected {
t.Errorf("Test #%d (%s) mismatched usage - got %v, "+
"want %v", i, test.name, usage, test.expected)
continue
}
}
}