diff --git a/diff/parameters_diff_by_location.go b/diff/parameters_diff_by_location.go index 5585d7ff..55ee44e1 100644 --- a/diff/parameters_diff_by_location.go +++ b/diff/parameters_diff_by_location.go @@ -34,11 +34,7 @@ type ParamNamesByLocation map[string]utils.StringList // Len returns the number of all params in all locations func (params ParamNamesByLocation) Len() int { - result := 0 - for _, l := range params { - result += l.Len() - } - return result + return lenNested(params) } // ParamDiffByLocation maps param location (path, query, header or cookie) to param diffs in this location @@ -46,8 +42,12 @@ type ParamDiffByLocation map[string]ParamDiffs // Len returns the number of all params in all locations func (params ParamDiffByLocation) Len() int { + return lenNested(params) +} + +func lenNested[T utils.StringList | ParamDiffs](mapOfList map[string]T) int { result := 0 - for _, l := range params { + for _, l := range mapOfList { result += len(l) } return result diff --git a/diff/parameters_diff_by_location_test.go b/diff/parameters_diff_by_location_test.go new file mode 100644 index 00000000..d68e5db3 --- /dev/null +++ b/diff/parameters_diff_by_location_test.go @@ -0,0 +1,23 @@ +package diff_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/tufin/oasdiff/diff" + "github.com/tufin/oasdiff/utils" +) + +func TestParamNamesByLocation_Len(t *testing.T) { + require.Equal(t, 3, diff.ParamNamesByLocation{ + "query": utils.StringList{"name"}, + "header": utils.StringList{"id", "organization"}, + }.Len()) +} + +func TestParamDiffByLocation_Len(t *testing.T) { + require.Equal(t, 3, diff.ParamDiffByLocation{ + "query": diff.ParamDiffs{"query": &diff.ParameterDiff{}}, + "header": diff.ParamDiffs{"id": &diff.ParameterDiff{}, "organization": &diff.ParameterDiff{}}, + }.Len()) +}