github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/matcher_test.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package instana_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/instana/testify/assert"
    10  	"github.com/instana/testify/require"
    11  	instana "github.com/mier85/go-sensor"
    12  )
    13  
    14  func TestNamedMatcher(t *testing.T) {
    15  	examples := map[string]struct {
    16  		List               []string
    17  		MatchingStrings    []string
    18  		NonMatchingStrings []string
    19  	}{
    20  		"equals": {
    21  			List:               []string{"foo", "bar"},
    22  			MatchingStrings:    []string{"foo", "bar"},
    23  			NonMatchingStrings: []string{"Foo", "foobar", "baz"},
    24  		},
    25  		"equals-ignore-case": {
    26  			List:               []string{"foo", "bar"},
    27  			MatchingStrings:    []string{"foo", "bar", "Foo"},
    28  			NonMatchingStrings: []string{"foobar", "baz"},
    29  		},
    30  		"contains": {
    31  			List:               []string{"foo", "bar"},
    32  			MatchingStrings:    []string{"foo", "foobar", "Foobar"},
    33  			NonMatchingStrings: []string{"baz", "FooBar"},
    34  		},
    35  		"contains-ignore-case": {
    36  			List:               []string{"foo", "bar"},
    37  			MatchingStrings:    []string{"foo", "foobar", "Foobar", "FooBar"},
    38  			NonMatchingStrings: []string{"baz"},
    39  		},
    40  		"regex": {
    41  			List:               []string{"(?i)foo.+", "ba[rz]"},
    42  			MatchingStrings:    []string{"foobar", "Foobar", "FooBar", "baz"},
    43  			NonMatchingStrings: []string{"foo", "bap"},
    44  		},
    45  		"none": {
    46  			List:               []string{"foo", "bar"},
    47  			NonMatchingStrings: []string{"foo", "bar", "baz"},
    48  		},
    49  	}
    50  
    51  	for name, example := range examples {
    52  		t.Run(name, func(t *testing.T) {
    53  			m, err := instana.NamedMatcher(name, example.List)
    54  			require.NoError(t, err)
    55  
    56  			for _, s := range example.MatchingStrings {
    57  				t.Run(s, func(t *testing.T) {
    58  					assert.True(t, m.Match(s))
    59  				})
    60  			}
    61  
    62  			for _, s := range example.NonMatchingStrings {
    63  				t.Run(s, func(t *testing.T) {
    64  					assert.False(t, m.Match(s))
    65  				})
    66  			}
    67  		})
    68  	}
    69  }
    70  
    71  func TestNamedMatcher_Unsupported(t *testing.T) {
    72  	_, err := instana.NamedMatcher("custom", []string{"foo", "bar"})
    73  	assert.Error(t, err)
    74  }