From 22b833d9720f2a308a116de3245a57546fb56582 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 28 Dec 2021 14:27:29 +0300 Subject: [PATCH] Panic if provided value is not a slice Signed-off-by: Evgenii Stratonikov --- hrw.go | 4 ++-- hrw_test.go | 11 ++--------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/hrw.go b/hrw.go index 6f6662c..07087df 100644 --- a/hrw.go +++ b/hrw.go @@ -109,7 +109,7 @@ func SortSliceByWeightIndex(slice interface{}, weights []float64, hash uint64) { func prepareRule(slice interface{}) []uint64 { t := reflect.TypeOf(slice) if t.Kind() != reflect.Slice { - return nil + panic("HRW sort expects slice, got " + t.Kind().String()) } var ( @@ -188,7 +188,7 @@ func prepareRule(slice interface{}) []uint64 { default: if _, ok := val.Index(0).Interface().(Hasher); !ok { - return nil + panic("slice elements must implement hrw.Hasher") } for i := 0; i < length; i++ { diff --git a/hrw_test.go b/hrw_test.go index bebee20..5173572 100644 --- a/hrw_test.go +++ b/hrw_test.go @@ -113,15 +113,13 @@ func TestSortSliceByValueFail(t *testing.T) { t.Run("must be slice", func(t *testing.T) { actual := 10 hash := Hash(testKey) - require.NotPanics(t, func() { SortSliceByValue(actual, hash) }) + require.Panics(t, func() { SortSliceByValue(actual, hash) }) }) t.Run("must 'fail' for unknown type", func(t *testing.T) { actual := []unknown{1, 2, 3, 4, 5} - expect := []unknown{1, 2, 3, 4, 5} hash := Hash(testKey) - SortSliceByValue(actual, hash) - require.Equal(t, expect, actual) + require.Panics(t, func() { SortSliceByValue(actual, hash) }) }) } @@ -175,11 +173,6 @@ func TestSortSliceByValueIntSlice(t *testing.T) { expect: []uint32{5, 1, 2, 0, 3, 4}, }, - { - actual: Uint32Slice{0, 1, 2, 3, 4, 5}, - expect: Uint32Slice{0, 1, 2, 3, 4, 5}, - }, - { actual: []int64{0, 1, 2, 3, 4, 5}, expect: []int64{5, 3, 0, 1, 4, 2},