Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
including empty.go
  • Loading branch information
mattcorby-eaglen committed Apr 5, 2023
commit d65cd7c4ac31849befc645736a743ff6d04e3442
31 changes: 26 additions & 5 deletions is/empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,34 @@ import (
"github.com/corbym/gocrest"
)

// Empty matches if the actual is "empty".
// 'string' values are empty if they are "", maps, arrays and slices are empty if len(actual) is 0.
// Empty matches if the actual array or slice is len(actual) is 0.
// Returns a matcher that evaluates true if actual is "empty".
func Empty[K comparable, T any, A string | []T | map[K]T]() *gocrest.Matcher[A] {
matcher := new(gocrest.Matcher[A])
func Empty[A any]() *gocrest.Matcher[[]A] {
matcher := new(gocrest.Matcher[[]A])
matcher.Describe = "empty value"
matcher.Matches = func(actual A) bool {
matcher.Matches = func(actual []A) bool {
return len(actual) == 0
}
return matcher
}

// EmptyString matches if the actual string if they are "" (==len(0))
// Returns a matcher that evaluates true if actual is "empty".
func EmptyString() *gocrest.Matcher[string] {
matcher := new(gocrest.Matcher[string])
matcher.Describe = "empty value"
matcher.Matches = func(actual string) bool {
return len(actual) == 0
}
return matcher
}

// EmptyMap matches if the actual maps if len(actual) is 0.
// Returns a matcher that evaluates true if actual is "empty".
func EmptyMap[K comparable, V any]() *gocrest.Matcher[map[K]V] {
matcher := new(gocrest.Matcher[map[K]V])
matcher.Describe = "empty value"
matcher.Matches = func(actual map[K]V) bool {
return len(actual) == 0
}
return matcher
Expand Down
14 changes: 7 additions & 7 deletions matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestEmptyStringIsEmptyPasses(testing *testing.T) {
}
for _, test := range equalsItems {
stubTestingT := new(StubTestingT)
then.AssertThat(stubTestingT, test.actual, is.Empty[string, string, string]())
then.AssertThat(stubTestingT, test.actual, is.EmptyString())
if stubTestingT.HasFailed() != test.shouldFail {
testing.Errorf("assertThat(%v, Empty()) gave unexpected test result (wanted failed %v, got failed %v)", test.actual, test.shouldFail, stubTestingT.HasFailed())
}
Expand All @@ -196,9 +196,9 @@ func TestEmptyMapIsEmptyPasses(testing *testing.T) {
}
for _, test := range equalsItems {
stubTestingT := new(StubTestingT)
then.AssertThat(stubTestingT, test.actual, is.Empty[string, bool, map[string]bool]())
then.AssertThat(stubTestingT, test.actual, is.EmptyMap[string, bool]())
if stubTestingT.HasFailed() != test.shouldFail {
testing.Errorf("assertThat(%v, Empty()) gave unexpected test result (wanted failed %v, got failed %v)", test.actual, test.shouldFail, stubTestingT.HasFailed())
testing.Errorf("assertThat(%v, EmptyMap()) gave unexpected test result (wanted failed %v, got failed %v)", test.actual, test.shouldFail, stubTestingT.HasFailed())
}
}
}
Expand All @@ -212,7 +212,7 @@ func TestEmptyArrayIsEmptyPasses(testing *testing.T) {
}
for _, test := range equalsItems {
stubTestingT := new(StubTestingT)
then.AssertThat(stubTestingT, test.actual, is.Empty[string, string, []string]())
then.AssertThat(stubTestingT, test.actual, is.Empty[string]())
if stubTestingT.HasFailed() != test.shouldFail {
testing.Errorf("assertThat(%v, Empty()) gave unexpected test result (wanted failed %v, got failed %v)", test.actual, test.shouldFail, stubTestingT.HasFailed())
}
Expand Down Expand Up @@ -791,7 +791,7 @@ func TestSizeMapMatcherDescription(t *testing.T) {
matcher *gocrest.Matcher[map[string]bool]
expected string
}{
{description: "Empty", actual: map[string]bool{"foo": true}, matcher: is.Empty[string, bool, map[string]bool](), expected: "empty value"},
{description: "Empty", actual: map[string]bool{"foo": true}, matcher: is.EmptyMap[string, bool](), expected: "empty value"},
{description: "HasKey", actual: map[string]bool{"hi": true}, matcher: has.Key[string, bool]("foo"), expected: "map has key 'foo'"},
{description: "HasKeys", actual: map[string]bool{"hi": true, "bye": false}, matcher: has.AllKeys[string, bool]("hi", "foo"), expected: "map has keys '[hi foo]'"},
}
Expand Down Expand Up @@ -856,7 +856,7 @@ func TestAllOfDescribesOnlyMismatches(testing *testing.T) {
then.AssertThat(stubTestingT, "abc", is.AllOf(
is.EqualTo("abc"),
is.StringContaining("e", "f"),
is.Empty[string, string, string](),
is.EmptyString(),
))
if !strings.Contains(stubTestingT.MockTestOutput, "Expected: something that contains [e f] and empty value\n") {
testing.Errorf("incorrect description:%s", stubTestingT.MockTestOutput)
Expand Down Expand Up @@ -1040,7 +1040,7 @@ func TestStructValues(t *testing.T) {
Id string
}{},
expected: has.StructMatchers[string]{
"Id": is.Empty[string, string, string](),
"Id": is.EmptyString(),
},
shouldFail: false,
},
Expand Down