Skip to content

Commit

Permalink
Merge pull request #88 from meian/86-add-test-with-0-element-but-not-…
Browse files Browse the repository at this point in the history
…empty

#86 Add test with 0 element but not empty
  • Loading branch information
meian authored Feb 5, 2022
2 parents d7eaa9d + bca0008 commit 397bc4b
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 29 deletions.
29 changes: 23 additions & 6 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,30 @@ func testBeforeAndAfter[T any](t *testing.T, itb gcf.Iterable[T]) {
})
}

// testEmptyChain tests Iterable is emptyIterable when chained from emptyIterable.
// On function called with emptyIterable should be emptyIterable.
func testEmptyChain(t *testing.T, f func(itb gcf.Iterable[int]) gcf.Iterable[int]) {
// testEmpties tests Iterable func by empty element case variations.
//
// - emptyIterable chaining
// - test any func chaining result from emptyIterable is emptyIterable or not.
// - no panic from empty
// - test that panic does not occurred when any func chaining from empty elements that are not emptyIterable.
func testEmpties(t *testing.T, f func(itb gcf.Iterable[int]) gcf.Iterable[int]) {
t.Helper()
itbe := gcf.FromSlice([]int{})
t.Run("is empty Iterable", func(t *testing.T) {
itb := f(itbe)
t.Run("emptyIterable chaining", func(t *testing.T) {
itb := gcf.FromSlice([]int{}) // returns emptyIterable
itb = f(itb)
assert.True(t, gcf.IsEmptyIterable(itb), "%v", gcf.ToSlice(itb))
})
t.Run("no panic from empty", func(t *testing.T) {
defer func() {
err := recover()
if err != nil {
t.Errorf("%v", err)
}
}()
// make empty elements but not emptyIterable.
itb := gcf.FromSlice([]int{1})
itb = gcf.Filter(itb, func(v int) bool { return false })
itb = f(itb)
_ = gcf.ToSlice(itb)
})
}
4 changes: 2 additions & 2 deletions concat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func TestConcat(t *testing.T) {
itb = gcf.Concat(itb, gcf.FromSlice([]int{4, 5, 6}))
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.Concat(itb, gcf.FromSlice([]int{}))
})
testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.Concat(itb, nil)
})
}
Expand Down
2 changes: 1 addition & 1 deletion distinct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestDistinct(t *testing.T) {
itb = gcf.Distinct(itb)
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.Distinct(itb)
})
}
Expand Down
2 changes: 1 addition & 1 deletion filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestFilter(t *testing.T) {
itb = gcf.Filter(itb, func(v int) bool { return v%2 == 0 })
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.Filter(itb, func(v int) bool { return true })
})
}
Expand Down
4 changes: 2 additions & 2 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestMap(t *testing.T) {
})
}

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.Map(itb, func(v int) int { return v })
})
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestFlatMap(t *testing.T) {
})
testBeforeAndAfter(t, itbs)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.FlatMap(itb, func(v int) []int { return []int{v, v, v} })
})
}
Expand Down
4 changes: 2 additions & 2 deletions range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ func TestRange(t *testing.T) {
itb, _ = gcf.Range(3, 1, -1)
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
itb, _ = gcf.Range(1, 0, 1)
return itb
})
testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
itb, _ = gcf.Range(1, 2, -1)
return itb
})
Expand Down
8 changes: 4 additions & 4 deletions repeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func TestRepeat(t *testing.T) {
itb := gcf.Repeat(1, 3)
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.Repeat(1, 0)
})
testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.Repeat(1, -1)
})
}
Expand Down Expand Up @@ -124,10 +124,10 @@ func TestRepeatIterable(t *testing.T) {
itb = gcf.RepeatIterable(itb, 2)
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.RepeatIterable(itb, 0)
})
testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.RepeatIterable(itb, -1)
})
}
Expand Down
2 changes: 1 addition & 1 deletion reverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestReverse(t *testing.T) {
itb = gcf.Reverse(itb)
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.Reverse(itb)
})
}
Expand Down
4 changes: 2 additions & 2 deletions skip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestSkip(t *testing.T) {
itb = gcf.Skip(itb, 2)
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.Skip(itb, 0)
})
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestSkipWhile(t *testing.T) {
itb = gcf.SkipWhile(itb, func(v int) bool { return v < 2 })
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.SkipWhile(itb, func(v int) bool { return true })
})
}
Expand Down
4 changes: 2 additions & 2 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestFromSlice_pointer(t *testing.T) {
itb := gcf.FromSlice([]*int{&i1, &i2, &i3})
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.FromSlice([]int{})
})
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestFromSliceImmutable(t *testing.T) {
itb := gcf.FromSliceImmutable([]int{1, 2, 3})
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.FromSliceImmutable([]int{})
})
}
Expand Down
6 changes: 3 additions & 3 deletions sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestSortAsc(t *testing.T) {
itb = gcf.SortAsc(itb)
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.SortAsc(itb)
})
}
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestSortDesc(t *testing.T) {
itb = gcf.SortDesc(itb)
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.SortDesc(itb)
})
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestSortBy(t *testing.T) {
itb = gcf.SortBy(itb, func(x, y data) bool { return x.v < y.v })
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.SortBy(itb, func(x, y int) bool { return true })
})
}
Expand Down
2 changes: 1 addition & 1 deletion take_last_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestTakeLast(t *testing.T) {
itb = gcf.TakeLast(itb, 2)
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.TakeLast(itb, 2)
})
}
Expand Down
4 changes: 2 additions & 2 deletions take_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestTake(t *testing.T) {
itb = gcf.Take(itb, 2)
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.Take(itb, 1)
})
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestTakeWhile(t *testing.T) {
itb = gcf.TakeWhile(itb, func(v int) bool { return v < 2 })
testBeforeAndAfter(t, itb)

testEmptyChain(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.TakeWhile(itb, func(v int) bool { return true })
})
}
Expand Down

0 comments on commit 397bc4b

Please sign in to comment.