From dfc8c21e87fe760e086fd175c6f586105ec6095b Mon Sep 17 00:00:00 2001 From: mattcorby-eaglen Date: Wed, 5 Apr 2023 16:54:04 +0100 Subject: [PATCH 1/5] attempt to fix/lessen some problems mentioned in issue #9 --- has/haslength.go | 49 +++++++++++++++++++------- is/contains.go | 2 +- is/greaterthan.go | 6 +++- is/greaterthanorequalto.go | 2 +- is/lessthan.go | 2 +- is/lessthanorequalto.go | 2 +- is/nil.go | 47 ++++++++++++++++++------- matcher_test.go | 70 ++++++++++++++++++++++---------------- 8 files changed, 121 insertions(+), 59 deletions(-) diff --git a/has/haslength.go b/has/haslength.go index d5fd30a..bbb45b5 100644 --- a/has/haslength.go +++ b/has/haslength.go @@ -5,13 +5,27 @@ import ( "github.com/corbym/gocrest" ) -// Length can be called with arrays and strings +const description = "value with length %v" + +// Length can be called with arrays +// Returns a matcher that matches if the length matches the given criteria +func Length[T any](expected int) *gocrest.Matcher[[]T] { + matcher := new(gocrest.Matcher[[]T]) + matcher.Describe = fmt.Sprintf(description, expected) + matcher.Matches = func(actual []T) bool { + lenOfActual := len(actual) + matcher.Actual = fmt.Sprintf("length was %d", lenOfActual) + return lenOfActual == expected + } + return matcher +} + +// StringLength can be called with strings // Returns a matcher that matches if the length matches the given criteria -func Length[V any, A []V | string](expected int) *gocrest.Matcher[A] { - const description = "value with length %v" - matcher := new(gocrest.Matcher[A]) +func StringLength(expected int) *gocrest.Matcher[string] { + matcher := new(gocrest.Matcher[string]) matcher.Describe = fmt.Sprintf(description, expected) - matcher.Matches = func(actual A) bool { + matcher.Matches = func(actual string) bool { lenOfActual := len(actual) matcher.Actual = fmt.Sprintf("length was %d", lenOfActual) return lenOfActual == expected @@ -22,7 +36,6 @@ func Length[V any, A []V | string](expected int) *gocrest.Matcher[A] { // MapLength can be called with maps // Returns a matcher that matches if the length matches the given criteria func MapLength[K comparable, V any](expected int) *gocrest.Matcher[map[K]V] { - const description = "value with length %v" matcher := new(gocrest.Matcher[map[K]V]) matcher.Describe = fmt.Sprintf(description, expected) matcher.Matches = func(actual map[K]V) bool { @@ -33,13 +46,26 @@ func MapLength[K comparable, V any](expected int) *gocrest.Matcher[map[K]V] { return matcher } -// LengthMatching can be called with arrays or strings +// LengthMatching can be called with arrays +// Returns a matcher that matches if the length matches matcher passed in +func LengthMatching[A any](expected *gocrest.Matcher[int]) *gocrest.Matcher[[]A] { + matcher := new(gocrest.Matcher[[]A]) + matcher.Describe = fmt.Sprintf(description, expected) + matcher.Matches = func(actual []A) bool { + lenOfActual := len(actual) + matcher.Actual = fmt.Sprintf("length was %d", lenOfActual) + return expected.Matches(lenOfActual) + + } + return matcher +} + +// StringLengthMatching can be called with arrays or strings // Returns a matcher that matches if the length matches matcher passed in -func LengthMatching[V any, A []V | string](expected *gocrest.Matcher[int]) *gocrest.Matcher[A] { - const description = "value with length %v" - matcher := new(gocrest.Matcher[A]) +func StringLengthMatching(expected *gocrest.Matcher[int]) *gocrest.Matcher[string] { + matcher := new(gocrest.Matcher[string]) matcher.Describe = fmt.Sprintf(description, expected) - matcher.Matches = func(actual A) bool { + matcher.Matches = func(actual string) bool { lenOfActual := len(actual) matcher.Actual = fmt.Sprintf("length was %d", lenOfActual) return expected.Matches(lenOfActual) @@ -51,7 +77,6 @@ func LengthMatching[V any, A []V | string](expected *gocrest.Matcher[int]) *gocr // MapLengthMatching can be called with maps // Returns a matcher that matches if the length matches the given matcher func MapLengthMatching[K comparable, V any](expected *gocrest.Matcher[int]) *gocrest.Matcher[map[K]V] { - const description = "value with length %v" matcher := new(gocrest.Matcher[map[K]V]) matcher.Describe = fmt.Sprintf(description, expected) matcher.Matches = func(actual map[K]V) bool { diff --git a/is/contains.go b/is/contains.go index 0557178..76e3f6c 100644 --- a/is/contains.go +++ b/is/contains.go @@ -143,7 +143,7 @@ func listContains[T comparable, A []T](expected A, actualValue A) bool { return len(contains) == len(expected) } func listMatches[T comparable](expected []*gocrest.Matcher[T], actualValue []T) bool { - contains := make(map[interface{}]bool) + contains := make(map[T]bool) for i := 0; i < len(expected); i++ { for y := 0; y < len(actualValue); y++ { exp := expected[i] diff --git a/is/greaterthan.go b/is/greaterthan.go index 3f97fea..beb0b18 100644 --- a/is/greaterthan.go +++ b/is/greaterthan.go @@ -5,10 +5,14 @@ import ( "github.com/corbym/gocrest" ) +type Comparable interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64 | ~uint | ~uint16 | ~uint32 | ~uint64 | ~string +} + // GreaterThan matcher compares two values that are numeric or string values, and when // called returns true if actual > expected. Strings are compared lexicographically with '>'. // Returns a matcher that checks if actual is greater than expected. -func GreaterThan[A int | int8 | int16 | int32 | int64 | float32 | float64 | uint | uint16 | uint32 | uint64 | string](expected A) *gocrest.Matcher[A] { +func GreaterThan[A Comparable](expected A) *gocrest.Matcher[A] { matcher := new(gocrest.Matcher[A]) matcher.Describe = fmt.Sprintf("value greater than <%v>", expected) matcher.Matches = func(actual A) bool { diff --git a/is/greaterthanorequalto.go b/is/greaterthanorequalto.go index 17f2018..f5c9c81 100644 --- a/is/greaterthanorequalto.go +++ b/is/greaterthanorequalto.go @@ -4,7 +4,7 @@ import "github.com/corbym/gocrest" // GreaterThanOrEqualTo is a shorthand matcher for anyOf(greaterThan(x), equalTo(x)) // Returns a matcher matching if actual >= expected (using deepEquals). -func GreaterThanOrEqualTo[A int | int8 | int16 | int32 | int64 | float32 | float64 | uint | uint16 | uint32 | uint64 | string](expected A) *gocrest.Matcher[A] { +func GreaterThanOrEqualTo[A Comparable](expected A) *gocrest.Matcher[A] { matcher := new(gocrest.Matcher[A]) matcher.Matches = func(actual A) bool { anyOf := AnyOf(GreaterThan(expected), EqualTo(expected)) diff --git a/is/lessthan.go b/is/lessthan.go index 3c8f9e2..1bd20ba 100644 --- a/is/lessthan.go +++ b/is/lessthan.go @@ -8,7 +8,7 @@ import ( // LessThan matcher compares two values that are numeric or string values, and when // called returns true if actual < expected. Strings are compared lexicographically with '<'. // Returns a matcher that checks if actual is greater than expected. -func LessThan[A int | int8 | int16 | int32 | int64 | float32 | float64 | uint | uint16 | uint32 | uint64 | string](expected A) *gocrest.Matcher[A] { +func LessThan[A Comparable](expected A) *gocrest.Matcher[A] { matcher := new(gocrest.Matcher[A]) matcher.Describe = fmt.Sprintf("value less than <%v>", expected) matcher.Matches = func(actual A) bool { diff --git a/is/lessthanorequalto.go b/is/lessthanorequalto.go index f9c66dc..42a3ee9 100644 --- a/is/lessthanorequalto.go +++ b/is/lessthanorequalto.go @@ -4,7 +4,7 @@ import "github.com/corbym/gocrest" // LessThanOrEqualTo is a short hand matcher for anyOf(lessThan(x), equalTo(x)) // Returns a matcher matching if actual <= expected (using deepEquals). -func LessThanOrEqualTo[A int | int8 | int16 | int32 | int64 | float32 | float64 | uint | uint16 | uint32 | uint64 | string](expected A) *gocrest.Matcher[A] { +func LessThanOrEqualTo[A Comparable](expected A) *gocrest.Matcher[A] { matcher := new(gocrest.Matcher[A]) matcher.Matches = func(actual A) bool { anyOf := AnyOf(LessThan(expected), EqualTo(expected)) diff --git a/is/nil.go b/is/nil.go index 04a6e69..f53715b 100644 --- a/is/nil.go +++ b/is/nil.go @@ -3,23 +3,44 @@ package is import ( "fmt" "github.com/corbym/gocrest" - "reflect" ) +var description = "value that is " + // Nil matches if the actual value is nil -func Nil[A any]() *gocrest.Matcher[A] { - match := new(gocrest.Matcher[A]) - match.Describe = "value that is " - match.Matches = func(actual A) bool { +func Nil() *gocrest.Matcher[error] { + match := new(gocrest.Matcher[error]) + match.Describe = description + match.Matches = func(actual error) bool { + match.Actual = fmt.Sprintf("%v", actual) + return actual == nil + } + return match +} +func NilArray[A any]() *gocrest.Matcher[[]A] { + match := new(gocrest.Matcher[[]A]) + match.Describe = description + match.Matches = func(actual []A) bool { + match.Actual = fmt.Sprintf("%v", actual) + return actual == nil + } + return match +} +func NilMap[K comparable, V any]() *gocrest.Matcher[map[K]V] { + match := new(gocrest.Matcher[map[K]V]) + match.Describe = description + match.Matches = func(actual map[K]V) bool { + match.Actual = fmt.Sprintf("%v", actual) + return actual == nil + } + return match +} +func NilPtr[T any]() *gocrest.Matcher[*T] { + match := new(gocrest.Matcher[*T]) + match.Describe = description + match.Matches = func(actual *T) bool { match.Actual = fmt.Sprintf("%v", actual) - if any(actual) == nil { - return true - } - switch reflect.TypeOf(actual).Kind() { - case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice: - return reflect.ValueOf(actual).IsNil() - } - return false // anything else is never nil (hopefully) + return actual == nil } return match } diff --git a/matcher_test.go b/matcher_test.go index b074af6..c0c7680 100644 --- a/matcher_test.go +++ b/matcher_test.go @@ -36,8 +36,18 @@ func TestNil(testing *testing.T) { }{ actual: nil, } - then.AssertThat(testing, values.actual, is.Nil[*string]()) + then.AssertThat(testing, values.actual, is.NilPtr[string]()) } + +func TestError(testing *testing.T) { + values := struct { + actual error + }{ + actual: nil, + } + then.AssertThat(testing, values.actual, is.Nil()) +} + func TestHasLengthStringMatchesOrNot(testing *testing.T) { var hasLengthItems = []struct { actual string @@ -51,8 +61,8 @@ func TestHasLengthStringMatchesOrNot(testing *testing.T) { } for _, test := range hasLengthItems { stubTestingT = new(StubTestingT) - then.AssertThat(testing, "a", has.Length[string, string](1)) - then.AssertThat(stubTestingT, test.actual, has.Length[string, string](test.expected)) + then.AssertThat(testing, "a", has.StringLength(1)) + then.AssertThat(stubTestingT, test.actual, has.StringLength(test.expected)) if stubTestingT.HasFailed() != test.shouldFail { testing.Errorf("assertThat(%v, has.Length(%v)) gave unexpected test result (wanted failed %v, got failed %v)", test.actual, test.expected, test.shouldFail, stubTestingT.HasFailed()) } @@ -93,7 +103,7 @@ func TestHasLengthArrayMatchesOrNot(testing *testing.T) { } for _, test := range hasLengthItems { stubTestingT = new(StubTestingT) - then.AssertThat(stubTestingT, test.actual, has.Length[int, []int](test.expected)) + then.AssertThat(stubTestingT, test.actual, has.Length[int](test.expected)) if stubTestingT.HasFailed() != test.shouldFail { testing.Errorf("assertThat(%v, has.Length(%v)) gave unexpected test result (wanted failed %v, got failed %v)", test.actual, test.expected, test.shouldFail, stubTestingT.HasFailed()) } @@ -112,7 +122,7 @@ func TestHasLengthMatchesArrayMatchesOrNot(testing *testing.T) { } for _, test := range hasLengthItems { stubTestingT = new(StubTestingT) - then.AssertThat(stubTestingT, test.actual, has.LengthMatching[int, []int](is.EqualTo(test.expected))) + then.AssertThat(stubTestingT, test.actual, has.LengthMatching[int](is.EqualTo(test.expected))) if stubTestingT.HasFailed() != test.shouldFail { testing.Errorf("assertThat(%v, has.Length(%v)) gave unexpected test result (wanted failed %v, got failed %v)", test.actual, test.expected, test.shouldFail, stubTestingT.HasFailed()) } @@ -132,7 +142,7 @@ func TestHasLengthArrayStringMatchesOrNot(testing *testing.T) { } for _, test := range hasLengthItems { stubTestingT = new(StubTestingT) - then.AssertThat(stubTestingT, test.actual, has.Length[string, []string](test.expected)) + then.AssertThat(stubTestingT, test.actual, has.Length[string](test.expected)) if stubTestingT.HasFailed() != test.shouldFail { testing.Errorf("assertThat(%v, has.Length(%v)) gave unexpected test result (wanted failed %v, got failed %v)", test.actual, test.expected, test.shouldFail, stubTestingT.HasFailed()) } @@ -255,7 +265,7 @@ func TestAssertThatTwoIntsValuesAreGreaterThanOrNot(testing *testing.T) { then.AssertThat(testing, int64(12), is.GreaterThan(int64(1))) } func TestAssertThatHasLengthFailsWithDescriptionTest(testing *testing.T) { - then.AssertThat(stubTestingT, "a", has.Length[string, string](2)) + then.AssertThat(stubTestingT, "a", has.StringLength(2)) if !strings.Contains(stubTestingT.MockTestOutput, "value with length 2") { testing.Errorf("did not get expected description, got: %s", stubTestingT.MockTestOutput) } @@ -431,33 +441,47 @@ func TestNotReturnsTheOppositeOfGivenMatcher(testing *testing.T) { } func TestNotReturnsTheSubMatcherActual(testing *testing.T) { - not := is.Not[string](has.Length[string, string](1)) + not := is.Not(has.StringLength(1)) not.Matches("a") then.AssertThat(testing, not.Actual, is.EqualTo("length was 1")) } func TestAnyofReturnsTheSubMatcherActual(testing *testing.T) { - anyOf := is.AnyOf[string](has.Length[string, string](1), is.EqualTo("a")) + anyOf := is.AnyOf(has.StringLength(1), is.EqualTo("a")) anyOf.Matches("a") then.AssertThat(testing, anyOf.Actual, is.EqualTo("actual length was 1")) } func TestAllofReturnsTheSubMatcherActual(testing *testing.T) { - anyOf := is.AllOf[string](has.Length[string, string](1), is.EqualTo("a")) + anyOf := is.AllOf(has.StringLength(1), is.EqualTo("a")) anyOf.Matches("a") then.AssertThat(testing, anyOf.Actual, is.EqualTo("actual length was 1")) } func TestIsNilMatches(testing *testing.T) { - then.AssertThat(testing, nil, is.Nil[any]()) + then.AssertThat(testing, nil, is.Nil()) } func TestIsNilFails(testing *testing.T) { var actual = 2 - then.AssertThat(stubTestingT, &actual, is.Nil[*int]()) + then.AssertThat(stubTestingT, &actual, is.NilPtr[int]()) + if !stubTestingT.HasFailed() { + testing.Fail() + } +} +func TestIsNilArrayFails(testing *testing.T) { + var actual = []int{1, 2} + then.AssertThat(stubTestingT, actual, is.NilArray[int]()) + if !stubTestingT.HasFailed() { + testing.Fail() + } +} +func TestIsNilMapFails(testing *testing.T) { + var actual = map[string]string{"a": "b"} + then.AssertThat(stubTestingT, actual, is.NilMap[string, string]()) if !stubTestingT.HasFailed() { testing.Fail() } @@ -813,7 +837,7 @@ func TestStringMatchersDescription(t *testing.T) { {description: "Prefix", actual: "blarney stone", matcher: has.Prefix("123"), expected: "value with prefix 123"}, {description: "AllOf", actual: "abc", matcher: is.AllOf(is.EqualTo("abc"), is.StringContaining("e", "f")), expected: "something that contains [e f]"}, {description: "AnyOf", actual: "abc", matcher: is.AnyOf(is.EqualTo("efg"), is.StringContaining("e")), expected: "any of (value equal to or something that contains [e])"}, - {description: "LengthOf Composed", actual: "a", matcher: has.LengthMatching[string, string](is.GreaterThan(2)), expected: "value with length value greater than <2>"}, + {description: "LengthOf Composed", actual: "a", matcher: has.StringLengthMatching(is.GreaterThan(2)), expected: "value with length value greater than <2>"}, {description: "EqualToIgnoringWhitespace", actual: "a b c", matcher: is.EqualToIgnoringWhitespace("b c d"), expected: "ignoring whitespace value equal to "}, } for _, test := range equalsItems { @@ -922,13 +946,13 @@ func TestTypeName(t *testing.T) { } } -func TestNilArrayInterface(t *testing.T) { +func TestNilError(t *testing.T) { actual := nilResponse() - then.AssertThat(t, actual, is.Nil[[]any]()) + then.AssertThat(t, actual, is.Nil()) } -func nilResponse() []any { +func nilResponse() error { return nil } @@ -1059,20 +1083,8 @@ func TestStructValues(t *testing.T) { } } -func TestStructValuesPanicsWithStringActual(t *testing.T) { - actual := "not a struct" - expected := has.StructMatchers[string]{ - "Id": is.Empty[string, string, string](), - } - defer func() { - recover := recover() - then.AssertThat(t, recover, is.Not(is.Nil[any]())) - }() - then.AssertThat(stubTestingT, actual, has.StructWithValues[string](expected)) -} - func TestConformsToStringer(t *testing.T) { - then.AssertThat(t, is.Nil[any]().String(), is.EqualTo("value that is ")) + then.AssertThat(t, is.Nil().String(), is.EqualTo("value that is ")) } type DelayedReader struct { From d65cd7c4ac31849befc645736a743ff6d04e3442 Mon Sep 17 00:00:00 2001 From: mattcorby-eaglen Date: Wed, 5 Apr 2023 17:05:14 +0100 Subject: [PATCH 2/5] including empty.go --- is/empty.go | 31 ++++++++++++++++++++++++++----- matcher_test.go | 14 +++++++------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/is/empty.go b/is/empty.go index 2f8ce2f..8dcc6f8 100644 --- a/is/empty.go +++ b/is/empty.go @@ -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 diff --git a/matcher_test.go b/matcher_test.go index c0c7680..f61ffc3 100644 --- a/matcher_test.go +++ b/matcher_test.go @@ -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()) } @@ -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()) } } } @@ -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()) } @@ -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]'"}, } @@ -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) @@ -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, }, From 2b58e8410502e5d75d21a63731035ee8af09ff03 Mon Sep 17 00:00:00 2001 From: mattcorby-eaglen Date: Wed, 5 Apr 2023 17:07:36 +0100 Subject: [PATCH 3/5] godoc added --- is/nil.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/is/nil.go b/is/nil.go index f53715b..a4ec93a 100644 --- a/is/nil.go +++ b/is/nil.go @@ -7,7 +7,7 @@ import ( var description = "value that is " -// Nil matches if the actual value is nil +// Nil matches if the actual error value is nil func Nil() *gocrest.Matcher[error] { match := new(gocrest.Matcher[error]) match.Describe = description @@ -17,6 +17,8 @@ func Nil() *gocrest.Matcher[error] { } return match } + +// NilArray matches if the actual array value is nil func NilArray[A any]() *gocrest.Matcher[[]A] { match := new(gocrest.Matcher[[]A]) match.Describe = description @@ -26,6 +28,8 @@ func NilArray[A any]() *gocrest.Matcher[[]A] { } return match } + +// NilMap matches if the actual map value is nil func NilMap[K comparable, V any]() *gocrest.Matcher[map[K]V] { match := new(gocrest.Matcher[map[K]V]) match.Describe = description @@ -35,6 +39,8 @@ func NilMap[K comparable, V any]() *gocrest.Matcher[map[K]V] { } return match } + +// NilPtr matches if the actual pointer to T is nil func NilPtr[T any]() *gocrest.Matcher[*T] { match := new(gocrest.Matcher[*T]) match.Describe = description From 9e328838687d8ef9ab9ea2c1ce3442de6945c97f Mon Sep 17 00:00:00 2001 From: mattcorby-eaglen Date: Tue, 11 Apr 2023 09:06:23 +0100 Subject: [PATCH 4/5] small clean up --- has/hasstructvalues.go | 4 ++-- is/contains.go | 2 +- matcher_test.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/has/hasstructvalues.go b/has/hasstructvalues.go index 273851d..07d9af2 100644 --- a/has/hasstructvalues.go +++ b/has/hasstructvalues.go @@ -13,8 +13,8 @@ type StructMatchers[A any] map[string]*gocrest.Matcher[A] // This method can be used to check single struct fields in different ways or omit checking some struct fields at all. // Will automatically de-reference pointers. // Panics if the actual value is not a struct. -// Panics if Structmatchers contains a key that can not be found in the actual struct. -// Panics if Structmatchers contains a key that is unexported. +// Panics if StructMatchers contains a key that can not be found in the actual struct. +// Panics if StructMatchers contains a key that is unexported. func StructWithValues[A any, B any](expects StructMatchers[B]) *gocrest.Matcher[A] { match := new(gocrest.Matcher[A]) match.Describe = fmt.Sprintf("struct values to match {%s}", describeStructMatchers(expects)) diff --git a/is/contains.go b/is/contains.go index 76e3f6c..50577f8 100644 --- a/is/contains.go +++ b/is/contains.go @@ -7,7 +7,7 @@ import ( ) // StringContaining finds if all x's are contained as value in y. -// Acts like "ContainsAll", all elements given must be present (or must match) in actual in the same order as the expected values. +// Acts like "ContainsAll", all elements given must be present. func StringContaining(expected ...string) *gocrest.Matcher[string] { match := new(gocrest.Matcher[string]) match.Describe = fmt.Sprintf("something that contains %v", expected) diff --git a/matcher_test.go b/matcher_test.go index f61ffc3..0f5f477 100644 --- a/matcher_test.go +++ b/matcher_test.go @@ -1160,7 +1160,7 @@ func TestCallingFunctionEventually(t *testing.T) { return a } then.WithinFiveSeconds(t, func(eventually gocrest.TestingT) { - then.AssertThat(eventually, by.Calling[string, string](function, "hi"), is.EqualTo("hi")) + then.AssertThat(eventually, by.Calling(function, "hi"), is.EqualTo("hi")) }) } func firstTestChannel() chan int { From a0129432961ca8a1e7fd867eec130b522fd21d8a Mon Sep 17 00:00:00 2001 From: mattcorby-eaglen Date: Fri, 6 Sep 2024 14:32:36 +0100 Subject: [PATCH 5/5] * Correct documentation * remove redundant code --- is/contains.go | 4 ++-- is/nil.go | 5 ----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/is/contains.go b/is/contains.go index 50577f8..3c7dcc0 100644 --- a/is/contains.go +++ b/is/contains.go @@ -67,7 +67,7 @@ func descriptionForMatchers[A any](expected []*gocrest.Matcher[A]) string { } // ArrayContaining finds if all x's are contained in y. -// Acts like "ContainsAll", all elements given must be present in actual in the same order as the expected values. +// Acts like "ContainsAll", all elements given must be present in actual. func ArrayContaining[A comparable](expected ...A) *gocrest.Matcher[[]A] { match := new(gocrest.Matcher[[]A]) match.Describe = fmt.Sprintf("something that contains %v", descriptionFor(expected)) @@ -78,7 +78,7 @@ func ArrayContaining[A comparable](expected ...A) *gocrest.Matcher[[]A] { } // ArrayMatching finds if all x's are matched in y. -// Acts like "ContainsAll", all elements given must be present in actual in the same order as the expected values. +// Acts like "ContainsAll", all elements given must be present in actual. func ArrayMatching[A comparable](expected ...*gocrest.Matcher[A]) *gocrest.Matcher[[]A] { match := new(gocrest.Matcher[[]A]) match.Describe = fmt.Sprintf("something that contains %v", descriptionFor(expected)) diff --git a/is/nil.go b/is/nil.go index a4ec93a..510c84a 100644 --- a/is/nil.go +++ b/is/nil.go @@ -1,7 +1,6 @@ package is import ( - "fmt" "github.com/corbym/gocrest" ) @@ -12,7 +11,6 @@ func Nil() *gocrest.Matcher[error] { match := new(gocrest.Matcher[error]) match.Describe = description match.Matches = func(actual error) bool { - match.Actual = fmt.Sprintf("%v", actual) return actual == nil } return match @@ -23,7 +21,6 @@ func NilArray[A any]() *gocrest.Matcher[[]A] { match := new(gocrest.Matcher[[]A]) match.Describe = description match.Matches = func(actual []A) bool { - match.Actual = fmt.Sprintf("%v", actual) return actual == nil } return match @@ -34,7 +31,6 @@ func NilMap[K comparable, V any]() *gocrest.Matcher[map[K]V] { match := new(gocrest.Matcher[map[K]V]) match.Describe = description match.Matches = func(actual map[K]V) bool { - match.Actual = fmt.Sprintf("%v", actual) return actual == nil } return match @@ -45,7 +41,6 @@ func NilPtr[T any]() *gocrest.Matcher[*T] { match := new(gocrest.Matcher[*T]) match.Describe = description match.Matches = func(actual *T) bool { - match.Actual = fmt.Sprintf("%v", actual) return actual == nil } return match