go-interface-fuzzer icon indicating copy to clipboard operation
go-interface-fuzzer copied to clipboard

Use supplied comparators for array/slice/map elements.

Open barrucadu opened this issue 9 years ago • 0 comments

Currently supplied comparators are only used for "naked" values, they don't get used for arrays/slices/maps.

These two values will compare equal, using the built-in comparator for error:

x := errors.New("An Error!")
y := errors.New("Another Error!")

But these two won't:

x := []error{errors.New("An Error!")}
y := []error{errors.New("Another Error!")}

The current value comparison code is pretty simple, it just returns a boolean expression which is plugged into an if. This needs to be smarter, in particular, it needs to be able to generate a statement.

This reduces boilerplate in lifting comparators to arrays/slices/maps, and also allows for more robust error messages by default. For example, with a slice this could be generated:

for k, ev := range expected {
    av, ok := actual[k]
    if ! ok {
        return fmt.Errorf("at position %v: inconsistent result in <function name>\nexpected key not present", k)
    }
    if ! <comparison function> (ev, av) {
        return fmt.Errorf("at position %v: inconsistent result in <function name>\nexpected: %v\nactual: %v", k, ev, av)
    }
}

That at position %v can naturally be extended for nested structures, giving a precise breadcrumb-like pointer to the offending value.

barrucadu avatar Jul 27 '16 13:07 barrucadu