Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pkg/compare/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ func TestDifferentAt(t *testing.T) {
d = compare.NewDelta()
d.Add("Bar", a.Bar, b.Bar)
require.True(d.DifferentAt("Bar"))
require.False(d.DifferentAt("Baz"))
require.False(d.DifferentAt("Baz")) // diff exists but was not added to Delta

d = compare.NewDelta()
d.Add("Baz.Y", a.Baz.Y, b.Baz.Y)
require.True(d.DifferentAt("Baz"))
require.True(d.DifferentAt("Y"))
require.False(d.DifferentAt("Bar"))
require.True(d.DifferentAt("Baz.Y"))
require.False(d.DifferentAt("Y")) // there is no diff for top-level field "Y"
require.False(d.DifferentAt("Bar")) // diff exists but it was not added to Delta
require.False(d.DifferentAt("Baz.Y.Z")) // subject length exceeds length of diff Path
require.False(d.DifferentAt("Baz.Z")) // matches Path top-level field but not sub-field
Comment on lines +61 to +65
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ Super clear test cases! Love it!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, excellent clarity @echen-98

}
24 changes: 19 additions & 5 deletions pkg/compare/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,28 @@ func (p Path) Pop() {
}
}

// Contains returns true if the supplied string appears within the Path
// Contains returns true if the supplied string, delimited on ".", matches
// p.parts up to the length of the supplied string.
Comment on lines +54 to +55
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

femto-nit: not sure if it's your IDE or editor changing the above, but comments should have a single space (not \t) after the // and before the first letter of the comment...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, will keep in mind for next time

// e.g. if the Path p represents "A.B":
// subject "A" -> true
// subject "A.B" -> true
// subject "A.B.C" -> false
// subject "B" -> false
// subject "A.C" -> false
func (p Path) Contains(subject string) bool {
for _, p := range p.parts {
if p == subject {
return true
subjectSplit := strings.Split(subject, ".")

if len(subjectSplit) > len(p.parts) {
return false
}

for i, s := range subjectSplit {
if p.parts[i] != s {
return false
}
}
return false

return true
}

// NewPath returns a new Path struct pointer from a dotted-notation string,
Expand Down