-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.go
65 lines (62 loc) · 1.84 KB
/
utils_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
package bencodex
import (
"math/big"
"reflect"
"testing"
"github.com/sircoon4/bencodex-go/bencodextype"
)
// customizedAssertEqual is a function that compares the real values of the result and decoded data.
func customizedAssertEqual(t *testing.T, result any, decoded any) {
// If the decoded data is a dictionary type, compare the values of the result and decoded data.
dDict, ok := decoded.(*bencodextype.Dictionary)
if ok {
rDict, ok := result.(*bencodextype.Dictionary)
if !ok {
t.Fatalf("result and decoded are not equal")
}
if dDict.Length() != rDict.Length() {
t.Fatalf("result and decoded are not equal")
}
for _, key := range dDict.Keys() {
if rDict.Get(key) == nil {
t.Fatalf("result and decoded are not equal")
} else {
customizedAssertEqual(t, rDict.Get(key), dDict.Get(key))
}
}
} else {
// If the decoded data is a big.Int type, compare the values of the result and decoded data.
dBigInt, ok := decoded.(*big.Int)
if ok {
rBigInt, ok := result.(*big.Int)
if !ok {
t.Fatalf("result and decoded are not equal")
}
if dBigInt.Cmp(rBigInt) != 0 {
t.Fatalf("result and decoded are not equal")
}
} else {
// If the decoded data is not a dictionary or big.Int type, compare the values of the result and decoded data.
rvr := reflect.ValueOf(result)
rvd := reflect.ValueOf(decoded)
if rvr.Kind() == rvd.Kind() {
switch rvd.Kind() {
case reflect.Slice:
if rvr.Len() == rvd.Len() {
for i := 0; i < rvd.Len(); i++ {
customizedAssertEqual(t, rvr.Index(i).Interface(), rvd.Index(i).Interface())
}
} else {
t.Fatalf("result and decoded are not equal")
}
default:
if !reflect.DeepEqual(result, decoded) {
t.Fatalf("result and decoded are not equal")
}
}
} else {
t.Fatalf("result and decoded are not equal")
}
}
}
}