github.com/googleapis/api-linter@v1.65.2/lint/rule_name_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 lint 16 17 import ( 18 "testing" 19 ) 20 21 func TestRuleNameValid(t *testing.T) { 22 tests := []struct { 23 testName string 24 ruleName RuleName 25 }{ 26 {"Lower", "aip"}, 27 {"LowerNumber", "aip0121"}, 28 {"LowerNumberKebab", "aip-0121"}, 29 {"Namespaced", "aip::0121"}, 30 {"NamespacedHyphen", "core::aip-0121"}, 31 } 32 33 for _, test := range tests { 34 t.Run(test.testName, func(t *testing.T) { 35 if !test.ruleName.IsValid() { 36 t.Errorf("Rule name %q is invalid; want valid.", test.ruleName) 37 } 38 }) 39 } 40 } 41 42 func TestRuleNameInvalid(t *testing.T) { 43 tests := []struct { 44 testName string 45 ruleName RuleName 46 }{ 47 {"EmptyString", ""}, 48 {"TripleColon", "a:::b"}, 49 {"QuadrupleColon", "a::::b"}, 50 {"CapitalLetter", "A"}, 51 {"LeadingDoubleColon", "::my-rule"}, 52 {"TrailingDoubleColon", "my-namespace::"}, 53 {"LeadingHyphen", "-core::aip-0131"}, 54 {"LeadingSegmentHyphen", "core::-aip-0131"}, 55 {"OnlyHyphen", "-"}, 56 {"SingleColon", "core:aip-0131"}, 57 {"Underscore", "core::aip_0131"}, 58 {"CamelCase", "myRule"}, 59 {"PascalCase", "MyRule"}, 60 } 61 62 for _, test := range tests { 63 t.Run(test.testName, func(t *testing.T) { 64 if test.ruleName.IsValid() { 65 t.Errorf("Rule name %q is valid; want invalid.", test.ruleName) 66 } 67 }) 68 } 69 } 70 71 func TestNewRuleName(t *testing.T) { 72 tests := []struct { 73 testName string 74 aip int 75 name string 76 want string 77 }{ 78 {"ZeroPad", 131, "http-method", "core::0131::http-method"}, 79 } 80 for _, test := range tests { 81 t.Run(test.testName, func(t *testing.T) { 82 rn := NewRuleName(test.aip, test.name) 83 if got := string(rn); got != test.want { 84 t.Errorf("Got %q, expected %q.", got, test.want) 85 } 86 }) 87 } 88 } 89 90 func TestRuleName_HasPrefix(t *testing.T) { 91 tests := []struct { 92 r RuleName 93 prefix []string 94 hasPrefix bool 95 }{ 96 {"a::b::c", []string{"a", "b"}, true}, 97 {"a::b::c", []string{"a"}, true}, 98 {"a::b::c", []string{"a::b"}, true}, 99 {"a::b::c::d", []string{"a::b", "c"}, true}, 100 {"a::b::c", []string{"a::b::c"}, true}, 101 {"ab::b::c", []string{"a"}, false}, 102 } 103 104 for _, test := range tests { 105 if test.r.HasPrefix(test.prefix...) != test.hasPrefix { 106 t.Errorf( 107 "%q.HasPrefix(%v)=%t; want %t", 108 test.r, test.prefix, test.r.HasPrefix(test.prefix...), test.hasPrefix, 109 ) 110 } 111 } 112 }