github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/regexp/empty_matcher_test.go (about)

     1  // Copyright (c) 2023  Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package regexp
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestMatchesEmptyValue(t *testing.T) {
    30  	tests := []struct {
    31  		given string
    32  		want  bool
    33  	}{
    34  		{given: "", want: true},
    35  		{given: "x", want: false},
    36  		{given: ".*", want: true},
    37  		{given: ".+", want: false},
    38  		{given: "x*", want: true},
    39  		{given: "x+", want: false},
    40  		{given: "|", want: true},
    41  		{given: "\\|", want: false},
    42  		{given: "[|]", want: false},
    43  		{given: "(|)", want: true},
    44  		{given: "a|b", want: false},
    45  		{given: "a||b", want: true},
    46  		{given: "a|", want: true},
    47  		{given: "|a", want: true},
    48  		{given: "|a|", want: true},
    49  		{given: "a|b|c", want: false},
    50  		{given: "||a|||b||||c||||", want: true},
    51  		{given: "()", want: true},
    52  		{given: "()*", want: true},
    53  		{given: "()+", want: true},
    54  		{given: "(ab)", want: false},
    55  		{given: "(ab)+", want: false},
    56  		{given: "(a|b)", want: false},
    57  		{given: "(a||b)", want: true},
    58  		{given: "(a|)", want: true},
    59  		{given: "(|a)", want: true},
    60  		{given: "(\\|a)", want: false},
    61  		{given: "([|])", want: false},
    62  		{given: "([|]a)", want: false},
    63  		{given: "([|a])", want: false},
    64  		{given: ".*|a", want: true},
    65  		{given: ".+|a", want: false},
    66  		{given: ".*|.+", want: true},
    67  		{given: ".*|.*", want: true},
    68  		{given: ".+|.+", want: false},
    69  		{given: "a(|)", want: false},
    70  		{given: "a(|)b", want: false},
    71  		{given: "(|)(|)", want: true},
    72  		{given: "(|)a(|)", want: false},
    73  		{given: "(|).*(|)", want: true},
    74  		{given: "(|).+(|)", want: false},
    75  		{given: "\\d*", want: true},
    76  		{given: "\\d+", want: false},
    77  		{given: "(\\d*|a)", want: true},
    78  		{given: "(a|\\d*)", want: true},
    79  		{given: "(|\\d*)", want: true},
    80  		{given: "a(\\d*)", want: false},
    81  		{given: "(\\d*)a", want: false},
    82  	}
    83  	for _, tt := range tests {
    84  		t.Run(tt.given, func(t *testing.T) {
    85  			res, err := MatchesEmptyValue([]byte(tt.given))
    86  			require.NoError(t, err)
    87  			require.Equal(t, tt.want, res)
    88  		})
    89  	}
    90  }