forked from gcash/bchd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelp_test.go
737 lines (710 loc) · 18.7 KB
/
help_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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// Copyright (c) 2014 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/gcash/bchd/btcjson"
)
// TestHelpReflectInternals ensures the various help functions which deal with
// reflect types work as expected for various Go types.
func TestHelpReflectInternals(t *testing.T) {
t.Parallel()
tests := []struct {
name string
reflectType reflect.Type
indentLevel int
key string
examples []string
isComplex bool
help string
isInvalid bool
}{
{
name: "int",
reflectType: reflect.TypeOf(int(0)),
key: "json-type-numeric",
examples: []string{"n"},
help: "n (json-type-numeric) fdk",
},
{
name: "*int",
reflectType: reflect.TypeOf((*int)(nil)),
key: "json-type-value",
examples: []string{"n"},
help: "n (json-type-value) fdk",
isInvalid: true,
},
{
name: "int8",
reflectType: reflect.TypeOf(int8(0)),
key: "json-type-numeric",
examples: []string{"n"},
help: "n (json-type-numeric) fdk",
},
{
name: "int16",
reflectType: reflect.TypeOf(int16(0)),
key: "json-type-numeric",
examples: []string{"n"},
help: "n (json-type-numeric) fdk",
},
{
name: "int32",
reflectType: reflect.TypeOf(int32(0)),
key: "json-type-numeric",
examples: []string{"n"},
help: "n (json-type-numeric) fdk",
},
{
name: "int64",
reflectType: reflect.TypeOf(int64(0)),
key: "json-type-numeric",
examples: []string{"n"},
help: "n (json-type-numeric) fdk",
},
{
name: "uint",
reflectType: reflect.TypeOf(uint(0)),
key: "json-type-numeric",
examples: []string{"n"},
help: "n (json-type-numeric) fdk",
},
{
name: "uint8",
reflectType: reflect.TypeOf(uint8(0)),
key: "json-type-numeric",
examples: []string{"n"},
help: "n (json-type-numeric) fdk",
},
{
name: "uint16",
reflectType: reflect.TypeOf(uint16(0)),
key: "json-type-numeric",
examples: []string{"n"},
help: "n (json-type-numeric) fdk",
},
{
name: "uint32",
reflectType: reflect.TypeOf(uint32(0)),
key: "json-type-numeric",
examples: []string{"n"},
help: "n (json-type-numeric) fdk",
},
{
name: "uint64",
reflectType: reflect.TypeOf(uint64(0)),
key: "json-type-numeric",
examples: []string{"n"},
help: "n (json-type-numeric) fdk",
},
{
name: "float32",
reflectType: reflect.TypeOf(float32(0)),
key: "json-type-numeric",
examples: []string{"n.nnn"},
help: "n.nnn (json-type-numeric) fdk",
},
{
name: "float64",
reflectType: reflect.TypeOf(float64(0)),
key: "json-type-numeric",
examples: []string{"n.nnn"},
help: "n.nnn (json-type-numeric) fdk",
},
{
name: "string",
reflectType: reflect.TypeOf(""),
key: "json-type-string",
examples: []string{`"json-example-string"`},
help: "\"json-example-string\" (json-type-string) fdk",
},
{
name: "bool",
reflectType: reflect.TypeOf(true),
key: "json-type-bool",
examples: []string{"json-example-bool"},
help: "json-example-bool (json-type-bool) fdk",
},
{
name: "array of int",
reflectType: reflect.TypeOf([1]int{0}),
key: "json-type-arrayjson-type-numeric",
examples: []string{"[n,...]"},
help: "[n,...] (json-type-arrayjson-type-numeric) fdk",
},
{
name: "slice of int",
reflectType: reflect.TypeOf([]int{0}),
key: "json-type-arrayjson-type-numeric",
examples: []string{"[n,...]"},
help: "[n,...] (json-type-arrayjson-type-numeric) fdk",
},
{
name: "struct",
reflectType: reflect.TypeOf(struct{}{}),
key: "json-type-object",
examples: []string{"{", "}\t\t"},
isComplex: true,
help: "{\n} ",
},
{
name: "struct indent level 1",
reflectType: reflect.TypeOf(struct{ field int }{}),
indentLevel: 1,
key: "json-type-object",
examples: []string{
" \"field\": n,\t(json-type-numeric)\t-field",
" },\t\t",
},
help: "{\n" +
" \"field\": n, (json-type-numeric) -field\n" +
"} ",
isComplex: true,
},
{
name: "array of struct indent level 0",
reflectType: func() reflect.Type {
type s struct {
field int
}
return reflect.TypeOf([]s{})
}(),
key: "json-type-arrayjson-type-object",
examples: []string{
"[{",
" \"field\": n,\t(json-type-numeric)\ts-field",
"},...]",
},
help: "[{\n" +
" \"field\": n, (json-type-numeric) s-field\n" +
"},...]",
isComplex: true,
},
{
name: "array of struct indent level 1",
reflectType: func() reflect.Type {
type s struct {
field int
}
return reflect.TypeOf([]s{})
}(),
indentLevel: 1,
key: "json-type-arrayjson-type-object",
examples: []string{
" \"field\": n,\t(json-type-numeric)\ts-field",
" },...],\t\t",
},
help: "[{\n" +
" \"field\": n, (json-type-numeric) s-field\n" +
"},...]",
isComplex: true,
},
{
name: "map",
reflectType: reflect.TypeOf(map[string]string{}),
key: "json-type-object",
examples: []string{"{",
" \"fdk--key\": fdk--value, (json-type-object) fdk--desc",
" ...", "}",
},
help: "{\n" +
" \"fdk--key\": fdk--value, (json-type-object) fdk--desc\n" +
" ...\n" +
"}",
isComplex: true,
},
{
name: "complex",
reflectType: reflect.TypeOf(complex64(0)),
key: "json-type-value",
examples: []string{"json-example-unknown"},
help: "json-example-unknown (json-type-value) fdk",
isInvalid: true,
},
}
xT := func(key string) string {
return key
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Ensure the description key is the expected value.
key := btcjson.TstReflectTypeToJSONType(xT, test.reflectType)
if key != test.key {
t.Errorf("Test #%d (%s) unexpected key - got: %v, "+
"want: %v", i, test.name, key, test.key)
continue
}
// Ensure the generated example is as expected.
examples, isComplex := btcjson.TstReflectTypeToJSONExample(xT,
test.reflectType, test.indentLevel, "fdk")
if isComplex != test.isComplex {
t.Errorf("Test #%d (%s) unexpected isComplex - got: %v, "+
"want: %v", i, test.name, isComplex,
test.isComplex)
continue
}
if len(examples) != len(test.examples) {
t.Errorf("Test #%d (%s) unexpected result length - "+
"got: %v, want: %v", i, test.name, len(examples),
len(test.examples))
continue
}
for j, example := range examples {
if example != test.examples[j] {
t.Errorf("Test #%d (%s) example #%d unexpected "+
"example - got: %v, want: %v", i,
test.name, j, example, test.examples[j])
continue
}
}
// Ensure the generated result type help is as expected.
helpText := btcjson.TstResultTypeHelp(xT, test.reflectType, "fdk")
if helpText != test.help {
t.Errorf("Test #%d (%s) unexpected result help - "+
"got: %v, want: %v", i, test.name, helpText,
test.help)
continue
}
isValid := btcjson.TstIsValidResultType(test.reflectType.Kind())
if isValid != !test.isInvalid {
t.Errorf("Test #%d (%s) unexpected result type validity "+
"- got: %v", i, test.name, isValid)
continue
}
}
}
// TestResultStructHelp ensures the expected help text format is returned for
// various Go struct types.
func TestResultStructHelp(t *testing.T) {
t.Parallel()
tests := []struct {
name string
reflectType reflect.Type
expected []string
}{
{
name: "empty struct",
reflectType: func() reflect.Type {
type s struct{}
return reflect.TypeOf(s{})
}(),
expected: nil,
},
{
name: "struct with primitive field",
reflectType: func() reflect.Type {
type s struct {
field int
}
return reflect.TypeOf(s{})
}(),
expected: []string{
"\"field\": n,\t(json-type-numeric)\ts-field",
},
},
{
name: "struct with primitive field and json tag",
reflectType: func() reflect.Type {
type s struct {
Field int `json:"f"`
}
return reflect.TypeOf(s{})
}(),
expected: []string{
"\"f\": n,\t(json-type-numeric)\ts-f",
},
},
{
name: "struct with array of primitive field",
reflectType: func() reflect.Type {
type s struct {
field []int
}
return reflect.TypeOf(s{})
}(),
expected: []string{
"\"field\": [n,...],\t(json-type-arrayjson-type-numeric)\ts-field",
},
},
{
name: "struct with sub-struct field",
reflectType: func() reflect.Type {
type s2 struct {
subField int
}
type s struct {
field s2
}
return reflect.TypeOf(s{})
}(),
expected: []string{
"\"field\": {\t(json-type-object)\ts-field",
"{",
" \"subfield\": n,\t(json-type-numeric)\ts2-subfield",
"}\t\t",
},
},
{
name: "struct with sub-struct field pointer",
reflectType: func() reflect.Type {
type s2 struct {
subField int
}
type s struct {
field *s2
}
return reflect.TypeOf(s{})
}(),
expected: []string{
"\"field\": {\t(json-type-object)\ts-field",
"{",
" \"subfield\": n,\t(json-type-numeric)\ts2-subfield",
"}\t\t",
},
},
{
name: "struct with array of structs field",
reflectType: func() reflect.Type {
type s2 struct {
subField int
}
type s struct {
field []s2
}
return reflect.TypeOf(s{})
}(),
expected: []string{
"\"field\": [{\t(json-type-arrayjson-type-object)\ts-field",
"[{",
" \"subfield\": n,\t(json-type-numeric)\ts2-subfield",
"},...]",
},
},
}
xT := func(key string) string {
return key
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
results := btcjson.TstResultStructHelp(xT, test.reflectType, 0)
if len(results) != len(test.expected) {
t.Errorf("Test #%d (%s) unexpected result length - "+
"got: %v, want: %v", i, test.name, len(results),
len(test.expected))
continue
}
for j, result := range results {
if result != test.expected[j] {
t.Errorf("Test #%d (%s) result #%d unexpected "+
"result - got: %v, want: %v", i,
test.name, j, result, test.expected[j])
continue
}
}
}
}
// TestHelpArgInternals ensures the various help functions which deal with
// arguments work as expected for various argument types.
func TestHelpArgInternals(t *testing.T) {
t.Parallel()
tests := []struct {
name string
method string
reflectType reflect.Type
defaults map[int]reflect.Value
help string
}{
{
name: "command with no args",
method: "test",
reflectType: func() reflect.Type {
type s struct{}
return reflect.TypeOf((*s)(nil))
}(),
defaults: nil,
help: "",
},
{
name: "command with one required arg",
method: "test",
reflectType: func() reflect.Type {
type s struct {
Field int
}
return reflect.TypeOf((*s)(nil))
}(),
defaults: nil,
help: "1. field (json-type-numeric, help-required) test-field\n",
},
{
name: "command with one optional arg, no default",
method: "test",
reflectType: func() reflect.Type {
type s struct {
Optional *int
}
return reflect.TypeOf((*s)(nil))
}(),
defaults: nil,
help: "1. optional (json-type-numeric, help-optional) test-optional\n",
},
{
name: "command with one optional arg with default",
method: "test",
reflectType: func() reflect.Type {
type s struct {
Optional *string
}
return reflect.TypeOf((*s)(nil))
}(),
defaults: func() map[int]reflect.Value {
defVal := "test"
return map[int]reflect.Value{
0: reflect.ValueOf(&defVal),
}
}(),
help: "1. optional (json-type-string, help-optional, help-default=\"test\") test-optional\n",
},
{
name: "command with struct field",
method: "test",
reflectType: func() reflect.Type {
type s2 struct {
F int8
}
type s struct {
Field s2
}
return reflect.TypeOf((*s)(nil))
}(),
defaults: nil,
help: "1. field (json-type-object, help-required) test-field\n" +
"{\n" +
" \"f\": n, (json-type-numeric) s2-f\n" +
"} \n",
},
{
name: "command with map field",
method: "test",
reflectType: func() reflect.Type {
type s struct {
Field map[string]float64
}
return reflect.TypeOf((*s)(nil))
}(),
defaults: nil,
help: "1. field (json-type-object, help-required) test-field\n" +
"{\n" +
" \"test-field--key\": test-field--value, (json-type-object) test-field--desc\n" +
" ...\n" +
"}\n",
},
{
name: "command with slice of primitives field",
method: "test",
reflectType: func() reflect.Type {
type s struct {
Field []int64
}
return reflect.TypeOf((*s)(nil))
}(),
defaults: nil,
help: "1. field (json-type-arrayjson-type-numeric, help-required) test-field\n",
},
{
name: "command with slice of structs field",
method: "test",
reflectType: func() reflect.Type {
type s2 struct {
F int64
}
type s struct {
Field []s2
}
return reflect.TypeOf((*s)(nil))
}(),
defaults: nil,
help: "1. field (json-type-arrayjson-type-object, help-required) test-field\n" +
"[{\n" +
" \"f\": n, (json-type-numeric) s2-f\n" +
"},...]\n",
},
}
xT := func(key string) string {
return key
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
help := btcjson.TstArgHelp(xT, test.reflectType, test.defaults,
test.method)
if help != test.help {
t.Errorf("Test #%d (%s) unexpected help - got:\n%v\n"+
"want:\n%v", i, test.name, help, test.help)
continue
}
}
}
// TestMethodHelp ensures the method help function works as expected for various
// command structs.
func TestMethodHelp(t *testing.T) {
t.Parallel()
tests := []struct {
name string
method string
reflectType reflect.Type
defaults map[int]reflect.Value
resultTypes []interface{}
help string
}{
{
name: "command with no args or results",
method: "test",
reflectType: func() reflect.Type {
type s struct{}
return reflect.TypeOf((*s)(nil))
}(),
help: "test\n\ntest--synopsis\n\n" +
"help-arguments:\nhelp-arguments-none\n\n" +
"help-result:\nhelp-result-nothing\n",
},
{
name: "command with no args and one primitive result",
method: "test",
reflectType: func() reflect.Type {
type s struct{}
return reflect.TypeOf((*s)(nil))
}(),
resultTypes: []interface{}{(*int64)(nil)},
help: "test\n\ntest--synopsis\n\n" +
"help-arguments:\nhelp-arguments-none\n\n" +
"help-result:\nn (json-type-numeric) test--result0\n",
},
{
name: "command with no args and two results",
method: "test",
reflectType: func() reflect.Type {
type s struct{}
return reflect.TypeOf((*s)(nil))
}(),
resultTypes: []interface{}{(*int64)(nil), nil},
help: "test\n\ntest--synopsis\n\n" +
"help-arguments:\nhelp-arguments-none\n\n" +
"help-result (test--condition0):\nn (json-type-numeric) test--result0\n\n" +
"help-result (test--condition1):\nhelp-result-nothing\n",
},
{
name: "command with primitive arg and no results",
method: "test",
reflectType: func() reflect.Type {
type s struct {
Field bool
}
return reflect.TypeOf((*s)(nil))
}(),
help: "test field\n\ntest--synopsis\n\n" +
"help-arguments:\n1. field (json-type-bool, help-required) test-field\n\n" +
"help-result:\nhelp-result-nothing\n",
},
{
name: "command with primitive optional and no results",
method: "test",
reflectType: func() reflect.Type {
type s struct {
Field *bool
}
return reflect.TypeOf((*s)(nil))
}(),
help: "test (field)\n\ntest--synopsis\n\n" +
"help-arguments:\n1. field (json-type-bool, help-optional) test-field\n\n" +
"help-result:\nhelp-result-nothing\n",
},
}
xT := func(key string) string {
return key
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
help := btcjson.TestMethodHelp(xT, test.reflectType,
test.defaults, test.method, test.resultTypes)
if help != test.help {
t.Errorf("Test #%d (%s) unexpected help - got:\n%v\n"+
"want:\n%v", i, test.name, help, test.help)
continue
}
}
}
// TestGenerateHelpErrors ensures the GenerateHelp function returns the expected
// errors.
func TestGenerateHelpErrors(t *testing.T) {
t.Parallel()
tests := []struct {
name string
method string
resultTypes []interface{}
err btcjson.Error
}{
{
name: "unregistered command",
method: "boguscommand",
err: btcjson.Error{ErrorCode: btcjson.ErrUnregisteredMethod},
},
{
name: "non-pointer result type",
method: "help",
resultTypes: []interface{}{0},
err: btcjson.Error{ErrorCode: btcjson.ErrInvalidType},
},
{
name: "invalid result type",
method: "help",
resultTypes: []interface{}{(*complex64)(nil)},
err: btcjson.Error{ErrorCode: btcjson.ErrInvalidType},
},
{
name: "missing description",
method: "help",
resultTypes: []interface{}{(*string)(nil), nil},
err: btcjson.Error{ErrorCode: btcjson.ErrMissingDescription},
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
_, err := btcjson.GenerateHelp(test.method, nil,
test.resultTypes...)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("Test #%d (%s) wrong error - got %T (%v), "+
"want %T", i, test.name, err, err, test.err)
continue
}
gotErrorCode := err.(btcjson.Error).ErrorCode
if gotErrorCode != test.err.ErrorCode {
t.Errorf("Test #%d (%s) mismatched error code - got "+
"%v (%v), want %v", i, test.name, gotErrorCode,
err, test.err.ErrorCode)
continue
}
}
}
// TestGenerateHelp performs a very basic test to ensure GenerateHelp is working
// as expected. The internal are testd much more thoroughly in other tests, so
// there is no need to add more tests here.
func TestGenerateHelp(t *testing.T) {
t.Parallel()
descs := map[string]string{
"help--synopsis": "test",
"help-command": "test",
}
help, err := btcjson.GenerateHelp("help", descs)
if err != nil {
t.Fatalf("GenerateHelp: unexpected error: %v", err)
}
wantHelp := "help (\"command\")\n\n" +
"test\n\nArguments:\n1. command (string, optional) test\n\n" +
"Result:\nNothing\n"
if help != wantHelp {
t.Fatalf("GenerateHelp: unexpected help - got\n%v\nwant\n%v",
help, wantHelp)
}
}