github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/pathtools/path_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 pathtools 17 18 import "testing" 19 20 func TestHasPrefix(t *testing.T) { 21 for _, tc := range []struct { 22 desc, path, prefix string 23 want bool 24 }{ 25 { 26 desc: "empty_prefix", 27 path: "home/jr_hacker", 28 prefix: "", 29 want: true, 30 }, { 31 desc: "partial_prefix", 32 path: "home/jr_hacker", 33 prefix: "home", 34 want: true, 35 }, { 36 desc: "full_prefix", 37 path: "home/jr_hacker", 38 prefix: "home/jr_hacker", 39 want: true, 40 }, { 41 desc: "too_long", 42 path: "home", 43 prefix: "home/jr_hacker", 44 want: false, 45 }, { 46 desc: "partial_component", 47 path: "home/jr_hacker", 48 prefix: "home/jr_", 49 want: false, 50 }, { 51 desc: "trailing_slash_prefix", 52 path: "home/jr_hacker", 53 prefix: "home/", 54 want: true, 55 }, { 56 desc: "trailing_slash_path", 57 path: "home/jr_hacker/", 58 prefix: "home", 59 want: true, 60 }, 61 } { 62 t.Run(tc.desc, func(t *testing.T) { 63 if got := HasPrefix(tc.path, tc.prefix); got != tc.want { 64 t.Errorf("got %v ; want %v", got, tc.want) 65 } 66 }) 67 } 68 } 69 70 func TestTrimPrefix(t *testing.T) { 71 for _, tc := range []struct { 72 desc, path, prefix, want string 73 }{ 74 { 75 desc: "empty_prefix", 76 path: "home/jr_hacker", 77 prefix: "", 78 want: "home/jr_hacker", 79 }, { 80 desc: "partial_prefix", 81 path: "home/jr_hacker", 82 prefix: "home", 83 want: "jr_hacker", 84 }, { 85 desc: "full_prefix", 86 path: "home/jr_hacker", 87 prefix: "home/jr_hacker", 88 want: "", 89 }, { 90 desc: "partial_component", 91 path: "home/jr_hacker", 92 prefix: "home/jr_", 93 want: "home/jr_hacker", 94 }, { 95 desc: "trailing_slash_prefix", 96 path: "home/jr_hacker", 97 prefix: "home/", 98 want: "jr_hacker", 99 }, { 100 desc: "trailing_slash_path", 101 path: "home/jr_hacker/", 102 prefix: "home", 103 want: "jr_hacker", 104 }, 105 } { 106 t.Run(tc.desc, func(t *testing.T) { 107 if got := TrimPrefix(tc.path, tc.prefix); got != tc.want { 108 t.Errorf("got %q ; want %q", got, tc.want) 109 } 110 }) 111 } 112 }