Skip to content

[feature] add not empty assertions #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions assert_collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@ package goassert

import "testing"

func EmptySlice[T any](t *testing.T, s []T) {
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)
}
}

func SliceLength[T any](t *testing.T, s []T, expectedLength int) {
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")
}
}

func SliceLength[T any](t testing.TB, s []T, expectedLength int) {
t.Helper()

length := len(s)
Expand All @@ -20,15 +38,15 @@ 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) {
t.Errorf("Element %v could not be found in the slice %v", element, s)
}
}

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) {
Expand All @@ -46,16 +64,34 @@ 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()

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)
}
}

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 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")
}
}

func MapLength[K comparable, V any](t testing.TB, m map[K]V, expectedLength int) {
t.Helper()

length := len(m)
Expand All @@ -64,7 +100,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]
Expand All @@ -79,7 +115,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]
Expand Down
91 changes: 91 additions & 0 deletions assert_collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -26,6 +36,40 @@ 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_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)

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)

Expand Down Expand Up @@ -114,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)

Expand All @@ -129,6 +183,43 @@ 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_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)

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)

Expand Down
8 changes: 4 additions & 4 deletions assert_panic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
)

func Panic(t *testing.T, underTest func()) {
func Panic(t testing.TB, underTest func()) {
t.Helper()

defer func() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down