Atlas › Test

TestProtoElementsMatch

Exact test identity: go.temporal.io/server/common/testing/protoassert/TestProtoElementsMatch

Package
go.temporal.io/server/common/testing/protoassert
Suite / test hierarchy
TestProtoElementsMatch
Test
TestProtoElementsMatch
Introduced at
testify_assert.go ×8 Frontier kind: Joint frontier
Covered ranges
14
Covered lines
50
Covered files
2

Covered source

Expand a file to inspect source; the > gutter marks covered lines.

go.temporal.io/server/common/testing/protoassert/testify_assert.go 33 covered LOC · 8 ranges

Open complete file

31 }
32
33 > func diffLists(listA, listB any) (extraA, extraB []any) { testify_assert.go
34 > aValue := reflect.ValueOf(listA)
35 > bValue := reflect.ValueOf(listB)
36 >
37 > aLen := aValue.Len()
38 > bLen := bValue.Len()
39 >
40 > // Mark indexes in bValue that we already used
41 > visited := make([]bool, bLen)
42 > for i := range aLen {
43 > element := aValue.Index(i).Interface()
44 > found := false
45 > for j := range bLen {
46 > if visited[j] {
47 > continue
48 }
49 > if temporalproto.DeepEqual(bValue.Index(j).Interface(), element) { testify_assert.go
50 > visited[j] = true
51 > found = true
52 > break
53 }
54 }
55 > if !found { testify_assert.go
56 extraA = append(extraA, element)
57 }
58 }
59
60 > for j := range bLen { testify_assert.go
61 > if visited[j] {
62 > continue
63 }
64 extraB = append(extraB, bValue.Index(j).Interface())
65 }
66
67 > return testify_assert.go
68 }
69
70 > func isEmpty(object any) bool { testify_assert.go
71 >
72 > // get nil case out of the way
73 > if object == nil {
74 return true
75 }
76
77 > objValue := reflect.ValueOf(object) testify_assert.go
78 >
79 > switch objValue.Kind() {
80 // collection types are empty when they have no element
81 > case reflect.Chan, reflect.Map, reflect.Slice: testify_assert.go
82 > return objValue.Len() == 0
83 // pointers are empty if nil or if the value they point to is empty
84 case reflect.Pointer:
go.temporal.io/server/common/testing/protoassert/assert.go 17 covered LOC · 6 ranges

Open complete file

20 }
21
22 > func New(t assert.TestingT) ProtoAssertions { assert.go
23 > return ProtoAssertions{t}
24 > }
25
26 // ProtoEqual compares two proto.Message objects for equality
67 // ProtoElementsMatch behaves like assert.ElementsMatch except in that it works for
68 // google/protobuf-generated structs
69 > func ProtoElementsMatch(t assert.TestingT, a any, b any, msgAndArgs ...any) bool { assert.go
70 > if th, ok := t.(helper); ok {
71 > th.Helper()
72 > }
73
74 > if isEmpty(a) && isEmpty(b) { assert.go
75 return true
76 }
77
78 > extraA, extraB := diffLists(a, b) assert.go
79 > if len(extraA) == 0 && len(extraB) == 0 {
80 > return true
81 > }
82
83 return assert.Fail(t, formatListDiff(a, b, extraA, extraB), msgAndArgs...)
114 }
115
116 > func (x ProtoAssertions) ProtoElementsMatch(a any, b any) bool { assert.go
117 > if th, ok := x.t.(helper); ok {
118 > th.Helper()
119 > }
120
121 > return ProtoElementsMatch(x.t, a, b) assert.go
122 }