MCPcopy
hub / github.com/stretchr/testify / diffLists

Function diffLists

assert/assertions.go:1173–1208  ·  view source on GitHub ↗

diffLists diffs two arrays/slices and returns slices of elements that are only in A and only in B. If some element is present multiple times, each instance is counted separately (e.g. if something is 2x in A and 5x in B, it will be 0x in extraA and 3x in extraB). The order of items in both lists is

(listA, listB interface{})

Source from the content-addressed store, hash-verified

1171// If some element is present multiple times, each instance is counted separately (e.g. if something is 2x in A and
1172// 5x in B, it will be 0x in extraA and 3x in extraB). The order of items in both lists is ignored.
1173func diffLists(listA, listB interface{}) (extraA, extraB []interface{}) {
1174 aValue := reflect.ValueOf(listA)
1175 bValue := reflect.ValueOf(listB)
1176
1177 aLen := aValue.Len()
1178 bLen := bValue.Len()
1179
1180 // Mark indexes in bValue that we already used
1181 visited := make([]bool, bLen)
1182 for i := 0; i < aLen; i++ {
1183 element := aValue.Index(i).Interface()
1184 found := false
1185 for j := 0; j < bLen; j++ {
1186 if visited[j] {
1187 continue
1188 }
1189 if ObjectsAreEqual(bValue.Index(j).Interface(), element) {
1190 visited[j] = true
1191 found = true
1192 break
1193 }
1194 }
1195 if !found {
1196 extraA = append(extraA, element)
1197 }
1198 }
1199
1200 for j := 0; j < bLen; j++ {
1201 if visited[j] {
1202 continue
1203 }
1204 extraB = append(extraB, bValue.Index(j).Interface())
1205 }
1206
1207 return
1208}
1209
1210func formatListDiff(listA, listB interface{}, extraA, extraB []interface{}) string {
1211 var msg bytes.Buffer

Callers 3

TestDiffListsFunction · 0.85
ElementsMatchFunction · 0.85
NotElementsMatchFunction · 0.85

Calls 2

ObjectsAreEqualFunction · 0.85
LenMethod · 0.45

Tested by 1

TestDiffListsFunction · 0.68