dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/utils/grpcutil/regex_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  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   *
    20   * Copyright 2021 gRPC authors.
    21   *
    22   */
    23  
    24  package grpcutil
    25  
    26  import (
    27  	"regexp"
    28  	"testing"
    29  )
    30  
    31  func TestFullMatchWithRegex(t *testing.T) {
    32  	tests := []struct {
    33  		name     string
    34  		regexStr string
    35  		string   string
    36  		want     bool
    37  	}{
    38  		{
    39  			name:     "not match because only partial",
    40  			regexStr: "^a+$",
    41  			string:   "ab",
    42  			want:     false,
    43  		},
    44  		{
    45  			name:     "match because fully match",
    46  			regexStr: "^a+$",
    47  			string:   "aa",
    48  			want:     true,
    49  		},
    50  		{
    51  			name:     "longest",
    52  			regexStr: "a(|b)",
    53  			string:   "ab",
    54  			want:     true,
    55  		},
    56  		{
    57  			name:     "match all",
    58  			regexStr: ".*",
    59  			string:   "",
    60  			want:     true,
    61  		},
    62  		{
    63  			name:     "matches non-empty strings",
    64  			regexStr: ".+",
    65  			string:   "",
    66  			want:     false,
    67  		},
    68  	}
    69  	for _, tt := range tests {
    70  		t.Run(tt.name, func(t *testing.T) {
    71  			hrm := regexp.MustCompile(tt.regexStr)
    72  			if got := FullMatchWithRegex(hrm, tt.string); got != tt.want {
    73  				t.Errorf("match() = %v, want %v", got, tt.want)
    74  			}
    75  		})
    76  	}
    77  }