diff --git a/skip.go b/skip.go index bf46ae5..5c3b077 100644 --- a/skip.go +++ b/skip.go @@ -17,12 +17,16 @@ type skipIterator[T any] struct { // itb := gcf.FromSlice([]{1, 2, 3}) // itb = gcf.Skip(itb, 2) // -// If count is 0 or negative, returns original Iterable. +// If count is 0, returns original Iterable. +// If count is negative, raises panic. func Skip[T any](itb Iterable[T], count int) Iterable[T] { + if count < 0 { + panic("count for Skip must not be negative.") + } if isEmpty(itb) { return orEmpty(itb) } - if count < 1 { + if count == 0 { return itb } return &skipIterable[T]{itb, count} diff --git a/skip_test.go b/skip_test.go index 05479da..d754608 100644 --- a/skip_test.go +++ b/skip_test.go @@ -14,9 +14,10 @@ func TestSkip(t *testing.T) { count int } tests := []struct { - name string - args args - want []int + name string + args args + want []int + wantPanic bool }{ { name: "skip 1 from slice 3", @@ -64,7 +65,7 @@ func TestSkip(t *testing.T) { itb: gcf.FromSlice([]int{1, 2, 3}), count: -1, }, - want: []int{1, 2, 3}, + wantPanic: true, }, { name: "nil Iterable", @@ -74,9 +75,23 @@ func TestSkip(t *testing.T) { }, want: []int{}, }, + { + name: "nil and negative", + args: args{ + itb: nil, + count: -1, + }, + wantPanic: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + if tt.wantPanic { + assert.Panics(t, func() { + _ = gcf.Skip(tt.args.itb, tt.args.count) + }) + return + } itb := gcf.Skip(tt.args.itb, tt.args.count) s := gcf.ToSlice(itb) assert.Equal(t, tt.want, s)