protobuf-go/proto/isinit_test.go
Joe Tsai 09217f08d2 all: make error messages unstable
Use internal/detrand in the construction of our error messages.
This alters whether there is one or two spaces following the "proto:" prefix.
While it is easy for users to still work around this mutation,
sit at least forces them to write test infrastructure to more fuzzily
match on error strings.

Change-Id: I4ddca717526ee3fc4dbb1e0b36cfca8c6e0df36d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/194038
Reviewed-by: Herbie Ong <herbie@google.com>
2019-09-07 00:39:30 +00:00

60 lines
1.3 KiB
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style.
// license that can be found in the LICENSE file.
package proto_test
import (
"fmt"
"strings"
"testing"
"google.golang.org/protobuf/proto"
testpb "google.golang.org/protobuf/internal/testprotos/test"
)
func TestIsInitializedErrors(t *testing.T) {
for _, test := range []struct {
m proto.Message
want string
}{
{
&testpb.TestRequired{},
`goproto.proto.test.TestRequired.required_field`,
},
{
&testpb.TestRequiredForeign{
OptionalMessage: &testpb.TestRequired{},
},
`goproto.proto.test.TestRequired.required_field`,
},
{
&testpb.TestRequiredForeign{
RepeatedMessage: []*testpb.TestRequired{
{RequiredField: proto.Int32(1)},
{},
},
},
`goproto.proto.test.TestRequired.required_field`,
},
{
&testpb.TestRequiredForeign{
MapMessage: map[int32]*testpb.TestRequired{
1: {},
},
},
`goproto.proto.test.TestRequired.required_field`,
},
} {
err := proto.IsInitialized(test.m)
got := "<nil>"
if err != nil {
got = fmt.Sprintf("%q", err)
}
if !strings.Contains(got, test.want) {
t.Errorf("IsInitialized(m):\n got: %v\nwant contains: %v\nMessage:\n%v", got, test.want, marshalText(test.m))
}
}
}