Add test for label.go

This commit is contained in:
Theresa Gresch 2020-01-20 18:20:18 +01:00
parent c2386e958c
commit ff2014a29e

View file

@ -79,4 +79,84 @@ func TestLabels_Title(t *testing.T) {
assert.Equal(t, "fallback", labels.Title("fallback"))
})
t.Run("empty fallback", func(t *testing.T) {
cat := Label{Name: "cat", Source: "location", Uncertainty: 80, Priority: 5}
dog := Label{Name: "dog", Source: "location", Uncertainty: 80, Priority: 4}
labels := Labels{cat, dog}
assert.Equal(t, "", labels.Title(""))
})
t.Run("empty labels", func(t *testing.T) {
labels := Labels{}
assert.Equal(t, "fallback", labels.Title("fallback"))
})
t.Run("priority < 0", func(t *testing.T) {
cat := Label{Name: "cat", Source: "location", Uncertainty: 61, Priority: -5}
dog := Label{Name: "dog", Source: "location", Uncertainty: 10, Priority: -4}
labels := Labels{cat, dog}
assert.Equal(t, "fallback", labels.Title("fallback"))
})
t.Run("priority == 0", func(t *testing.T) {
cat := Label{Name: "cat", Source: "location", Uncertainty: 60, Priority: 0}
dog := Label{Name: "dog", Source: "location", Uncertainty: 51, Priority: 0}
labels := Labels{cat, dog}
assert.Equal(t, "fallback", labels.Title("fallback"))
})
}
func TestLabels_Len(t *testing.T) {
t.Run("len = 2", func(t *testing.T) {
cat := Label{Name: "cat", Source: "location", Uncertainty: 59, Priority: 5}
dog := Label{Name: "dog", Source: "location", Uncertainty: 10, Priority: 4}
labels := Labels{cat, dog}
assert.Equal(t, 2, labels.Len())
})
}
func TestLabels_Swap(t *testing.T) {
t.Run("swap cat with dog", func(t *testing.T) {
cat := Label{Name: "cat", Source: "location", Uncertainty: 59, Priority: 5}
dog := Label{Name: "dog", Source: "location", Uncertainty: 10, Priority: 4}
labels := Labels{cat, dog}
assert.Equal(t, "dog", labels[1].Name)
labels.Swap(0, 1)
assert.Equal(t, "cat", labels[1].Name)
})
}
func TestLabels_Less(t *testing.T) {
t.Run("different priorities", func(t *testing.T) {
cat := Label{Name: "cat", Source: "location", Uncertainty: 59, Priority: 5}
dog := Label{Name: "dog", Source: "location", Uncertainty: 10, Priority: 4}
labels := Labels{cat, dog}
assert.Equal(t, "dog", labels[1].Name)
assert.True(t, labels.Less(0, 1))
assert.False(t, labels.Less(1, 0))
})
t.Run("equal priorities", func(t *testing.T) {
cat := Label{Name: "cat", Source: "location", Uncertainty: 59, Priority: 5}
dog := Label{Name: "dog", Source: "location", Uncertainty: 10, Priority: 5}
labels := Labels{cat, dog}
assert.Equal(t, "dog", labels[1].Name)
assert.False(t, labels.Less(0, 1))
assert.True(t, labels.Less(1, 0))
})
}
func TestLabels_Keywords(t *testing.T) {
cat := Label{Name: "cat", Source: "location", Uncertainty: 59, Priority: 5}
dog := Label{Name: "dog", Source: "location", Uncertainty: 10, Priority: 5}
labels := Labels{cat, dog}
result := labels.Keywords()
assert.Equal(t, "cat", result[0])
assert.Equal(t, "dog", result[1])
}