github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/plugin/grpcecosystem/grpcgateway/protoc-gen-grpc-gateway_test.go (about)

     1  package grpcgateway
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stackb/rules_proto/pkg/plugintest"
     7  )
     8  
     9  const serviceEmpty = `
    10  service S{}
    11  `
    12  
    13  const serviceWithUnboundMethods = `
    14  service S{rpc R (I) returns (O) {}}
    15  message I{}
    16  message O{}
    17  `
    18  
    19  const serviceWithBoundMethods = `
    20  import "google/api/annotations.proto";
    21  service S{rpc R (I) returns (O) {
    22    option (google.api.http) = {
    23      get: "/path"
    24     };
    25  }}
    26  message I{}
    27  message O{}
    28  `
    29  
    30  func TestProtocGenGoPlugin(t *testing.T) {
    31  	plugintest.Cases(t, &protocGenGrpcGatewayPlugin{}, map[string]plugintest.Case{
    32  		"no service": {
    33  			Input: "message M{}",
    34  			Directives: plugintest.WithDirectives(
    35  				"proto_plugin", "protoc-gen-grpc-gateway implementation grpc-ecosystem:grpc-gateway:protoc-gen-grpc-gateway",
    36  			),
    37  			PluginName:      "protoc-gen-grpc-gateway",
    38  			Configuration:   nil,
    39  			SkipIntegration: true,
    40  		},
    41  		"empty service": {
    42  			Input: serviceEmpty,
    43  			Directives: plugintest.WithDirectives(
    44  				"proto_plugin", "protoc-gen-grpc-gateway implementation grpc-ecosystem:grpc-gateway:protoc-gen-grpc-gateway",
    45  			),
    46  			PluginName:      "protoc-gen-grpc-gateway",
    47  			Configuration:   nil,
    48  			SkipIntegration: true,
    49  		},
    50  		"empty service and generate_unbound_methods": {
    51  			Input: serviceEmpty,
    52  			Directives: plugintest.WithDirectives(
    53  				"proto_plugin", "protoc-gen-grpc-gateway implementation grpc-ecosystem:grpc-gateway:protoc-gen-grpc-gateway",
    54  				"proto_plugin", "protoc-gen-grpc-gateway option generate_unbound_methods=true",
    55  			),
    56  			PluginName:      "protoc-gen-grpc-gateway",
    57  			Configuration:   nil,
    58  			SkipIntegration: true,
    59  		},
    60  		"service with unbound methods": {
    61  			Input: serviceWithUnboundMethods,
    62  			Directives: plugintest.WithDirectives(
    63  				"proto_plugin", "protoc-gen-grpc-gateway implementation grpc-ecosystem:grpc-gateway:protoc-gen-grpc-gateway",
    64  			),
    65  			PluginName:      "protoc-gen-grpc-gateway",
    66  			Configuration:   nil,
    67  			SkipIntegration: true,
    68  		},
    69  		"service with unbound methods and generate_unbound_methods": {
    70  			Input: serviceWithUnboundMethods,
    71  			Directives: plugintest.WithDirectives(
    72  				"proto_plugin", "protoc-gen-grpc-gateway implementation grpc-ecosystem:grpc-gateway:protoc-gen-grpc-gateway",
    73  				"proto_plugin", "protoc-gen-grpc-gateway option generate_unbound_methods=true",
    74  			),
    75  			PluginName: "protoc-gen-grpc-gateway",
    76  			Configuration: plugintest.WithConfiguration(
    77  				plugintest.WithLabel(t, "@build_stack_rules_proto//plugin/grpc-ecosystem/grpc-gateway:protoc-gen-grpc-gateway"),
    78  				plugintest.WithOptions("generate_unbound_methods=true"),
    79  				plugintest.WithOutputs("test.pb.gw.go"),
    80  			),
    81  			SkipIntegration: true,
    82  		},
    83  		"service with bound methods": {
    84  			Input: serviceWithBoundMethods,
    85  			Directives: plugintest.WithDirectives(
    86  				"proto_plugin", "protoc-gen-grpc-gateway implementation grpc-ecosystem:grpc-gateway:protoc-gen-grpc-gateway",
    87  			),
    88  			PluginName: "protoc-gen-grpc-gateway",
    89  			Configuration: plugintest.WithConfiguration(
    90  				plugintest.WithLabel(t, "@build_stack_rules_proto//plugin/grpc-ecosystem/grpc-gateway:protoc-gen-grpc-gateway"),
    91  				plugintest.WithOutputs("test.pb.gw.go"),
    92  			),
    93  			SkipIntegration: true,
    94  		},
    95  		"service with bound methods and generate_unbound_methods": {
    96  			Input: serviceWithBoundMethods,
    97  			Directives: plugintest.WithDirectives(
    98  				"proto_plugin", "protoc-gen-grpc-gateway implementation grpc-ecosystem:grpc-gateway:protoc-gen-grpc-gateway",
    99  				"proto_plugin", "protoc-gen-grpc-gateway option generate_unbound_methods=true",
   100  			),
   101  			PluginName: "protoc-gen-grpc-gateway",
   102  			Configuration: plugintest.WithConfiguration(
   103  				plugintest.WithLabel(t, "@build_stack_rules_proto//plugin/grpc-ecosystem/grpc-gateway:protoc-gen-grpc-gateway"),
   104  				plugintest.WithOptions("generate_unbound_methods=true"),
   105  				plugintest.WithOutputs("test.pb.gw.go"),
   106  			),
   107  			SkipIntegration: true,
   108  		},
   109  	})
   110  }