-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert_same_util.go
82 lines (65 loc) · 1.75 KB
/
assert_same_util.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
package actually
import (
"bytes"
"reflect"
w "github.com/bayashi/actually/witness"
)
func reportForSame(a *testingA) *w.Witness {
r := a.wi().Expect(a.expect).Got(a.got)
if a.showRawData {
r = r.ShowRaw()
}
return r
}
func reportForSameWithDiff(a *testingA) *w.Witness {
return reportForSame(a).ShowDiff()
}
// Just confirming only types are convertible or not
func objectsAreConvertible(expectv any, gotv any) bool {
return reflect.ValueOf(expectv).Type().ConvertibleTo(reflect.TypeOf(gotv)) &&
reflect.ValueOf(gotv).Type().ConvertibleTo(reflect.TypeOf(expectv))
}
func isSameConvertedValueAsOther(expectv any, gotv any) bool {
return reflect.DeepEqual(reflect.ValueOf(expectv).Convert(reflect.TypeOf(gotv)).Interface(), gotv)
}
func objectsAreSameType(expectv any, gotv any) bool {
return reflect.TypeOf(gotv) == reflect.TypeOf(expectv)
}
func objectsAreSame(expectv any, gotv any) bool {
if expectv == nil || gotv == nil {
return expectv == gotv
}
exp, ok := expectv.([]byte)
if !ok {
return reflect.DeepEqual(expectv, gotv)
}
act, ok := gotv.([]byte)
if !ok {
return false
}
if exp == nil || act == nil {
return exp == nil && act == nil
}
return bytes.Equal(exp, act)
}
func invalidCallForSame(a *testingA) {
if !a.setExpect {
panic("You called kind of Same() method, but you forgot to call Expect().")
}
if !a.setGot {
panic("You called kind of Same() method, but you forgot to call Got().")
}
}
func convert2float64(a any) float64 {
var f float64
b := reflect.TypeOf(a).Kind()
// https://pkg.go.dev/reflect#Kind
if b >= 2 && b <= 6 {
f = float64(reflect.ValueOf(a).Int())
} else if b >= 7 && b <= 11 {
f = float64(reflect.ValueOf(a).Uint())
} else if b == 13 || b == 14 {
f = reflect.ValueOf(a).Float()
}
return f
}