Skip to content

Commit

Permalink
#102 Change SkipLast raises panic if count < 0
Browse files Browse the repository at this point in the history
  • Loading branch information
meian committed Feb 8, 2022
1 parent c7abc80 commit ab9a39b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
8 changes: 6 additions & 2 deletions skip_last.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ type skipLastIterator[T any] struct {
// itb := gcf.FromSlice([]{1, 2, 3})
// itb = gcf.SkipLast(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 SkipLast[T any](itb Iterable[T], count int) Iterable[T] {
if count < 0 {
panic("count for SkipLast must not be negative.")
}
if isEmpty(itb) {
return orEmpty(itb)
}
if count < 1 {
if count == 0 {
return itb
}
return &skipLastIterable[T]{
Expand Down
23 changes: 19 additions & 4 deletions skip_last_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ func TestSkipLast(t *testing.T) {
count int
}
tests := []struct {
name string
args args
want []int
name string
args args
want []int
wantPanic bool
}{
{
name: "skip last 1 from slice 3",
Expand Down Expand Up @@ -64,7 +65,7 @@ func TestSkipLast(t *testing.T) {
itb: gcf.FromSlice([]int{1, 2, 3}),
count: -1,
},
want: []int{1, 2, 3},
wantPanic: true,
},
{
name: "nil Iterable",
Expand All @@ -74,9 +75,23 @@ func TestSkipLast(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.SkipLast(tt.args.itb, tt.args.count)
})
return
}
itb := gcf.SkipLast(tt.args.itb, tt.args.count)
s := gcf.ToSlice(itb)
assert.Equal(t, tt.want, s)
Expand Down

0 comments on commit ab9a39b

Please sign in to comment.