From f9d96bfbe0c9197643927e01d85b24077e3b0a3d Mon Sep 17 00:00:00 2001 From: Alexander Zielenski Date: Wed, 9 Mar 2022 13:34:38 -0800 Subject: [PATCH] use reflect.DeepEqual for gnostic test comparisons --- pkg/validation/spec/gnostic_test.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/validation/spec/gnostic_test.go b/pkg/validation/spec/gnostic_test.go index 6b944120f..880ce279d 100644 --- a/pkg/validation/spec/gnostic_test.go +++ b/pkg/validation/spec/gnostic_test.go @@ -4,10 +4,12 @@ import ( "encoding/json" "io" "os" + "reflect" "testing" "time" "github.com/go-openapi/jsonreference" + "github.com/google/go-cmp/cmp" fuzz "github.com/google/gofuzz" openapi_v2 "github.com/googleapis/gnostic/openapiv2" "github.com/stretchr/testify/assert" @@ -15,6 +17,13 @@ import ( "google.golang.org/protobuf/proto" ) +var SpecV2DiffOptions = []cmp.Option{ + // cmp.Diff panics on Ref since jsonreference.Ref uses unexported fields + cmp.Comparer(func (a Ref, b Ref) bool { + return a.String() == b.String() + }), +} + func gnosticCommonTest(t testing.TB, fuzzer *fuzz.Fuzzer) { fuzzer.Funcs( func (v **Paths, c fuzz.Continue) { @@ -327,11 +336,15 @@ func gnosticCommonTest(t testing.TB, fuzzer *fuzz.Fuzzer) { actual := Swagger{} require.NoError(t, actual.FromGnostic(gnosticSpec)) - require.EqualValues(t, expected, actual) + if !reflect.DeepEqual(expected, actual) { + t.Fatal(cmp.Diff(expected, actual, SpecV2DiffOptions...)) + } newJsonBytes, err := json.Marshal(actual) require.NoError(t, err) - require.Equal(t, string(jsonBytes), string(newJsonBytes)) + if !reflect.DeepEqual(jsonBytes, newJsonBytes) { + t.Fatal(cmp.Diff(string(jsonBytes), string(newJsonBytes), SpecV2DiffOptions...)) + } } func TestGnosticConversionSmallDeterministic(t *testing.T) {