-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_runner.go
93 lines (81 loc) · 2.13 KB
/
test_runner.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
package coincodec
import (
"encoding/hex"
"fmt"
"reflect"
"strings"
"testing"
)
type TestcaseEncode struct {
name string
input string // string
output string // encoded as hex string
err error
}
type TestcaseDecode struct {
name string
input string // encoded as hex string
output string // string
err error
}
func errorString(err error) string {
if err != nil {
return err.Error()
}
return "(no error)"
}
func RunTestsEncode(t *testing.T, coinType uint32, tests []TestcaseEncode) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := RunTestEncode(coinType, tt)
if err != nil {
t.Error(err.Error())
}
})
}
}
func RunTestEncode(coinType uint32, tt TestcaseEncode) error {
testfun, _ := toBytesMap[coinType]
got, err := testfun(tt.input)
goterror := errorString(err)
if tt.err != nil {
if !strings.HasPrefix(goterror, tt.err.Error()) {
return fmt.Errorf("%v %v: ToBytes() error = %v, wantErr %v", coinType, tt.name, goterror, tt.err)
}
} else {
gothex := hex.EncodeToString(got)
if !reflect.DeepEqual(gothex, tt.output) {
return fmt.Errorf("%v %v: ToBytes() = %v, err: %v, want %v, err: %v", coinType, tt.name, gothex, goterror, tt.output, tt.err)
}
}
return nil
}
func RunTestsDecode(t *testing.T, coinType uint32, tests []TestcaseDecode) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := RunTestDecode(coinType, tt)
if err != nil {
t.Error(err.Error())
}
})
}
}
func RunTestDecode(coinType uint32, tt TestcaseDecode) error {
decoded, err := hex.DecodeString(tt.input)
if err != nil {
return fmt.Errorf("%v %v: Preparation error, input is not valid hex string err %v input %v", coinType, tt.name, err, tt.input)
}
testfun, _ := toStringMap[coinType]
got, err := testfun(decoded)
goterror := errorString(err)
if tt.err != nil {
if goterror != tt.err.Error() {
return fmt.Errorf("%v %v: ToString() error = %v, wantErr %v", coinType, tt.name, goterror, tt.err)
}
} else {
if got != tt.output {
return fmt.Errorf("%v %v: ToString() = %v, want %v, err: %v", coinType, tt.name, got, tt.output, goterror)
}
}
return nil
}