1
- package util_test
1
+ package hash_test
2
2
3
3
import (
4
+ "errors"
4
5
"testing"
5
6
6
7
"github.com/stretchr/testify/assert"
7
8
"github.com/stretchr/testify/require"
8
9
9
- "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/ util"
10
+ hashutil "github.com/operator-framework/operator-controller/internal/shared/ util/hash "
10
11
)
11
12
13
+ type unmarshalable struct {}
14
+
15
+ func (u * unmarshalable ) MarshalJSON () ([]byte , error ) {
16
+ return nil , errors .New ("unmarshalable" )
17
+ }
18
+
12
19
func TestDeepHashObject (t * testing.T ) {
13
20
tests := []struct {
14
21
name string
15
- wantErr bool
22
+ wantPanic bool
16
23
obj interface {}
17
24
expectedHash string
18
25
}{
19
26
{
20
- name : "populated obj with exported fields" ,
21
- wantErr : false ,
27
+ name : "populated obj with exported fields" ,
22
28
obj : struct {
23
29
Str string
24
30
Num int
@@ -37,8 +43,7 @@ func TestDeepHashObject(t *testing.T) {
37
43
expectedHash : "gta1bt5sybll5qjqxdiekmjm7py93glrinmnrfb31fj" ,
38
44
},
39
45
{
40
- name : "modified populated obj with exported fields" ,
41
- wantErr : false ,
46
+ name : "modified populated obj with exported fields" ,
42
47
obj : struct {
43
48
Str string
44
49
Num int
@@ -57,8 +62,7 @@ func TestDeepHashObject(t *testing.T) {
57
62
expectedHash : "1ftn1z2ieih8hsmi2a8c6mkoef6uodrtn4wtt1qapioh" ,
58
63
},
59
64
{
60
- name : "populated obj with unexported fields" ,
61
- wantErr : false ,
65
+ name : "populated obj with unexported fields" ,
62
66
obj : struct {
63
67
str string
64
68
num int
@@ -80,38 +84,42 @@ func TestDeepHashObject(t *testing.T) {
80
84
// The JSON encoder requires exported fields or it will generate
81
85
// the same hash as a completely empty object
82
86
name : "empty obj" ,
83
- wantErr : false ,
84
87
obj : struct {}{},
85
88
expectedHash : "16jfjhihxbzhfhs1k5mimq740kvioi98pfbea9q6qtf9" ,
86
89
},
87
90
{
88
91
name : "string a" ,
89
- wantErr : false ,
90
92
obj : "a" ,
91
93
expectedHash : "1lu1qv1451mq7gv9upu1cx8ffffi07rel5xvbvvc44dh" ,
92
94
},
93
95
{
94
96
name : "string b" ,
95
- wantErr : false ,
96
97
obj : "b" ,
97
98
expectedHash : "1ija85ah4gd0beltpfhszipkxfyqqxhp94tf2mjfgq61" ,
98
99
},
99
100
{
100
101
name : "nil obj" ,
101
- wantErr : false ,
102
102
obj : nil ,
103
103
expectedHash : "2im0kl1kwvzn46sr4cdtkvmdzrlurvj51xdzhwdht8l0" ,
104
104
},
105
+ {
106
+ name : "unmarshalable obj" ,
107
+ obj : & unmarshalable {},
108
+ wantPanic : true ,
109
+ },
105
110
}
106
111
for _ , tc := range tests {
107
112
t .Run (tc .name , func (t * testing.T ) {
108
- hash , err := util .DeepHashObject (tc .obj )
109
- if tc .wantErr {
110
- require .Error (t , err )
111
- } else {
112
- require .NoError (t , err )
113
+ test := func () {
114
+ hash := hashutil .DeepHashObject (tc .obj )
113
115
assert .Equal (t , tc .expectedHash , hash )
114
116
}
117
+
118
+ if tc .wantPanic {
119
+ require .Panics (t , test )
120
+ } else {
121
+ require .NotPanics (t , test )
122
+ }
115
123
})
116
124
}
117
125
}
0 commit comments