Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | package proto_test |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "testing" |
| 10 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 11 | "google.golang.org/protobuf/internal/scalar" |
| 12 | "google.golang.org/protobuf/proto" |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 13 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 14 | testpb "google.golang.org/protobuf/internal/testprotos/test" |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | func 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 | } |