Skip to content

Commit

Permalink
use reflect.DeepEqual for gnostic test comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Zielenski committed Mar 10, 2022
1 parent 5916067 commit a373b1a
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pkg/validation/spec/gnostic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ 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"
"github.com/stretchr/testify/require"
"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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a373b1a

Please sign in to comment.