github.com/wolfd/bazel-gazelle@v0.14.0/internal/language/proto/fileinfo_test.go (about)

     1  /* Copyright 2018 The Bazel Authors. All rights reserved.
     2  
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7     http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package proto
    17  
    18  import (
    19  	"io/ioutil"
    20  	"os"
    21  	"path/filepath"
    22  	"reflect"
    23  	"testing"
    24  )
    25  
    26  func TestProtoRegexpGroupNames(t *testing.T) {
    27  	names := protoRe.SubexpNames()
    28  	nameMap := map[string]int{
    29  		"import":  importSubexpIndex,
    30  		"package": packageSubexpIndex,
    31  		"optkey":  optkeySubexpIndex,
    32  		"optval":  optvalSubexpIndex,
    33  		"service": serviceSubexpIndex,
    34  	}
    35  	for name, index := range nameMap {
    36  		if names[index] != name {
    37  			t.Errorf("proto regexp subexp %d is %s ; want %s", index, names[index], name)
    38  		}
    39  	}
    40  	if len(names)-1 != len(nameMap) {
    41  		t.Errorf("proto regexp has %d groups ; want %d", len(names), len(nameMap))
    42  	}
    43  }
    44  
    45  func TestProtoFileInfo(t *testing.T) {
    46  	for _, tc := range []struct {
    47  		desc, name, proto string
    48  		want              FileInfo
    49  	}{
    50  		{
    51  			desc:  "empty",
    52  			name:  "empty^file.proto",
    53  			proto: "",
    54  			want:  FileInfo{},
    55  		}, {
    56  			desc:  "simple package",
    57  			name:  "package.proto",
    58  			proto: "package foo;",
    59  			want: FileInfo{
    60  				PackageName: "foo",
    61  			},
    62  		}, {
    63  			desc:  "full package",
    64  			name:  "full.proto",
    65  			proto: "package foo.bar.baz;",
    66  			want: FileInfo{
    67  				PackageName: "foo.bar.baz",
    68  			},
    69  		}, {
    70  			desc: "import simple",
    71  			name: "imp.proto",
    72  			proto: `import 'single.proto';
    73  import "double.proto";`,
    74  			want: FileInfo{
    75  				Imports: []string{"double.proto", "single.proto"},
    76  			},
    77  		}, {
    78  			desc: "import quote",
    79  			name: "quote.proto",
    80  			proto: `import '""\".proto"';
    81  import "'.proto";`,
    82  			want: FileInfo{
    83  				Imports: []string{"\"\"\".proto\"", "'.proto"},
    84  			},
    85  		}, {
    86  			desc:  "import escape",
    87  			name:  "escape.proto",
    88  			proto: `import '\n\012\x0a.proto';`,
    89  			want: FileInfo{
    90  				Imports: []string{"\n\n\n.proto"},
    91  			},
    92  		}, {
    93  			desc: "import two",
    94  			name: "two.proto",
    95  			proto: `import "first.proto";
    96  import "second.proto";`,
    97  			want: FileInfo{
    98  				Imports: []string{"first.proto", "second.proto"},
    99  			},
   100  		}, {
   101  			desc:  "go_package",
   102  			name:  "gopkg.proto",
   103  			proto: `option go_package = "github.com/example/project;projectpb";`,
   104  			want: FileInfo{
   105  				Options: []Option{{Key: "go_package", Value: "github.com/example/project;projectpb"}},
   106  			},
   107  		}, {
   108  			desc:  "service",
   109  			name:  "service.proto",
   110  			proto: `service ChatService {}`,
   111  			want: FileInfo{
   112  				HasServices: true,
   113  			},
   114  		},
   115  	} {
   116  		t.Run(tc.desc, func(t *testing.T) {
   117  			dir, err := ioutil.TempDir(os.Getenv("TEST_TEMPDIR"), "TestProtoFileinfo")
   118  			if err != nil {
   119  				t.Fatal(err)
   120  			}
   121  			defer os.RemoveAll(dir)
   122  			if err := ioutil.WriteFile(filepath.Join(dir, tc.name), []byte(tc.proto), 0600); err != nil {
   123  				t.Fatal(err)
   124  			}
   125  
   126  			got := protoFileInfo(dir, tc.name)
   127  
   128  			// Clear fields we don't care about for testing.
   129  			got = FileInfo{
   130  				PackageName: got.PackageName,
   131  				Imports:     got.Imports,
   132  				Options:     got.Options,
   133  				HasServices: got.HasServices,
   134  			}
   135  			if !reflect.DeepEqual(got, tc.want) {
   136  				t.Errorf("got %#v; want %#v", got, tc.want)
   137  			}
   138  		})
   139  	}
   140  }