@@ -2,13 +2,16 @@ package dbauthz_test
2
2
3
3
import (
4
4
"context"
5
+ "encoding/gob"
5
6
"errors"
6
7
"fmt"
7
8
"reflect"
8
9
"sort"
9
10
"strings"
10
11
"testing"
11
12
13
+ "github.com/google/go-cmp/cmp"
14
+ "github.com/google/go-cmp/cmp/cmpopts"
12
15
"github.com/google/uuid"
13
16
"github.com/open-policy-agent/opa/topdown"
14
17
"github.com/stretchr/testify/require"
@@ -198,11 +201,29 @@ func (s *MethodTestSuite) Subtest(testCaseF func(db database.Store, check *expec
198
201
s .Equal (len (testCase .outputs ), len (outputs ), "method %q returned unexpected number of outputs" , methodName )
199
202
for i := range outputs {
200
203
a , b := testCase .outputs [i ].Interface (), outputs [i ].Interface ()
201
- if reflect .TypeOf (a ).Kind () == reflect .Slice || reflect .TypeOf (a ).Kind () == reflect .Array {
202
- // Order does not matter
203
- s .ElementsMatch (a , b , "method %q returned unexpected output %d" , methodName , i )
204
- } else {
205
- s .Equal (a , b , "method %q returned unexpected output %d" , methodName , i )
204
+
205
+ // To avoid the extra small overhead of gob encoding, we can
206
+ // first check if the values are equal with regard to order.
207
+ // If not, re-check disregarding order and show a nice diff
208
+ // output of the two values.
209
+ if ! cmp .Equal (a , b , cmpopts .EquateEmpty ()) {
210
+ if diff := cmp .Diff (a , b ,
211
+ // Equate nil and empty slices.
212
+ cmpopts .EquateEmpty (),
213
+ // Allow slice order to be ignored.
214
+ cmpopts .SortSlices (func (a , b any ) bool {
215
+ var ab , bb strings.Builder
216
+ _ = gob .NewEncoder (& ab ).Encode (a )
217
+ _ = gob .NewEncoder (& bb ).Encode (b )
218
+ // This might seem a bit dubious, but we really
219
+ // don't care about order and cmp doesn't provide
220
+ // a generic less function for slices:
221
+ // https://github.com/google/go-cmp/issues/67
222
+ return ab .String () < bb .String ()
223
+ }),
224
+ ); diff != "" {
225
+ s .Failf ("compare outputs failed" , "method %q returned unexpected output %d (-want +got):\n %s" , methodName , i , diff )
226
+ }
206
227
}
207
228
}
208
229
}
0 commit comments