Add unit tests in labels.go (#6921)

* Add unit test for func Equal in labels.go
* Add unit test for func Compare in labels.go

Signed-off-by: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
This commit is contained in:
Guangwen Feng 2020-03-03 22:17:54 +08:00 committed by GitHub
parent 2bf4952049
commit a128fc3534
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -254,3 +254,202 @@ func TestLabels_WithoutEmpty(t *testing.T) {
testutil.Equals(t, test.expected, got, "unexpected labelset for test case %d", i) testutil.Equals(t, test.expected, got, "unexpected labelset for test case %d", i)
} }
} }
func TestLabels_Equal(t *testing.T) {
labels := Labels{
{
Name: "aaa",
Value: "111",
},
{
Name: "bbb",
Value: "222",
},
}
tests := []struct {
compared Labels
expected bool
}{
{
compared: Labels{
{
Name: "aaa",
Value: "111",
},
{
Name: "bbb",
Value: "222",
},
{
Name: "ccc",
Value: "333",
},
},
expected: false,
},
{
compared: Labels{
{
Name: "aaa",
Value: "111",
},
{
Name: "bar",
Value: "222",
},
},
expected: false,
},
{
compared: Labels{
{
Name: "aaa",
Value: "111",
},
{
Name: "bbb",
Value: "233",
},
},
expected: false,
},
{
compared: Labels{
{
Name: "aaa",
Value: "111",
},
{
Name: "bbb",
Value: "222",
},
},
expected: true,
},
}
for i, test := range tests {
got := Equal(labels, test.compared)
testutil.Equals(t, test.expected, got, "unexpected comparison result for test case %d", i)
}
}
func TestLabels_Compare(t *testing.T) {
labels := Labels{
{
Name: "aaa",
Value: "111",
},
{
Name: "bbb",
Value: "222",
},
}
tests := []struct {
compared Labels
expected int
}{
{
compared: Labels{
{
Name: "aaa",
Value: "110",
},
{
Name: "bbb",
Value: "222",
},
},
expected: 1,
},
{
compared: Labels{
{
Name: "aaa",
Value: "111",
},
{
Name: "bbb",
Value: "233",
},
},
expected: -1,
},
{
compared: Labels{
{
Name: "aaa",
Value: "111",
},
{
Name: "bar",
Value: "222",
},
},
expected: 1,
},
{
compared: Labels{
{
Name: "aaa",
Value: "111",
},
{
Name: "bbc",
Value: "222",
},
},
expected: -1,
},
{
compared: Labels{
{
Name: "aaa",
Value: "111",
},
},
expected: 1,
},
{
compared: Labels{
{
Name: "aaa",
Value: "111",
},
{
Name: "bbb",
Value: "222",
},
{
Name: "ccc",
Value: "333",
},
{
Name: "ddd",
Value: "444",
},
},
expected: -2,
},
{
compared: Labels{
{
Name: "aaa",
Value: "111",
},
{
Name: "bbb",
Value: "222",
},
},
expected: 0,
},
}
for i, test := range tests {
got := Compare(labels, test.compared)
testutil.Equals(t, test.expected, got, "unexpected comparison result for test case %d", i)
}
}