From 78cb5eb2c19d179b1183d68dbc5cbb1711c01778 Mon Sep 17 00:00:00 2001 From: "Hsing-Yu (David) Chen" Date: Fri, 30 Sep 2022 16:34:42 -0700 Subject: [PATCH] Document acceptable string-like types --- matchers.go | 18 +++++++++--------- matchers/contain_substring_matcher.go | 3 ++- matchers/have_prefix_matcher.go | 3 ++- matchers/have_suffix_matcher.go | 3 ++- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/matchers.go b/matchers.go index f9d9f2aad..4c247c557 100644 --- a/matchers.go +++ b/matchers.go @@ -193,9 +193,9 @@ func MatchRegexp(regexp string, args ...interface{}) types.GomegaMatcher { } } -// ContainSubstring succeeds if actual is a string or stringer that contains the -// passed-in substring. Optional arguments can be provided to construct the substring -// via fmt.Sprintf(). +// ContainSubstring succeeds if actual is a string, stringer, []bytes, or json.RawMessage +// that contains the passed-in substring. +// Optional arguments can be provided to construct the substring via fmt.Sprintf(). func ContainSubstring(substr string, args ...interface{}) types.GomegaMatcher { return &matchers.ContainSubstringMatcher{ Substr: substr, @@ -203,9 +203,9 @@ func ContainSubstring(substr string, args ...interface{}) types.GomegaMatcher { } } -// HavePrefix succeeds if actual is a string or stringer that contains the -// passed-in string as a prefix. Optional arguments can be provided to construct -// via fmt.Sprintf(). +// HavePrefix succeeds if actual is a string, stringer, []bytes, or json.RawMessage +// that contains the passed-in string as a prefix. +// Optional arguments can be provided to construct the substring via fmt.Sprintf(). func HavePrefix(prefix string, args ...interface{}) types.GomegaMatcher { return &matchers.HavePrefixMatcher{ Prefix: prefix, @@ -213,9 +213,9 @@ func HavePrefix(prefix string, args ...interface{}) types.GomegaMatcher { } } -// HaveSuffix succeeds if actual is a string or stringer that contains the -// passed-in string as a suffix. Optional arguments can be provided to construct -// via fmt.Sprintf(). +// HaveSuffix succeeds if actual is a string, stringer, []bytes, or json.RawMessage +// that contains the passed-in string as a suffix. +// Optional arguments can be provided to construct the substring via fmt.Sprintf(). func HaveSuffix(suffix string, args ...interface{}) types.GomegaMatcher { return &matchers.HaveSuffixMatcher{ Suffix: suffix, diff --git a/matchers/contain_substring_matcher.go b/matchers/contain_substring_matcher.go index e725f8c27..d61b772f7 100644 --- a/matchers/contain_substring_matcher.go +++ b/matchers/contain_substring_matcher.go @@ -17,7 +17,8 @@ type ContainSubstringMatcher struct { func (matcher *ContainSubstringMatcher) Match(actual interface{}) (success bool, err error) { actualString, ok := toString(actual) if !ok { - return false, fmt.Errorf("ContainSubstring matcher requires a string or stringer. Got:\n%s", format.Object(actual, 1)) + return false, fmt.Errorf("ContainSubstring matcher requires a string, stringer, []bytes, or json.RawMessage. "+ + "Got:\n%s", format.Object(actual, 1)) } return strings.Contains(actualString, matcher.stringToMatch()), nil diff --git a/matchers/have_prefix_matcher.go b/matchers/have_prefix_matcher.go index 1d8e80270..68fae4b0e 100644 --- a/matchers/have_prefix_matcher.go +++ b/matchers/have_prefix_matcher.go @@ -14,7 +14,8 @@ type HavePrefixMatcher struct { func (matcher *HavePrefixMatcher) Match(actual interface{}) (success bool, err error) { actualString, ok := toString(actual) if !ok { - return false, fmt.Errorf("HavePrefix matcher requires a string or stringer. Got:\n%s", format.Object(actual, 1)) + return false, fmt.Errorf("HavePrefix matcher requires a string, stringer, []bytes, or json.RawMessage. "+ + "Got:\n%s", format.Object(actual, 1)) } prefix := matcher.prefix() return len(actualString) >= len(prefix) && actualString[0:len(prefix)] == prefix, nil diff --git a/matchers/have_suffix_matcher.go b/matchers/have_suffix_matcher.go index 40a3526eb..0e02d8e8a 100644 --- a/matchers/have_suffix_matcher.go +++ b/matchers/have_suffix_matcher.go @@ -14,7 +14,8 @@ type HaveSuffixMatcher struct { func (matcher *HaveSuffixMatcher) Match(actual interface{}) (success bool, err error) { actualString, ok := toString(actual) if !ok { - return false, fmt.Errorf("HaveSuffix matcher requires a string or stringer. Got:\n%s", format.Object(actual, 1)) + return false, fmt.Errorf("HaveSuffix matcher requires a string, stringer, []bytes, or json.RawMessage. "+ + "Got:\n%s", format.Object(actual, 1)) } suffix := matcher.suffix() return len(actualString) >= len(suffix) && actualString[len(actualString)-len(suffix):] == suffix, nil