From f1e92e437cc5110f634fc0132af9397825fda0f0 Mon Sep 17 00:00:00 2001 From: kitamin <11195207+meian@users.noreply.github.com> Date: Sat, 5 Feb 2022 15:45:47 +0900 Subject: [PATCH 1/2] #86 Rename test empty func --- common_test.go | 14 ++++++++------ concat_test.go | 4 ++-- distinct_test.go | 2 +- filter_test.go | 2 +- map_test.go | 4 ++-- range_test.go | 4 ++-- repeat_test.go | 8 ++++---- reverse_test.go | 2 +- skip_test.go | 4 ++-- slice_test.go | 4 ++-- sort_test.go | 6 +++--- take_last_test.go | 2 +- take_test.go | 4 ++-- 13 files changed, 31 insertions(+), 29 deletions(-) diff --git a/common_test.go b/common_test.go index 714229c..e6137e0 100644 --- a/common_test.go +++ b/common_test.go @@ -25,13 +25,15 @@ 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. +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)) }) } diff --git a/concat_test.go b/concat_test.go index 726629a..2c64124 100644 --- a/concat_test.go +++ b/concat_test.go @@ -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) }) } diff --git a/distinct_test.go b/distinct_test.go index 930c4be..73dfbf4 100644 --- a/distinct_test.go +++ b/distinct_test.go @@ -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) }) } diff --git a/filter_test.go b/filter_test.go index 9d00697..aac9ca7 100644 --- a/filter_test.go +++ b/filter_test.go @@ -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 }) }) } diff --git a/map_test.go b/map_test.go index 2270369..faea86f 100644 --- a/map_test.go +++ b/map_test.go @@ -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 }) }) } @@ -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} }) }) } diff --git a/range_test.go b/range_test.go index ebf4b36..de2e06f 100644 --- a/range_test.go +++ b/range_test.go @@ -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 }) diff --git a/repeat_test.go b/repeat_test.go index fe91431..1c02c4a 100644 --- a/repeat_test.go +++ b/repeat_test.go @@ -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) }) } @@ -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) }) } diff --git a/reverse_test.go b/reverse_test.go index b8830ff..74396f8 100644 --- a/reverse_test.go +++ b/reverse_test.go @@ -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) }) } diff --git a/skip_test.go b/skip_test.go index 2e264ad..05479da 100644 --- a/skip_test.go +++ b/skip_test.go @@ -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) }) } @@ -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 }) }) } diff --git a/slice_test.go b/slice_test.go index f2e80b6..8ccf423 100644 --- a/slice_test.go +++ b/slice_test.go @@ -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{}) }) } @@ -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{}) }) } diff --git a/sort_test.go b/sort_test.go index e62af39..e3faa9b 100644 --- a/sort_test.go +++ b/sort_test.go @@ -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) }) } @@ -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) }) } @@ -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 }) }) } diff --git a/take_last_test.go b/take_last_test.go index 572bdd5..dcef15e 100644 --- a/take_last_test.go +++ b/take_last_test.go @@ -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) }) } diff --git a/take_test.go b/take_test.go index 761bd85..f68d69b 100644 --- a/take_test.go +++ b/take_test.go @@ -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) }) } @@ -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 }) }) } From bca00083853934da0001512f4247b96801b51c29 Mon Sep 17 00:00:00 2001 From: kitamin <11195207+meian@users.noreply.github.com> Date: Sat, 5 Feb 2022 15:47:09 +0900 Subject: [PATCH 2/2] #86 Add empty test case --- common_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/common_test.go b/common_test.go index e6137e0..ef34e7f 100644 --- a/common_test.go +++ b/common_test.go @@ -29,6 +29,8 @@ func testBeforeAndAfter[T any](t *testing.T, itb gcf.Iterable[T]) { // // - 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() t.Run("emptyIterable chaining", func(t *testing.T) { @@ -36,4 +38,17 @@ func testEmpties(t *testing.T, f func(itb gcf.Iterable[int]) gcf.Iterable[int]) 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) + }) }