Browse Source

Add unit test for Inverse in pkg/labels/matcher.go (#6586)

This PR is about adding a unit test for Inverse and updating the other test in pkg/labels/matcher.go.

Signed-off-by: Hu Shuai <hus.fnst@cn.fujitsu.com>
pull/6617/head
Hu Shuai 5 years ago committed by Chris Marchbanks
parent
commit
a7f16749b7
  1. 44
      pkg/labels/matcher_test.go

44
pkg/labels/matcher_test.go

@ -13,13 +13,15 @@
package labels package labels
import "testing" import (
"testing"
"github.com/prometheus/prometheus/util/testutil"
)
func mustNewMatcher(t *testing.T, mType MatchType, value string) *Matcher { func mustNewMatcher(t *testing.T, mType MatchType, value string) *Matcher {
m, err := NewMatcher(mType, "", value) m, err := NewMatcher(mType, "", value)
if err != nil { testutil.Ok(t, err)
t.Fatal(err)
}
return m return m
} }
@ -82,8 +84,36 @@ func TestMatcher(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
if test.matcher.Matches(test.value) != test.match { testutil.Equals(t, test.matcher.Matches(test.value), test.match)
t.Fatalf("Unexpected match result for matcher %v and value %q; want %v, got %v", test.matcher, test.value, test.match, !test.match) }
} }
func TestInverse(t *testing.T) {
tests := []struct {
matcher *Matcher
expected *Matcher
}{
{
matcher: &Matcher{Type: MatchEqual, Name: "name1", Value: "value1"},
expected: &Matcher{Type: MatchNotEqual, Name: "name1", Value: "value1"},
},
{
matcher: &Matcher{Type: MatchNotEqual, Name: "name2", Value: "value2"},
expected: &Matcher{Type: MatchEqual, Name: "name2", Value: "value2"},
},
{
matcher: &Matcher{Type: MatchRegexp, Name: "name3", Value: "value3"},
expected: &Matcher{Type: MatchNotRegexp, Name: "name3", Value: "value3"},
},
{
matcher: &Matcher{Type: MatchNotRegexp, Name: "name4", Value: "value4"},
expected: &Matcher{Type: MatchRegexp, Name: "name4", Value: "value4"},
},
}
for _, test := range tests {
result, err := test.matcher.Inverse()
testutil.Ok(t, err)
testutil.Equals(t, test.expected.Type, result.Type)
} }
} }

Loading…
Cancel
Save