Update package labels tests for new labels.Labels type

Re-did the FromStrings test to avoid assumptions about how it works.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2022-05-26 14:58:06 +00:00
parent 617bee60f1
commit cbf432d2ac

View file

@ -36,10 +36,6 @@ func TestLabels_String(t *testing.T) {
lables: Labels{}, lables: Labels{},
expected: "{}", expected: "{}",
}, },
{
lables: nil,
expected: "{}",
},
} }
for _, c := range cases { for _, c := range cases {
str := c.lables.String() str := c.lables.String()
@ -316,18 +312,18 @@ func TestLabels_Equal(t *testing.T) {
func TestLabels_FromStrings(t *testing.T) { func TestLabels_FromStrings(t *testing.T) {
labels := FromStrings("aaa", "111", "bbb", "222") labels := FromStrings("aaa", "111", "bbb", "222")
expected := Labels{ x := 0
{ labels.Range(func(l Label) {
Name: "aaa", switch x {
Value: "111", case 0:
}, require.Equal(t, Label{Name: "aaa", Value: "111"}, l, "unexpected value")
{ case 1:
Name: "bbb", require.Equal(t, Label{Name: "bbb", Value: "222"}, l, "unexpected value")
Value: "222", default:
}, t.Fatalf("unexpected labelset value %d: %v", x, l)
} }
x++
require.Equal(t, expected, labels, "unexpected labelset") })
require.Panics(t, func() { FromStrings("aaa", "111", "bbb") }) //nolint:staticcheck // Ignore SA5012, error is intentional test. require.Panics(t, func() { FromStrings("aaa", "111", "bbb") }) //nolint:staticcheck // Ignore SA5012, error is intentional test.
} }
@ -539,7 +535,6 @@ func TestBuilder(t *testing.T) {
want: FromStrings("aaa", "111", "ccc", "333"), want: FromStrings("aaa", "111", "ccc", "333"),
}, },
{ {
base: nil,
set: []Label{{"aaa", "111"}, {"bbb", "222"}, {"ccc", "333"}}, set: []Label{{"aaa", "111"}, {"bbb", "222"}, {"ccc", "333"}},
del: []string{"bbb"}, del: []string{"bbb"},
want: FromStrings("aaa", "111", "ccc", "333"), want: FromStrings("aaa", "111", "ccc", "333"),
@ -604,8 +599,7 @@ func TestBuilder(t *testing.T) {
func TestLabels_Hash(t *testing.T) { func TestLabels_Hash(t *testing.T) {
lbls := FromStrings("foo", "bar", "baz", "qux") lbls := FromStrings("foo", "bar", "baz", "qux")
require.Equal(t, lbls.Hash(), lbls.Hash()) require.Equal(t, lbls.Hash(), lbls.Hash())
require.NotEqual(t, lbls.Hash(), Labels{lbls[1], lbls[0]}.Hash(), "unordered labels match.") require.NotEqual(t, lbls.Hash(), FromStrings("foo", "bar").Hash(), "different labels match.")
require.NotEqual(t, lbls.Hash(), Labels{lbls[0]}.Hash(), "different labels match.")
} }
var benchmarkLabelsResult uint64 var benchmarkLabelsResult uint64
@ -623,7 +617,7 @@ func BenchmarkLabels_Hash(b *testing.B) {
// Label ~20B name, 50B value. // Label ~20B name, 50B value.
b.Set(fmt.Sprintf("abcdefghijabcdefghijabcdefghij%d", i), fmt.Sprintf("abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij%d", i)) b.Set(fmt.Sprintf("abcdefghijabcdefghijabcdefghij%d", i), fmt.Sprintf("abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij%d", i))
} }
return b.Labels(nil) return b.Labels(EmptyLabels())
}(), }(),
}, },
{ {
@ -634,7 +628,7 @@ func BenchmarkLabels_Hash(b *testing.B) {
// Label ~50B name, 50B value. // Label ~50B name, 50B value.
b.Set(fmt.Sprintf("abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij%d", i), fmt.Sprintf("abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij%d", i)) b.Set(fmt.Sprintf("abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij%d", i), fmt.Sprintf("abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij%d", i))
} }
return b.Labels(nil) return b.Labels(EmptyLabels())
}(), }(),
}, },
{ {