Skip to content

[feature] add empty and length assertions for slice and map #3

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 1 commit into from
Aug 23, 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
36 changes: 36 additions & 0 deletions assert_containment.go → assert_collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ package goassert

import "testing"

func EmptySlice[T any](t *testing.T, s []T) {
t.Helper()

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) {
t.Helper()

length := len(s)
if length != expectedLength {
t.Errorf("Expected slice to have length of %d but got %d", expectedLength, length)
}
}

func SliceContains[K comparable](t *testing.T, s []K, element K) {
t.Helper()

Expand All @@ -28,6 +46,24 @@ func sliceContains[K comparable](s []K, element K) bool {
return false
}

func EmptyMap[K comparable, V any](t *testing.T, m map[K]V) {
t.Helper()

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) {
t.Helper()

length := len(m)
if length != expectedLength {
t.Errorf("Expected map to have length of %d but got %d", expectedLength, length)
}
}

func MapContains[K, V comparable](t *testing.T, m map[K]V, k K, v V) {
t.Helper()

Expand Down
105 changes: 105 additions & 0 deletions assert_containment_test.go → assert_collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,54 @@ package goassert

import "testing"

func Test_EmptySliceShouldPass_GivenEmptySlice(t *testing.T) {
tester := new(testing.T)

slice := make([]int, 0)

EmptySlice(tester, slice)

if tester.Failed() {
t.Error("EmptySlice did not pass when empty slice was given")
}
}

func Test_EmptySliceShouldFail_GivenNonEmptySlice(t *testing.T) {
tester := new(testing.T)

slice := []int{10, 3, 5}

EmptySlice(tester, slice)

if !tester.Failed() {
t.Error("EmptySlice did not fail when non-empty slice was given")
}
}

func Test_SliceLengthShouldPass_IfGivenSliceLengthMatchesGivenLength(t *testing.T) {
tester := new(testing.T)

slice := []int{10, 3, 5}

SliceLength(tester, slice, 3)

if tester.Failed() {
t.Error("SliceLength did not pass when given slice's length matches the expected length")
}
}

func Test_SliceLengthShouldFail_IfGivenSliceLengthDoesNotMatchGivenLength(t *testing.T) {
tester := new(testing.T)

slice := []int{10, 3, 5}

SliceLength(tester, slice, 5)

if !tester.Failed() {
t.Error("SliceLength did not fail when given slice's length does not match the expected length")
}
}

func Test_SliceContainsShouldPass_GivenElementInGivenSlice(t *testing.T) {
tester := new(testing.T)

Expand Down Expand Up @@ -54,6 +102,63 @@ func Test_SliceNotContainsShouldFail_GivenElementInGivenSlice(t *testing.T) {
}
}

func Test_EmptyMapShouldPass_GivenEmptyMap(t *testing.T) {
tester := new(testing.T)

m := make(map[int]int, 0)

EmptyMap(tester, m)

if tester.Failed() {
t.Error("EmptyMap did not pass given empty map")
}
}

func Test_EmptyMapShouldFail_GivenNonEmptyMap(t *testing.T) {
tester := new(testing.T)

m := map[int]int{
10: 10,
5: 5,
}

EmptyMap(tester, m)

if !tester.Failed() {
t.Error("EmptyMap did not fail given non-empty map")
}
}

func Test_MapLengthShouldPass_IfGivenMapLengthMatchesGivenLength(t *testing.T) {
tester := new(testing.T)

m := map[int]int{
10: 10,
5: 5,
}

MapLength(tester, m, 2)

if tester.Failed() {
t.Error("MapLength did not pass when given map's length matches expected length")
}
}

func Test_MapLengthShouldFail_IfGivenMapLengthDoesNotMatchGivenLength(t *testing.T) {
tester := new(testing.T)

m := map[int]int{
10: 10,
5: 5,
}

MapLength(tester, m, 10)

if !tester.Failed() {
t.Error("MapLength did not fail when given map's length does not match expected length")
}
}

func Test_MapContainsShouldPass_GivenKeyValuePairInGivenMap(t *testing.T) {
tester := new(testing.T)

Expand Down