Skip to content

Commit

Permalink
openapi3: refactor tests: use BoolPtr, require.Same (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Jun 20, 2023
1 parent 48ce1ee commit f333d82
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 22 deletions.
8 changes: 2 additions & 6 deletions openapi3/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,8 @@ func TestContent_Get(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Using require.True here because require.Same is not yet released.
// We're comparing pointer values and the require.Equal will
// dereference and compare the pointed to values rather than check
// if the memory addresses are the same. Once require.Same is released
// this test should convert to using that.
require.True(t, tt.want == tt.content.Get(tt.mime))
got := tt.content.Get(tt.mime)
require.Same(t, tt.want, got)
})
}
}
2 changes: 1 addition & 1 deletion openapi3/discriminator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ func TestParsingDiscriminator(t *testing.T) {
err = doc.Validate(loader.Context)
require.NoError(t, err)

require.Equal(t, 2, len(doc.Components.Schemas["MyResponseType"].Value.Discriminator.Mapping))
require.Len(t, doc.Components.Schemas["MyResponseType"].Value.Discriminator.Mapping, 2)
}
13 changes: 5 additions & 8 deletions openapi3/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package openapi3
import (
"context"
"encoding/json"
"reflect"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -45,7 +44,6 @@ var encodingJSON = []byte(`
`)

func encoding() *Encoding {
explode := true
return &Encoding{
ContentType: "application/json",
Headers: map[string]*HeaderRef{
Expand All @@ -54,13 +52,12 @@ func encoding() *Encoding {
},
},
Style: "form",
Explode: &explode,
Explode: BoolPtr(true),
AllowReserved: true,
}
}

func TestEncodingSerializationMethod(t *testing.T) {
boolPtr := func(b bool) *bool { return &b }
testCases := []struct {
name string
enc *Encoding
Expand All @@ -77,24 +74,24 @@ func TestEncodingSerializationMethod(t *testing.T) {
},
{
name: "encoding with explode",
enc: &Encoding{Explode: boolPtr(true)},
enc: &Encoding{Explode: BoolPtr(true)},
want: &SerializationMethod{Style: SerializationForm, Explode: true},
},
{
name: "encoding with no explode",
enc: &Encoding{Explode: boolPtr(false)},
enc: &Encoding{Explode: BoolPtr(false)},
want: &SerializationMethod{Style: SerializationForm, Explode: false},
},
{
name: "encoding with style and explode ",
enc: &Encoding{Style: SerializationSpaceDelimited, Explode: boolPtr(false)},
enc: &Encoding{Style: SerializationSpaceDelimited, Explode: BoolPtr(false)},
want: &SerializationMethod{Style: SerializationSpaceDelimited, Explode: false},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := tc.enc.SerializationMethod()
require.True(t, reflect.DeepEqual(got, tc.want), "got %#v, want %#v", got, tc.want)
require.EqualValues(t, got, tc.want, "got %#v, want %#v", got, tc.want)
})
}
}
11 changes: 4 additions & 7 deletions openapi3filter/req_resp_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ import (

func TestDecodeParameter(t *testing.T) {
var (
boolPtr = func(b bool) *bool { return &b }
explode = boolPtr(true)
noExplode = boolPtr(false)
explode = openapi3.BoolPtr(true)
noExplode = openapi3.BoolPtr(false)
arrayOf = func(items *openapi3.SchemaRef) *openapi3.SchemaRef {
return &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "array", Items: items}}
}
Expand Down Expand Up @@ -1079,8 +1078,6 @@ func TestDecodeParameter(t *testing.T) {
}

func TestDecodeBody(t *testing.T) {
boolPtr := func(b bool) *bool { return &b }

urlencodedForm := make(url.Values)
urlencodedForm.Set("a", "a1")
urlencodedForm.Set("b", "10")
Expand Down Expand Up @@ -1198,7 +1195,7 @@ func TestDecodeBody(t *testing.T) {
WithProperty("b", openapi3.NewIntegerSchema()).
WithProperty("c", openapi3.NewArraySchema().WithItems(openapi3.NewStringSchema())),
encoding: map[string]*openapi3.Encoding{
"c": {Style: openapi3.SerializationSpaceDelimited, Explode: boolPtr(false)},
"c": {Style: openapi3.SerializationSpaceDelimited, Explode: openapi3.BoolPtr(false)},
},
want: map[string]interface{}{"a": "a1", "b": int64(10), "c": []interface{}{"c1", "c2"}},
},
Expand All @@ -1211,7 +1208,7 @@ func TestDecodeBody(t *testing.T) {
WithProperty("b", openapi3.NewIntegerSchema()).
WithProperty("c", openapi3.NewArraySchema().WithItems(openapi3.NewStringSchema())),
encoding: map[string]*openapi3.Encoding{
"c": {Style: openapi3.SerializationPipeDelimited, Explode: boolPtr(false)},
"c": {Style: openapi3.SerializationPipeDelimited, Explode: openapi3.BoolPtr(false)},
},
want: map[string]interface{}{"a": "a1", "b": int64(10), "c": []interface{}{"c1", "c2"}},
},
Expand Down

0 comments on commit f333d82

Please sign in to comment.