diff options
Diffstat (limited to 'htesting/hqt/checkers.go')
-rw-r--r-- | htesting/hqt/checkers.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/htesting/hqt/checkers.go b/htesting/hqt/checkers.go index eb8b733ac..6fb65ee47 100644 --- a/htesting/hqt/checkers.go +++ b/htesting/hqt/checkers.go @@ -15,12 +15,24 @@ package hqt import ( "errors" + "fmt" "reflect" + "strings" qt "github.com/frankban/quicktest" + "github.com/gohugoio/hugo/htesting" "github.com/google/go-cmp/cmp" + "github.com/spf13/cast" ) +// IsSameString asserts that two strings are equal. The two strings +// are normalized (whitespace removed) before doing a ==. +// Also note that two strings can be the same even if they're of different +// types. +var IsSameString qt.Checker = &stringChecker{ + argNames: []string{"got", "want"}, +} + // IsSameType asserts that got is the same type as want. var IsSameType qt.Checker = &typeChecker{ argNames: []string{"got", "want"}, @@ -47,6 +59,36 @@ func (c *typeChecker) Check(got interface{}, args []interface{}, note func(key s return nil } +type stringChecker struct { + argNames +} + +// Check implements Checker.Check by checking that got and args[0] represents the same normalized text (whitespace etc. trimmed). +func (c *stringChecker) Check(got interface{}, args []interface{}, note func(key string, value interface{})) (err error) { + s1, s2 := cast.ToString(got), cast.ToString(args[0]) + + if s1 == s2 { + return nil + } + + s1, s2 = normalizeString(s1), normalizeString(s2) + + if s1 == s2 { + return nil + } + + return fmt.Errorf("values are not the same text: %s", htesting.DiffStrings(s1, s2)) +} + +func normalizeString(s string) string { + lines := strings.Split(strings.TrimSpace(s), "\n") + for i, line := range lines { + lines[i] = strings.TrimSpace(line) + } + + return strings.Join(lines, "\n") +} + // DeepAllowUnexported creates an option to allow compare of unexported types // in the given list of types. // see https://github.com/google/go-cmp/issues/40#issuecomment-328615283 |