github.com/googleapis/api-linter@v1.65.2/rules/aip0191/php_namespace_test.go (about)

     1  // Copyright 2019 Google LLC
     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  //     https://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  package aip0191
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/googleapis/api-linter/rules/internal/testutils"
    22  )
    23  
    24  func TestPhpNamespace(t *testing.T) {
    25  	for _, test := range []struct {
    26  		name         string
    27  		PhpNamespace string
    28  		problems     testutils.Problems
    29  	}{
    30  		{"Valid", `Google\\Example\\V1`, testutils.Problems{}},
    31  		{"ValidBeta", `Google\\Example\\V1beta1`, testutils.Problems{}},
    32  		{"ValidServiceNamespaceCase", `Google\\FooBar\\V1`, testutils.Problems{}},
    33  		{"InvalidBadChars", "Google:Example:V1", testutils.Problems{{Message: "Invalid characters"}}},
    34  		{"Invalid", `google\\example\\v1`, testutils.Problems{{
    35  			Suggestion: fmt.Sprintf("option php_namespace = %s;", `Google\\Example\\V1`),
    36  		}}},
    37  		{"InvalidVersion", `Google\\Example\\v1`, testutils.Problems{{
    38  			Suggestion: fmt.Sprintf("option php_namespace = %s;", `Google\\Example\\V1`),
    39  		}}},
    40  		{"InvalidBeta", `Google\\Example\\V1Beta1`, testutils.Problems{{
    41  			Suggestion: fmt.Sprintf("option php_namespace = %s;", `Google\\Example\\V1beta1`),
    42  		}}},
    43  		{"ValidServiceNamespaceCase", `Google\\Foobar\\V1`, testutils.Problems{{Message: "Case"}}},
    44  	} {
    45  		t.Run(test.name, func(t *testing.T) {
    46  			f := testutils.ParseProto3Tmpl(t, `
    47  				package google.example.v1;
    48  
    49  				option php_namespace = "{{.PhpNamespace}}";
    50  
    51  				service FooBar {}
    52  			`, test)
    53  			if diff := test.problems.SetDescriptor(f).Diff(phpNamespace.Lint(f)); diff != "" {
    54  				t.Errorf(diff)
    55  			}
    56  		})
    57  	}
    58  }