google / google/go-cmp

cmp.Diff() switching from property diffs to "new object" diff when object changes significantly

Open
#343 1 comment 1 reaction 0 assignees View on GitHub
reporter
Dominant language
Go
Stars
4.7k
Forks
243
PR merge metrics
No merged PRs in 30d

Description

I am using cmp.Diff() with a reporter to manage some complex tests. The library is excellent but I have encountered confusing behaviour which also seems to be undocumented, unless I have missed it.

When a large portion of a slice struct changes, the slice is reported as an entirely new object. From what I can tell this happens when more than half of the properties change.

I cannot find this behaviour documented anywhere except a passing reference (possibly?) in the searchBudget variable .

https://github.com/google/go-cmp/blob/c3ad8435e7bef96af35732bc0789e5a2278c6d5f/cmp/internal/diff/diff.go#L185-L188

My tests assume that when I change a property of an object with 6 properties, I receive the associated diff, rather than arbitrarily seeing (for example) 3 changes, and then "a new object" when 4 changes are present.

I appreciate this may be unique to me but it caught me by surprise and took a considerable amount of time to troubleshoot.

I have posted a minimum reproducible example here - https://github.com/jgalliers/go-cmp-repro

`go run .` will show a diff with 3 changes followed by changing a property (bringing the total changes to 4) which converts the diff output to a single change, which is an entirely new object.

3 diffs:

```
[]*main.Parent{
&{
Id: "app",
Slice: []main.Child{
{
- Src: "src",
+ Src: "src2",
- Dst: "dst",
+ Dst: "dst2",
- DstVersion: "v1",
+ DstVersion: "v2",
Routes: {"test1", "test2"},
DeployDependency: false,
},
},
},
}
```

4 diffs:

```
[]*main.Parent{
&{
Id: "app",
Slice: []main.Child{
- {
- Src: "src",
- Dst: "dst",
- DstVersion: "v1",
- Routes: []string{"test1", "test2"},
- DeployDependency: true,
- },
+ {Src: "src2", Dst: "dst2", DstVersion: "v2", Routes: []string{"test1", "test2"}},
},
},
}
```

Is this desired or expected behaviour? Is it possible to disable this behaviour?

I would vastly prefer to receive a full property-wise diff, each property of which is reportable via the vx/vy objects rather than suddenly receiving a vy object with `IsValid() = false` due to the sudden conversion to a null pointer.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.