go.uber.org/yarpc@v1.72.1/internal/protoplugin/registry_test.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package protoplugin
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/gogo/protobuf/proto"
    27  	"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestGoPackageName(t *testing.T) {
    32  	tests := []struct {
    33  		msg           string
    34  		protoFileName *string
    35  		protoPackage  *string
    36  		goPackage     *string
    37  		expectedName  string
    38  	}{
    39  		{
    40  			msg:          "short go_package",
    41  			goPackage:    proto.String("package"),
    42  			expectedName: "package",
    43  		},
    44  		{
    45  			msg:          "full go_package",
    46  			goPackage:    proto.String("path/to/package"),
    47  			expectedName: "package",
    48  		},
    49  		{
    50  			msg:          "full go_package with alias",
    51  			goPackage:    proto.String("path/to/package;alias"),
    52  			expectedName: "alias",
    53  		},
    54  		{
    55  			msg:          "proto package",
    56  			protoPackage: proto.String("proto.package"),
    57  			expectedName: "proto_package",
    58  		},
    59  		{
    60  			msg:           "proto file name",
    61  			protoFileName: proto.String("path/filename.proto"),
    62  			expectedName:  "filename",
    63  		},
    64  		{
    65  			msg:          "prefer go_package over proto package",
    66  			goPackage:    proto.String("path/to/package1"),
    67  			protoPackage: proto.String("proto.package2"),
    68  			expectedName: "package1",
    69  		},
    70  		{
    71  			msg:           "prefer proto package over proto file name",
    72  			protoPackage:  proto.String("proto.package"),
    73  			protoFileName: proto.String("filename.proto"),
    74  			expectedName:  "proto_package",
    75  		},
    76  	}
    77  
    78  	for _, test := range tests {
    79  		t.Run(test.msg, func(t *testing.T) {
    80  			proto := &descriptor.FileDescriptorProto{
    81  				Name:    test.protoFileName,
    82  				Package: test.protoPackage,
    83  				Options: &descriptor.FileOptions{
    84  					GoPackage: test.goPackage,
    85  				},
    86  			}
    87  			assert.Equal(t, test.expectedName, defaultGoPackageName(proto))
    88  		})
    89  	}
    90  }