From 912079396607053dd0cff851f3eba52f9be04acd Mon Sep 17 00:00:00 2001 From: Elbert Oh Date: Fri, 26 Aug 2022 00:01:06 -0400 Subject: [PATCH 1/2] Add NotEmpty assertions for slice and map --- assert_collections.go | 32 ++++++++++++++++++------ assert_collections_test.go | 51 ++++++++++++++++++++++++++++++++++++++ assert_panic.go | 8 +++--- 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/assert_collections.go b/assert_collections.go index 8caf13c..6af444f 100644 --- a/assert_collections.go +++ b/assert_collections.go @@ -2,7 +2,7 @@ package goassert import "testing" -func EmptySlice[T any](t *testing.T, s []T) { +func EmptySlice[T any](t testing.TB, s []T) { t.Helper() length := len(s) @@ -11,7 +11,15 @@ func EmptySlice[T any](t *testing.T, s []T) { } } -func SliceLength[T any](t *testing.T, s []T, expectedLength int) { +func NotEmptySlice[T any](t testing.TB, s []T) { + t.Helper() + + if len(s) == 0 { + t.Error("Expected non- empty slice but got empty slice") + } +} + +func SliceLength[T any](t testing.TB, s []T, expectedLength int) { t.Helper() length := len(s) @@ -20,7 +28,7 @@ func SliceLength[T any](t *testing.T, s []T, expectedLength int) { } } -func SliceContains[K comparable](t *testing.T, s []K, element K) { +func SliceContains[K comparable](t testing.TB, s []K, element K) { t.Helper() if !sliceContains(s, element) { @@ -28,7 +36,7 @@ func SliceContains[K comparable](t *testing.T, s []K, element K) { } } -func SliceNotContains[K comparable](t *testing.T, s []K, element K) { +func SliceNotContains[K comparable](t testing.TB, s []K, element K) { t.Helper() if sliceContains(s, element) { @@ -46,7 +54,7 @@ func sliceContains[K comparable](s []K, element K) bool { return false } -func EmptyMap[K comparable, V any](t *testing.T, m map[K]V) { +func EmptyMap[K comparable, V any](t testing.TB, m map[K]V) { t.Helper() length := len(m) @@ -55,7 +63,15 @@ func EmptyMap[K comparable, V any](t *testing.T, m map[K]V) { } } -func MapLength[K comparable, V any](t *testing.T, m map[K]V, expectedLength int) { +func NotEmptyMap[K comparable, V any](t testing.TB, m map[K]V) { + t.Helper() + + if len(m) == 0 { + t.Error("Expected non-empty map but got empty map") + } +} + +func MapLength[K comparable, V any](t testing.TB, m map[K]V, expectedLength int) { t.Helper() length := len(m) @@ -64,7 +80,7 @@ func MapLength[K comparable, V any](t *testing.T, m map[K]V, expectedLength int) } } -func MapContains[K, V comparable](t *testing.T, m map[K]V, k K, v V) { +func MapContains[K, V comparable](t testing.TB, m map[K]V, k K, v V) { t.Helper() actualValue, found := m[k] @@ -79,7 +95,7 @@ func MapContains[K, V comparable](t *testing.T, m map[K]V, k K, v V) { } } -func MapNotContains[K, V comparable](t *testing.T, m map[K]V, k K, v V) { +func MapNotContains[K, V comparable](t testing.TB, m map[K]V, k K, v V) { t.Helper() value, found := m[k] diff --git a/assert_collections_test.go b/assert_collections_test.go index dd2334b..2f13c8a 100644 --- a/assert_collections_test.go +++ b/assert_collections_test.go @@ -26,6 +26,30 @@ func Test_EmptySliceShouldFail_GivenNonEmptySlice(t *testing.T) { } } +func Test_NotEmptySliceShouldPass_GivenNonEmptySlice(t *testing.T) { + tester := new(testing.T) + + slice := []int{10, 16} + + NotEmptySlice(tester, slice) + + if tester.Failed() { + t.Error("NotEmptySlice did not pass when non-empty slice was given") + } +} + +func Test_NotEmptySliceShouldFail_GivenNonEmptySlice(t *testing.T) { + tester := new(testing.T) + + slice := make([]int, 0) + + NotEmptySlice(tester, slice) + + if !tester.Failed() { + t.Error("NotEmptySlice did not fail when empty slice was given") + } +} + func Test_SliceLengthShouldPass_IfGivenSliceLengthMatchesGivenLength(t *testing.T) { tester := new(testing.T) @@ -129,6 +153,33 @@ func Test_EmptyMapShouldFail_GivenNonEmptyMap(t *testing.T) { } } +func Test_NotEmptyMapShouldPass_GivenNonEmptyMap(t *testing.T) { + tester := new(testing.T) + + m := map[int]int{ + 10: 5, + 16: 16, + } + + NotEmptyMap(tester, m) + + if tester.Failed() { + t.Error("NotEmptyMap did not pass given non-empty map") + } +} + +func Test_NotEmptyMapShouldFail_GivenNonEmptyMap(t *testing.T) { + tester := new(testing.T) + + m := make(map[int]int, 0) + + NotEmptyMap(tester, m) + + if !tester.Failed() { + t.Error("NotEmptyMap did not fail given empty map") + } +} + func Test_MapLengthShouldPass_IfGivenMapLengthMatchesGivenLength(t *testing.T) { tester := new(testing.T) diff --git a/assert_panic.go b/assert_panic.go index 4366844..b85850a 100644 --- a/assert_panic.go +++ b/assert_panic.go @@ -4,7 +4,7 @@ import ( "testing" ) -func Panic(t *testing.T, underTest func()) { +func Panic(t testing.TB, underTest func()) { t.Helper() defer func() { @@ -16,7 +16,7 @@ func Panic(t *testing.T, underTest func()) { underTest() } -func NotPanic(t *testing.T, underTest func()) { +func NotPanic(t testing.TB, underTest func()) { t.Helper() defer func() { @@ -28,7 +28,7 @@ func NotPanic(t *testing.T, underTest func()) { underTest() } -func PanicWithError[K comparable](t *testing.T, expectedError K, underTest func()) { +func PanicWithError[K comparable](t testing.TB, expectedError K, underTest func()) { t.Helper() defer func() { @@ -53,7 +53,7 @@ func PanicWithError[K comparable](t *testing.T, expectedError K, underTest func( underTest() } -func NotPanicWithError[K comparable](t *testing.T, expectedError K, underTest func()) { +func NotPanicWithError[K comparable](t testing.TB, expectedError K, underTest func()) { t.Helper() defer func() { From 344ec4eae6e96b66bd692d623cf9ba3ca1505ccf Mon Sep 17 00:00:00 2001 From: Elbert Oh Date: Fri, 26 Aug 2022 22:45:38 -0400 Subject: [PATCH 2/2] update empty assertions fail when slice/map is nil --- assert_collections.go | 20 +++++++++++++++++ assert_collections_test.go | 44 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/assert_collections.go b/assert_collections.go index 6af444f..6996c63 100644 --- a/assert_collections.go +++ b/assert_collections.go @@ -5,6 +5,11 @@ import "testing" func EmptySlice[T any](t testing.TB, s []T) { t.Helper() + if s == nil { + t.Error("Expected empty slice but got nil") + return + } + length := len(s) if length != 0 { t.Errorf("Expected empty slice but got slice with length %d", length) @@ -14,6 +19,11 @@ func EmptySlice[T any](t testing.TB, s []T) { func NotEmptySlice[T any](t testing.TB, s []T) { t.Helper() + if s == nil { + t.Error("Expected empty slice but got nil") + return + } + if len(s) == 0 { t.Error("Expected non- empty slice but got empty slice") } @@ -57,6 +67,11 @@ func sliceContains[K comparable](s []K, element K) bool { func EmptyMap[K comparable, V any](t testing.TB, m map[K]V) { t.Helper() + if m == nil { + t.Error("Expected empty map but got nil") + return + } + length := len(m) if length != 0 { t.Errorf("Expected empty map but got map with length of %d", length) @@ -66,6 +81,11 @@ func EmptyMap[K comparable, V any](t testing.TB, m map[K]V) { func NotEmptyMap[K comparable, V any](t testing.TB, m map[K]V) { t.Helper() + if m == nil { + t.Error("Expected non-empty map but got nil") + return + } + if len(m) == 0 { t.Error("Expected non-empty map but got empty map") } diff --git a/assert_collections_test.go b/assert_collections_test.go index 2f13c8a..e416e20 100644 --- a/assert_collections_test.go +++ b/assert_collections_test.go @@ -14,6 +14,16 @@ func Test_EmptySliceShouldPass_GivenEmptySlice(t *testing.T) { } } +func Test_EmptySliceShouldFail_GivenNilSlice(t *testing.T) { + tester := new(testing.T) + + EmptySlice[int](tester, nil) + + if !tester.Failed() { + t.Error("EmptySlice did not fail when nil slice was given") + } +} + func Test_EmptySliceShouldFail_GivenNonEmptySlice(t *testing.T) { tester := new(testing.T) @@ -38,7 +48,17 @@ func Test_NotEmptySliceShouldPass_GivenNonEmptySlice(t *testing.T) { } } -func Test_NotEmptySliceShouldFail_GivenNonEmptySlice(t *testing.T) { +func Test_NotEmptySliceShouldFail_GivenNilSlice(t *testing.T) { + tester := new(testing.T) + + NotEmptySlice[string](tester, nil) + + if !tester.Failed() { + t.Error("NotEmptySlice did not fail when nil slice was given") + } +} + +func Test_NotEmptySliceShouldFail_GivenEmptySlice(t *testing.T) { tester := new(testing.T) slice := make([]int, 0) @@ -138,6 +158,16 @@ func Test_EmptyMapShouldPass_GivenEmptyMap(t *testing.T) { } } +func Test_EmptyMapShouldFail_GivenNilEmptyMap(t *testing.T) { + tester := new(testing.T) + + EmptyMap[int, int](tester, nil) + + if !tester.Failed() { + t.Error("EmptyMap did not fail given nil map") + } +} + func Test_EmptyMapShouldFail_GivenNonEmptyMap(t *testing.T) { tester := new(testing.T) @@ -168,7 +198,17 @@ func Test_NotEmptyMapShouldPass_GivenNonEmptyMap(t *testing.T) { } } -func Test_NotEmptyMapShouldFail_GivenNonEmptyMap(t *testing.T) { +func Test_NotEmptyMapShouldFail_GivenNilMap(t *testing.T) { + tester := new(testing.T) + + NotEmptyMap[int, string](tester, nil) + + if !tester.Failed() { + t.Error("NotEmptyMap did not fail given nil map") + } +} + +func Test_NotEmptyMapShouldFail_GivenEmptyMap(t *testing.T) { tester := new(testing.T) m := make(map[int]int, 0)