1- package util_test
1+ package hash_test
22
33import (
4+ "errors"
45 "testing"
56
67 "github.com/stretchr/testify/assert"
78 "github.com/stretchr/testify/require"
89
9- "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/ util"
10+ hashutil "github.com/operator-framework/operator-controller/internal/shared/ util/hash "
1011)
1112
13+ type unmarshalable struct {}
14+
15+ func (u * unmarshalable ) MarshalJSON () ([]byte , error ) {
16+ return nil , errors .New ("unmarshalable" )
17+ }
18+
1219func TestDeepHashObject (t * testing.T ) {
1320 tests := []struct {
1421 name string
15- wantErr bool
22+ wantPanic bool
1623 obj interface {}
1724 expectedHash string
1825 }{
1926 {
20- name : "populated obj with exported fields" ,
21- wantErr : false ,
27+ name : "populated obj with exported fields" ,
2228 obj : struct {
2329 Str string
2430 Num int
@@ -37,8 +43,7 @@ func TestDeepHashObject(t *testing.T) {
3743 expectedHash : "gta1bt5sybll5qjqxdiekmjm7py93glrinmnrfb31fj" ,
3844 },
3945 {
40- name : "modified populated obj with exported fields" ,
41- wantErr : false ,
46+ name : "modified populated obj with exported fields" ,
4247 obj : struct {
4348 Str string
4449 Num int
@@ -57,8 +62,7 @@ func TestDeepHashObject(t *testing.T) {
5762 expectedHash : "1ftn1z2ieih8hsmi2a8c6mkoef6uodrtn4wtt1qapioh" ,
5863 },
5964 {
60- name : "populated obj with unexported fields" ,
61- wantErr : false ,
65+ name : "populated obj with unexported fields" ,
6266 obj : struct {
6367 str string
6468 num int
@@ -80,38 +84,42 @@ func TestDeepHashObject(t *testing.T) {
8084 // The JSON encoder requires exported fields or it will generate
8185 // the same hash as a completely empty object
8286 name : "empty obj" ,
83- wantErr : false ,
8487 obj : struct {}{},
8588 expectedHash : "16jfjhihxbzhfhs1k5mimq740kvioi98pfbea9q6qtf9" ,
8689 },
8790 {
8891 name : "string a" ,
89- wantErr : false ,
9092 obj : "a" ,
9193 expectedHash : "1lu1qv1451mq7gv9upu1cx8ffffi07rel5xvbvvc44dh" ,
9294 },
9395 {
9496 name : "string b" ,
95- wantErr : false ,
9697 obj : "b" ,
9798 expectedHash : "1ija85ah4gd0beltpfhszipkxfyqqxhp94tf2mjfgq61" ,
9899 },
99100 {
100101 name : "nil obj" ,
101- wantErr : false ,
102102 obj : nil ,
103103 expectedHash : "2im0kl1kwvzn46sr4cdtkvmdzrlurvj51xdzhwdht8l0" ,
104104 },
105+ {
106+ name : "unmarshalable obj" ,
107+ obj : & unmarshalable {},
108+ wantPanic : true ,
109+ },
105110 }
106111 for _ , tc := range tests {
107112 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 )
113115 assert .Equal (t , tc .expectedHash , hash )
114116 }
117+
118+ if tc .wantPanic {
119+ require .Panics (t , test )
120+ } else {
121+ require .NotPanics (t , test )
122+ }
115123 })
116124 }
117125}
0 commit comments