Files
tasks/common/testing/protoassert/testify_assert.go
Stephan BehnkeandGitHub c9e360a3fb go fix (#9337)
## What changed?

Integrate Go 1.26's new `go fix` into workflow.

NOTE that the changes caused our linter to fire; a [separate
commit](https://github.com/temporalio/temporal/pull/9337/commits/1b23f787aef370098b35829c4d17dcc06230b4c1)
addresses those.

## Why?

Ensure Go code is standardized/modernized.

## How did you test it?
- [ ] built
- [ ] run locally and tested manually
- [x] covered by existing tests
- [ ] added new unit test(s)
- [ ] added new functional test(s)
2026-02-18 09:12:19 -08:00

107 lines
2.5 KiB
Go

// The following helpers are slightly modified versions of those found in testify's assert package
package protoassert
import (
"bytes"
"fmt"
"reflect"
"github.com/stretchr/testify/assert"
"go.temporal.io/api/temporalproto"
)
func formatListDiff(listA, listB any, extraA, extraB any) string {
var msg bytes.Buffer
msg.WriteString("elements differ")
if !isEmpty(extraA) {
msg.WriteString("\n\nextra elements in list A:\n")
msg.WriteString(prettyPrint(extraA))
}
if !isEmpty(extraB) {
msg.WriteString("\n\nextra elements in list B:\n")
msg.WriteString(prettyPrint(extraB))
}
msg.WriteString("\n\nlistA:\n")
msg.WriteString(prettyPrint(listA))
msg.WriteString("\n\nlistB:\n")
msg.WriteString(prettyPrint(listB))
return msg.String()
}
func diffLists(listA, listB any) (extraA, extraB []any) {
aValue := reflect.ValueOf(listA)
bValue := reflect.ValueOf(listB)
aLen := aValue.Len()
bLen := bValue.Len()
// Mark indexes in bValue that we already used
visited := make([]bool, bLen)
for i := range aLen {
element := aValue.Index(i).Interface()
found := false
for j := range bLen {
if visited[j] {
continue
}
if temporalproto.DeepEqual(bValue.Index(j).Interface(), element) {
visited[j] = true
found = true
break
}
}
if !found {
extraA = append(extraA, element)
}
}
for j := range bLen {
if visited[j] {
continue
}
extraB = append(extraB, bValue.Index(j).Interface())
}
return
}
func isEmpty(object any) bool {
// get nil case out of the way
if object == nil {
return true
}
objValue := reflect.ValueOf(object)
switch objValue.Kind() {
// collection types are empty when they have no element
case reflect.Chan, reflect.Map, reflect.Slice:
return objValue.Len() == 0
// pointers are empty if nil or if the value they point to is empty
case reflect.Ptr:
if objValue.IsNil() {
return true
}
deref := objValue.Elem().Interface()
return isEmpty(deref)
// for all other types, compare against the zero value
// array types are empty when they match their zero-initialized state
default:
zero := reflect.Zero(objValue.Type())
return reflect.DeepEqual(object, zero.Interface())
}
}
// isList checks that the provided value is array or slice.
func isList(t assert.TestingT, list any, msgAndArgs ...any) (ok bool) {
kind := reflect.TypeOf(list).Kind()
if kind != reflect.Array && kind != reflect.Slice {
return assert.Fail(t, fmt.Sprintf("%q has an unsupported type %s, expecting array or slice", list, kind),
msgAndArgs...)
}
return true
}