@@ -2,21 +2,24 @@ package outputfilter
22
33import (
44 "reflect"
5+ "sort"
56 "testing"
67
78 "github.com/stretchr/testify/require"
89)
910
1011func TestNewEntriesMapPerIDAndOperationID (t * testing.T ) {
1112 testCases := []struct {
12- name string
13- entries []* OasDiffEntry
14- want map [string ]map [string ][]* OasDiffEntry
13+ name string
14+ entries []* OasDiffEntry
15+ want map [string ]map [string ][]* OasDiffEntry
16+ wantHidden map [string ]map [string ][]* OasDiffEntry
1517 }{
1618 {
17- name : "Empty entries" ,
18- entries : []* OasDiffEntry {},
19- want : map [string ]map [string ][]* OasDiffEntry {},
19+ name : "Empty entries" ,
20+ entries : []* OasDiffEntry {},
21+ want : map [string ]map [string ][]* OasDiffEntry {},
22+ wantHidden : map [string ]map [string ][]* OasDiffEntry {},
2023 },
2124 {
2225 name : "Single entry" ,
@@ -30,6 +33,7 @@ func TestNewEntriesMapPerIDAndOperationID(t *testing.T) {
3033 },
3134 },
3235 },
36+ wantHidden : map [string ]map [string ][]* OasDiffEntry {},
3337 },
3438 {
3539 name : "Multiple entries with same ID" ,
@@ -47,8 +51,8 @@ func TestNewEntriesMapPerIDAndOperationID(t *testing.T) {
4751 },
4852 },
4953 },
54+ wantHidden : map [string ]map [string ][]* OasDiffEntry {},
5055 },
51-
5256 {
5357 name : "Multiple entries with same ID and OperationID" ,
5458 entries : []* OasDiffEntry {
@@ -63,6 +67,7 @@ func TestNewEntriesMapPerIDAndOperationID(t *testing.T) {
6367 },
6468 },
6569 },
70+ wantHidden : map [string ]map [string ][]* OasDiffEntry {},
6671 },
6772 {
6873 name : "Multiple entries with different IDs" ,
@@ -90,15 +95,66 @@ func TestNewEntriesMapPerIDAndOperationID(t *testing.T) {
9095 },
9196 },
9297 },
98+ wantHidden : map [string ]map [string ][]* OasDiffEntry {},
99+ },
100+ {
101+ name : "Hidden entries" ,
102+ entries : []* OasDiffEntry {
103+ {ID : "response-write-only-property-enum-value-added" , OperationID : "op1" , HideFromChangelog : true },
104+ {ID : "response-write-only-property-enum-value-added" , OperationID : "op2" , HideFromChangelog : true },
105+ },
106+ want : map [string ]map [string ][]* OasDiffEntry {},
107+ wantHidden : map [string ]map [string ][]* OasDiffEntry {
108+ "response-write-only-property-enum-value-added" : {
109+ "op1" : []* OasDiffEntry {
110+ {ID : "response-write-only-property-enum-value-added" , OperationID : "op1" , HideFromChangelog : true },
111+ },
112+ "op2" : []* OasDiffEntry {
113+ {ID : "response-write-only-property-enum-value-added" , OperationID : "op2" , HideFromChangelog : true },
114+ },
115+ },
116+ },
117+ },
118+ {
119+ name : "Mixed hidden and non-hidden entries" ,
120+ entries : []* OasDiffEntry {
121+ {ID : "response-write-only-property-enum-value-added" , OperationID : "op1" },
122+ {ID : "response-write-only-property-enum-value-added" , OperationID : "op2" , HideFromChangelog : true },
123+ {ID : "response-write-only-property-enum-value-added" , OperationID : "op3" },
124+ {ID : "response-write-only-property-enum-value-added" , OperationID : "op4" , HideFromChangelog : true },
125+ },
126+ want : map [string ]map [string ][]* OasDiffEntry {
127+ "response-write-only-property-enum-value-added" : {
128+ "op1" : []* OasDiffEntry {
129+ {ID : "response-write-only-property-enum-value-added" , OperationID : "op1" },
130+ },
131+ "op3" : []* OasDiffEntry {
132+ {ID : "response-write-only-property-enum-value-added" , OperationID : "op3" },
133+ },
134+ },
135+ },
136+ wantHidden : map [string ]map [string ][]* OasDiffEntry {
137+ "response-write-only-property-enum-value-added" : {
138+ "op2" : []* OasDiffEntry {
139+ {ID : "response-write-only-property-enum-value-added" , OperationID : "op2" , HideFromChangelog : true },
140+ },
141+ "op4" : []* OasDiffEntry {
142+ {ID : "response-write-only-property-enum-value-added" , OperationID : "op4" , HideFromChangelog : true },
143+ },
144+ },
145+ },
93146 },
94147 }
95148
96149 for _ , tc := range testCases {
97150 t .Run (tc .name , func (t * testing.T ) {
98- got := newEntriesMapPerIDAndOperationID (tc .entries )
151+ got , gotHidden := newEntriesMapPerIDAndOperationID (tc .entries )
99152 if ! reflect .DeepEqual (got , tc .want ) {
100153 t .Errorf ("got %v, want %v" , got , tc .want )
101154 }
155+ if ! reflect .DeepEqual (gotHidden , tc .wantHidden ) {
156+ t .Errorf ("gotHidden %v, wantHidden %v" , gotHidden , tc .wantHidden )
157+ }
102158 })
103159 }
104160}
@@ -238,3 +294,168 @@ func TestNewSquashMap(t *testing.T) {
238294 })
239295 }
240296}
297+
298+ func TestSquashEntries (t * testing.T ) {
299+ testCases := []struct {
300+ name string
301+ entries []* OasDiffEntry
302+ want []* OasDiffEntry
303+ }{
304+ {
305+ name : "Empty entries" ,
306+ entries : []* OasDiffEntry {},
307+ want : []* OasDiffEntry {},
308+ },
309+ {
310+ name : "Single entry" ,
311+ entries : []* OasDiffEntry {
312+ {
313+ ID : "response-write-only-property-enum-value-added" ,
314+ OperationID : "op1" ,
315+ Text : "added the new 'ENUM1' enum value to the 'region' response write-only property" ,
316+ },
317+ },
318+ want : []* OasDiffEntry {
319+ {
320+ ID : "response-write-only-property-enum-value-added" ,
321+ OperationID : "op1" ,
322+ Text : "added the new 'ENUM1' enum value to the 'region' response write-only property" ,
323+ },
324+ },
325+ },
326+ {
327+ name : "Multiple entries with same ID and OperationID" ,
328+ entries : []* OasDiffEntry {
329+ {
330+ ID : "response-write-only-property-enum-value-added" ,
331+ OperationID : "op1" ,
332+ Text : "added the new 'ENUM1' enum value to the 'region' response write-only property" ,
333+ },
334+ {
335+ ID : "response-write-only-property-enum-value-added" ,
336+ OperationID : "op1" ,
337+ Text : "added the new 'ENUM2' enum value to the 'region' response write-only property" ,
338+ },
339+ },
340+ want : []* OasDiffEntry {
341+ {
342+ ID : "response-write-only-property-enum-value-added" ,
343+ OperationID : "op1" ,
344+ Text : "added the new 'ENUM1, ENUM2' enum values to the 'region' response write-only property" ,
345+ },
346+ },
347+ },
348+ {
349+ name : "Multiple entries with different IDs" ,
350+ entries : []* OasDiffEntry {
351+ {
352+ ID : "response-write-only-property-enum-value-added" ,
353+ OperationID : "op1" ,
354+ Text : "added the new 'ENUM1' enum value to the 'region' response write-only property" ,
355+ },
356+ {
357+ ID : "request-write-only-property-enum-value-added" ,
358+ OperationID : "op2" ,
359+ Text : "added the new 'ENUM2' enum value to the 'region' request write-only property" ,
360+ },
361+ },
362+ want : []* OasDiffEntry {
363+ {
364+ ID : "response-write-only-property-enum-value-added" ,
365+ OperationID : "op1" ,
366+ Text : "added the new 'ENUM1' enum value to the 'region' response write-only property" ,
367+ },
368+ {
369+ ID : "request-write-only-property-enum-value-added" ,
370+ OperationID : "op2" ,
371+ Text : "added the new 'ENUM2' enum value to the 'region' request write-only property" ,
372+ },
373+ },
374+ },
375+ {
376+ name : "Hidden entries" ,
377+ entries : []* OasDiffEntry {
378+ {
379+ ID : "response-write-only-property-enum-value-added" ,
380+ OperationID : "op1" ,
381+ Text : "added the new 'ENUM1' enum value to the 'region' response write-only property" ,
382+ HideFromChangelog : true ,
383+ },
384+ {
385+ ID : "response-write-only-property-enum-value-added" ,
386+ OperationID : "op1" ,
387+ Text : "added the new 'ENUM2' enum value to the 'region' response write-only property" ,
388+ HideFromChangelog : true ,
389+ },
390+ },
391+ want : []* OasDiffEntry {
392+ {
393+ ID : "response-write-only-property-enum-value-added" ,
394+ OperationID : "op1" ,
395+ Text : "added the new 'ENUM1, ENUM2' enum values to the 'region' response write-only property" ,
396+ HideFromChangelog : true ,
397+ },
398+ },
399+ },
400+ {
401+ name : "Mixed hidden and non-hidden entries" ,
402+ entries : []* OasDiffEntry {
403+ {
404+ ID : "response-write-only-property-enum-value-added" ,
405+ OperationID : "op1" ,
406+ Text : "added the new 'ENUM1' enum value to the 'region' response write-only property" ,
407+ },
408+ {
409+ ID : "response-write-only-property-enum-value-added" ,
410+ OperationID : "op1" ,
411+ Text : "added the new 'ENUM2' enum value to the 'region' response write-only property" ,
412+ HideFromChangelog : true ,
413+ },
414+ {
415+ ID : "response-write-only-property-enum-value-added" ,
416+ OperationID : "op1" ,
417+ Text : "added the new 'ENUM3' enum value to the 'region' response write-only property" ,
418+ },
419+ {
420+ ID : "response-write-only-property-enum-value-added" ,
421+ OperationID : "op1" ,
422+ Text : "added the new 'ENUM4' enum value to the 'region' response write-only property" ,
423+ HideFromChangelog : true ,
424+ },
425+ },
426+ want : []* OasDiffEntry {
427+ {
428+ ID : "response-write-only-property-enum-value-added" ,
429+ OperationID : "op1" ,
430+ Text : "added the new 'ENUM1, ENUM3' enum values to the 'region' response write-only property" ,
431+ },
432+ {
433+ ID : "response-write-only-property-enum-value-added" ,
434+ OperationID : "op1" ,
435+ Text : "added the new 'ENUM2, ENUM4' enum values to the 'region' response write-only property" ,
436+ HideFromChangelog : true ,
437+ },
438+ },
439+ },
440+ }
441+
442+ for _ , tc := range testCases {
443+ t .Run (tc .name , func (t * testing.T ) {
444+ got , err := squashEntries (tc .entries )
445+ require .NoError (t , err )
446+ sortEntries (got )
447+ sortEntries (tc .want )
448+ require .Equal (t , tc .want , got )
449+ })
450+ }
451+ }
452+
453+ // sortEntries sorts the entries by their ID and OperationID.
454+ func sortEntries (entries []* OasDiffEntry ) {
455+ sort .SliceStable (entries , func (i , j int ) bool {
456+ if entries [i ].ID != entries [j ].ID {
457+ return entries [i ].ID < entries [j ].ID
458+ }
459+ return entries [i ].OperationID < entries [j ].OperationID
460+ })
461+ }
0 commit comments