mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
tpl/tplimpl: Simplify some test assertions
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user