github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/strmatcher/matchergroup_substr_test.go (about)

     1  package strmatcher_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/v2fly/v2ray-core/v5/common"
     8  	. "github.com/v2fly/v2ray-core/v5/common/strmatcher"
     9  )
    10  
    11  func TestSubstrMatcherGroup(t *testing.T) {
    12  	patterns := []struct {
    13  		pattern string
    14  		mType   Type
    15  	}{
    16  		{
    17  			pattern: "apis",
    18  			mType:   Substr,
    19  		},
    20  		{
    21  			pattern: "google",
    22  			mType:   Substr,
    23  		},
    24  		{
    25  			pattern: "apis",
    26  			mType:   Substr,
    27  		},
    28  	}
    29  	cases := []struct {
    30  		input  string
    31  		output []uint32
    32  	}{
    33  		{
    34  			input:  "google.com",
    35  			output: []uint32{1},
    36  		},
    37  		{
    38  			input:  "apis.com",
    39  			output: []uint32{0, 2},
    40  		},
    41  		{
    42  			input:  "googleapis.com",
    43  			output: []uint32{1, 0, 2},
    44  		},
    45  		{
    46  			input:  "fonts.googleapis.com",
    47  			output: []uint32{1, 0, 2},
    48  		},
    49  		{
    50  			input:  "apis.googleapis.com",
    51  			output: []uint32{0, 2, 1, 0, 2},
    52  		},
    53  	}
    54  	matcherGroup := &SubstrMatcherGroup{}
    55  	for id, entry := range patterns {
    56  		matcher, err := entry.mType.New(entry.pattern)
    57  		common.Must(err)
    58  		common.Must(AddMatcherToGroup(matcherGroup, matcher, uint32(id)))
    59  	}
    60  	for _, test := range cases {
    61  		if r := matcherGroup.Match(test.input); !reflect.DeepEqual(r, test.output) {
    62  			t.Error("unexpected output: ", r, " for test case ", test)
    63  		}
    64  	}
    65  }