Update package labels tests for new labels.Labels type
Re-did the FromStrings test to avoid assumptions about how it works.
@@ -36,10 +36,6 @@ func TestLabels_String(t *testing.T) {
lables: Labels{},
expected: "{}",
},
- {
- lables: nil,
- expected: "{}",
- },
}
for _, c := range cases {
str := c.lables.String()
@@ -316,18 +312,18 @@ func TestLabels_Equal(t *testing.T) {
func TestLabels_FromStrings(t *testing.T) {
labels := FromStrings("aaa", "111", "bbb", "222")
- expected := Labels{
- {
- Name: "aaa",
- Value: "111",
- },
- {
- Name: "bbb",
- Value: "222",
- },
- }
-
- require.Equal(t, expected, labels, "unexpected labelset")
+ x := 0
+ labels.Range(func(l Label) {
+ switch x {
+ case 0:
+ require.Equal(t, Label{Name: "aaa", Value: "111"}, l, "unexpected value")
+ case 1:
+ require.Equal(t, Label{Name: "bbb", Value: "222"}, l, "unexpected value")
+ default:
+ t.Fatalf("unexpected labelset value %d: %v", x, l)
+ }
+ x++
+ })
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"),
},
{
- base: nil,
set: []Label{{"aaa", "111"}, {"bbb", "222"}, {"ccc", "333"}},
del: []string{"bbb"},
want: FromStrings("aaa", "111", "ccc", "333"),
@@ -604,8 +599,7 @@ func TestBuilder(t *testing.T) {
func TestLabels_Hash(t *testing.T) {
lbls := FromStrings("foo", "bar", "baz", "qux")
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(), Labels{lbls[0]}.Hash(), "different labels match.")
+ require.NotEqual(t, lbls.Hash(), FromStrings("foo", "bar").Hash(), "different labels match.")
}
var benchmarkLabelsResult uint64
@@ -623,7 +617,7 @@ func BenchmarkLabels_Hash(b *testing.B) {
// Label ~20B name, 50B value.
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.
b.Set(fmt.Sprintf("abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij%d", i), fmt.Sprintf("abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij%d", i))
}
- return b.Labels(nil)
+ return b.Labels(EmptyLabels())
}(),
},
{
