diff --git a/htesting/hqt/checkers.go b/htesting/hqt/checkers.go index bf2cb942820..a0520653509 100644 --- a/htesting/hqt/checkers.go +++ b/htesting/hqt/checkers.go @@ -96,6 +96,32 @@ func normalizeString(s string) string { return strings.Join(lines, "\n") } +// IsAllElementsEqual asserts that all elements in the slice are equal. +var IsAllElementsEqual qt.Checker = &sliceAllElementsEqualChecker{ + argNames: []string{"got"}, +} + +type sliceAllElementsEqualChecker struct { + argNames +} + +func (c *sliceAllElementsEqualChecker) Check(got any, args []any, note func(key string, value any)) (err error) { + gotSlice := reflect.ValueOf(got) + numElements := gotSlice.Len() + if numElements < 2 { + return nil + } + first := gotSlice.Index(0).Interface() + // Check that the others are equal to the first. + for i := 1; i < numElements; i++ { + if diff := cmp.Diff(first, gotSlice.Index(i).Interface()); diff != "" { + return fmt.Errorf("element %d is not equal to the first element:\n%s", i, diff) + } + } + + return nil +} + // 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 diff --git a/tpl/tplimpl/tplimpl_integration_test.go b/tpl/tplimpl/tplimpl_integration_test.go index 6ccf27f80e9..d1e214ce26f 100644 --- a/tpl/tplimpl/tplimpl_integration_test.go +++ b/tpl/tplimpl/tplimpl_integration_test.go @@ -6,6 +6,7 @@ import ( "testing" qt "github.com/frankban/quicktest" + "github.com/gohugoio/hugo/htesting/hqt" "github.com/gohugoio/hugo/hugolib" "github.com/gohugoio/hugo/tpl" ) @@ -787,9 +788,8 @@ title: p5 b.FileContent("public/p2/index.html"), b.FileContent("public/p3/index.html"), } - if !allElementsEqual(htmlFiles) { - t.Error("A: expected all files to be equal") - } + + b.Assert(htmlFiles, hqt.IsAllElementsEqual) // Test x_simple and twitter_simple shortcodes wantSimple := "
\n--" @@ -799,9 +799,7 @@ title: p5 b.FileContent("public/p4/index.html"), b.FileContent("public/p5/index.html"), } - if !allElementsEqual(htmlFiles) { - t.Error("B: expected all files to be equal") - } + b.Assert(htmlFiles, hqt.IsAllElementsEqual) filesOriginal := files @@ -813,9 +811,7 @@ title: p5 b.FileContent("public/p3/index.html"), b.FileContent("public/p5/index.html"), } - if !allElementsEqual(htmlFiles) { - t.Error("C: expected all files to be equal") - } + b.Assert(htmlFiles, hqt.IsAllElementsEqual) // Test privacy.x.simple files = strings.ReplaceAll(filesOriginal, "#CONFIG", "privacy.x.simple=true") @@ -825,16 +821,13 @@ title: p5 b.FileContent("public/p4/index.html"), b.FileContent("public/p4/index.html"), } - if !allElementsEqual(htmlFiles) { - t.Error("D: expected all files to be equal") - } + b.Assert(htmlFiles, hqt.IsAllElementsEqual) + htmlFiles = []string{ b.FileContent("public/p2/index.html"), b.FileContent("public/p3/index.html"), } - if !allElementsEqual(htmlFiles) { - t.Error("E: expected all files to be equal") - } + b.Assert(htmlFiles, hqt.IsAllElementsEqual) // Test privacy.twitter.disable files = strings.ReplaceAll(filesOriginal, "#CONFIG", "privacy.twitter.disable = true") @@ -847,9 +840,7 @@ title: p5 b.FileContent("public/p4/index.html"), b.FileContent("public/p4/index.html"), } - if !allElementsEqual(htmlFiles) { - t.Error("F: expected all files to be equal") - } + b.Assert(htmlFiles, hqt.IsAllElementsEqual) // Test privacy.x.disable files = strings.ReplaceAll(filesOriginal, "#CONFIG", "privacy.x.disable = true") @@ -859,29 +850,11 @@ title: p5 b.FileContent("public/p1/index.html"), b.FileContent("public/p4/index.html"), } - if !allElementsEqual(htmlFiles) { - t.Error("G: expected all files to be equal") - } + b.Assert(htmlFiles, hqt.IsAllElementsEqual) + htmlFiles = []string{ b.FileContent("public/p2/index.html"), b.FileContent("public/p3/index.html"), } - if !allElementsEqual(htmlFiles) { - t.Error("F: expected all files to be equal") - } -} - -// allElementsEqual reports whether all elements in the given string slice are -// equal. -func allElementsEqual(slice []string) bool { - if len(slice) == 0 { - return true - } - first := slice[0] - for _, v := range slice[1:] { - if v != first { - return false - } - } - return true + b.Assert(htmlFiles, hqt.IsAllElementsEqual) }Owl bet you'll lose this staring contest 🦉 pic.twitter.com/eJh4f2zncC
— San Diego Zoo Wildlife Alliance (@sandiegozoo) October 26, 2021