-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move AssertOutputFields to handlers package
- Loading branch information
Showing
2 changed files
with
37 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package handlers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"golang.org/x/exp/slices" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
) | ||
|
||
type protoReflector interface { | ||
ProtoReflect() protoreflect.Message | ||
} | ||
|
||
// assertOutputFields asserts that the output fields of a group match the expected fields | ||
// fields that is nil or empty in the result will throw an error if they are listed in expectedFields | ||
// e.g. members when group does not contian any members | ||
func AssertOutputFields(t *testing.T, p protoReflector, expectFields []string) { | ||
msg := p.ProtoReflect() | ||
descriptor := msg.Descriptor() | ||
for i := 0; i < descriptor.Fields().Len(); i++ { | ||
fd := descriptor.Fields().Get(i) | ||
fieldName := string(fd.Name()) | ||
if !slices.Contains(expectFields, fieldName) { | ||
require.Falsef(t, msg.Has(fd), "expect field '%s' to be empty but got %+v", fd.Name(), msg.Get(fd).Interface()) | ||
continue | ||
} | ||
require.Truef(t, msg.Has(fd), "expect field '%s' to be empty but got %+v", fd.Name(), msg.Get(fd).Interface()) | ||
} | ||
} |