github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/internal/grpcutil/regex_test.go (about) 1 /* 2 * 3 * Copyright 2021 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package grpcutil 20 21 import ( 22 "regexp" 23 "testing" 24 ) 25 26 func TestFullMatchWithRegex(t *testing.T) { 27 tests := []struct { 28 name string 29 regexStr string 30 string string 31 want bool 32 }{ 33 { 34 name: "not match because only partial", 35 regexStr: "^a+$", 36 string: "ab", 37 want: false, 38 }, 39 { 40 name: "match because fully match", 41 regexStr: "^a+$", 42 string: "aa", 43 want: true, 44 }, 45 { 46 name: "longest", 47 regexStr: "a(|b)", 48 string: "ab", 49 want: true, 50 }, 51 { 52 name: "match all", 53 regexStr: ".*", 54 string: "", 55 want: true, 56 }, 57 { 58 name: "matches non-empty strings", 59 regexStr: ".+", 60 string: "", 61 want: false, 62 }, 63 } 64 for _, tt := range tests { 65 t.Run(tt.name, func(t *testing.T) { 66 hrm := regexp.MustCompile(tt.regexStr) 67 if got := FullMatchWithRegex(hrm, tt.string); got != tt.want { 68 t.Errorf("match() = %v, want %v", got, tt.want) 69 } 70 }) 71 } 72 }