Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
qawatake committed Sep 8, 2023
1 parent 1c15701 commit 2d0feb6
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
49 changes: 49 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package genopenapi_test

import (
"os"
"reflect"
"sort"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopenapi"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -120,6 +122,53 @@ func TestGenerateExtension(t *testing.T) {
}
}

func TestGenerateYAML(t *testing.T) {
t.Parallel()

tests := []struct {
name string
requestFileName string
responseFileName string
}{
{
name: "path_item_object",
requestFileName: "testdata/generator/path_item_object.prototext",
responseFileName: "testdata/generator/path_item_object.swagger.yaml",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

b, err := os.ReadFile(tt.requestFileName)
if err != nil {
t.Fatal(err)
}
var req pluginpb.CodeGeneratorRequest
if err := prototext.Unmarshal(b, &req); err != nil {
t.Fatal(err)
}

resp := requireGenerate(t, &req, genopenapi.FormatYAML, false, true)
if len(resp) != 1 {
t.Fatalf("invalid count, expected: 1, actual: %d", len(resp))
}
got := resp[0].GetContent()

want, err := os.ReadFile(tt.responseFileName)
if err != nil {
t.Fatal(err)
}
diff := cmp.Diff(string(want), got)
if diff != "" {
t.Fatalf("content not match\n%s", diff)
}
})
}
}

func requireGenerate(
tb testing.TB,
req *pluginpb.CodeGeneratorRequest,
Expand Down
Loading

0 comments on commit 2d0feb6

Please sign in to comment.