blob: 232202f4080bf35ca7a3187d69c82a8e9bd3eea5 [file] [log] [blame]
Damien Neil4686e232019-04-05 13:31:40 -07001// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style.
3// license that can be found in the LICENSE file.
4
5package proto_test
6
7import (
8 "fmt"
9 "testing"
10
Damien Neile89e6242019-05-13 23:55:40 -070011 "google.golang.org/protobuf/internal/scalar"
12 "google.golang.org/protobuf/proto"
Damien Neil4686e232019-04-05 13:31:40 -070013
Damien Neile89e6242019-05-13 23:55:40 -070014 testpb "google.golang.org/protobuf/internal/testprotos/test"
Damien Neil4686e232019-04-05 13:31:40 -070015)
16
17func TestIsInitializedErrors(t *testing.T) {
18 for _, test := range []struct {
19 m proto.Message
20 want string
21 }{
22 {
23 &testpb.TestRequired{},
24 `proto: required field required_field not set`,
25 },
26 {
27 &testpb.TestRequiredForeign{
28 OptionalMessage: &testpb.TestRequired{},
29 },
30 `proto: required field optional_message.required_field not set`,
31 },
32 {
33 &testpb.TestRequiredForeign{
34 RepeatedMessage: []*testpb.TestRequired{
35 {RequiredField: scalar.Int32(1)},
36 {},
37 },
38 },
39 `proto: required field repeated_message[1].required_field not set`,
40 },
41 {
42 &testpb.TestRequiredForeign{
43 MapMessage: map[int32]*testpb.TestRequired{
44 1: {},
45 },
46 },
47 `proto: required field map_message[1].required_field not set`,
48 },
49 } {
50 err := proto.IsInitialized(test.m)
51 got := "<nil>"
52 if err != nil {
53 got = fmt.Sprintf("%q", err)
54 }
55 want := fmt.Sprintf("%q", test.want)
56 if got != want {
57 t.Errorf("IsInitialized(m):\n got: %v\nwant: %v\nMessage:\n%v", got, want, marshalText(test.m))
58 }
59 }
60}