tpl/tplimpl: Simplify some test assertions

This commit is contained in:
Bjørn Erik Pedersen
2025-01-16 10:53:53 +01:00
parent 8b52626915
commit 1fad3832a9
2 changed files with 38 additions and 39 deletions

View File

@@ -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