Fix Reporter GetDiffsForField (#50264)

pull/50264/merge
Yuriy Tseretyan 3 years ago committed by GitHub
parent 8876d56495
commit a88408bfd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      pkg/util/cmputil/reporter.go
  2. 65
      pkg/util/cmputil/reporter_test.go

@ -14,7 +14,13 @@ type DiffReport []Diff
func (r DiffReport) GetDiffsForField(path string) DiffReport { func (r DiffReport) GetDiffsForField(path string) DiffReport {
var result []Diff var result []Diff
for _, diff := range r { for _, diff := range r {
if strings.HasPrefix(path, diff.Path) { if strings.HasPrefix(diff.Path, path) {
if diff.Path != path {
char := []rune(diff.Path)[len(path)]
if char != '.' && char != '[' { // if the following symbol is not a delimiter or bracket then that's not our path
continue
}
}
result = append(result, diff) result = append(result, diff)
} }
} }

@ -130,3 +130,68 @@ func TestIsAddedDeleted_Collections(t *testing.T) {
} }
}) })
} }
func TestGetDiffsForField(t *testing.T) {
t.Run("should not include fields that starts has prefix", func(t *testing.T) {
diff := DiffReport{
Diff{
Path: "Property",
},
Diff{
Path: "PropertyData",
},
}
result := diff.GetDiffsForField("Property")
require.Len(t, result, 1)
require.Equal(t, "Property", result[0].Path)
})
t.Run("should return all changes by parent path", func(t *testing.T) {
diff := DiffReport{
Diff{
Path: "Property.Data.Value",
},
Diff{
Path: "Property.Array[0].Value",
},
}
result := diff.GetDiffsForField("Property")
require.Len(t, result, 2)
})
t.Run("should return all elements of array", func(t *testing.T) {
diff := DiffReport{
Diff{
Path: "Property[0].Data.Test",
},
Diff{
Path: "Property",
},
Diff{
Path: "Property[1]",
},
}
result := diff.GetDiffsForField("Property")
require.Len(t, result, 3)
})
t.Run("should find nothing if parent path does not exist", func(t *testing.T) {
diff := DiffReport{
Diff{
Path: "Property[0].Data.Test",
},
Diff{
Path: "Property",
},
Diff{
Path: "Property[1]",
},
}
result := diff.GetDiffsForField("Proper")
require.Empty(t, result)
})
}

Loading…
Cancel
Save