Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/protoadapt"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/runtime/protoimpl"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/durationpb"

Expand Down Expand Up @@ -378,23 +377,23 @@ func (s) TestStatus_WithDetails_Fail(t *testing.T) {

func (s) TestStatus_ErrorDetails_Fail(t *testing.T) {
tests := []struct {
s *Status
i []any
s *Status
want []any
}{
{
nil,
nil,
s: nil,
want: nil,
},
{
FromProto(nil),
nil,
s: FromProto(nil),
want: nil,
},
{
New(codes.OK, ""),
[]any{},
s: New(codes.OK, ""),
want: []any{},
},
{
FromProto(&spb.Status{
s: FromProto(&spb.Status{
Code: int32(cpb.Code_CANCELLED),
Details: []*anypb.Any{
{
Expand All @@ -408,8 +407,8 @@ func (s) TestStatus_ErrorDetails_Fail(t *testing.T) {
}),
},
}),
[]any{
protoimpl.X.NewError("invalid empty type URL"),
want: []any{
errors.New("invalid empty type URL"),
&epb.ResourceInfo{
ResourceType: "book",
ResourceName: "projects/1234/books/5678",
Expand All @@ -419,17 +418,24 @@ func (s) TestStatus_ErrorDetails_Fail(t *testing.T) {
},
}
for _, tc := range tests {
got := tc.s.Details()
if !cmp.Equal(got, tc.i, cmp.Comparer(proto.Equal), cmp.Comparer(equalError)) {
t.Errorf("(%v).Details() = %+v, want %+v", str(tc.s), got, tc.i)
details := tc.s.Details()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should first ensure len(details) == len(tc.want) here or it could panic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's right! thanks for catching that

for i, d := range details {
// s.Deatils can either contain an error or a proto message. We
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// want to do a compare the proto message for an Equal match, and
// for errors only check for presence.
if _, ok := d.(error); ok {
if (d != nil) != (tc.want[i] != nil) {
t.Fatalf("s.Details()[%v] was %v; want %v", i, d, tc.want[i])
}
continue
}
if !cmp.Equal(d, tc.want[i], cmp.Comparer(proto.Equal)) {
t.Fatalf("s.Details()[%v] was %v; want %v", i, d, tc.want[i])
}
}
}
}

func equalError(x, y error) bool {
return x == y || (x != nil && y != nil && x.Error() == y.Error())
}

func str(s *Status) string {
if s == nil {
return "nil"
Expand Down