| 21 | ) |
| 22 | |
| 23 | func TestJSONPbMarshal(t *testing.T) { |
| 24 | msg := examplepb.ABitOfEverything{ |
| 25 | SingleNested: &examplepb.ABitOfEverything_Nested{}, |
| 26 | RepeatedStringValue: []string{}, |
| 27 | MappedStringValue: map[string]string{}, |
| 28 | MappedNestedValue: map[string]*examplepb.ABitOfEverything_Nested{}, |
| 29 | RepeatedEnumValue: []examplepb.NumericEnum{}, |
| 30 | TimestampValue: ×tamppb.Timestamp{}, |
| 31 | Uuid: "6EC2446F-7E89-4127-B3E6-5C05E6BECBA7", |
| 32 | Nested: []*examplepb.ABitOfEverything_Nested{ |
| 33 | { |
| 34 | Name: "foo", |
| 35 | Amount: 12345, |
| 36 | }, |
| 37 | }, |
| 38 | Uint64Value: 0xFFFFFFFFFFFFFFFF, |
| 39 | EnumValue: examplepb.NumericEnum_ONE, |
| 40 | OneofValue: &examplepb.ABitOfEverything_OneofString{ |
| 41 | OneofString: "bar", |
| 42 | }, |
| 43 | MapValue: map[string]examplepb.NumericEnum{ |
| 44 | "a": examplepb.NumericEnum_ONE, |
| 45 | "b": examplepb.NumericEnum_ZERO, |
| 46 | }, |
| 47 | RepeatedEnumAnnotation: []examplepb.NumericEnum{}, |
| 48 | EnumValueAnnotation: examplepb.NumericEnum_ONE, |
| 49 | RepeatedStringAnnotation: []string{}, |
| 50 | RepeatedNestedAnnotation: []*examplepb.ABitOfEverything_Nested{}, |
| 51 | NestedAnnotation: &examplepb.ABitOfEverything_Nested{}, |
| 52 | } |
| 53 | |
| 54 | for i, spec := range []struct { |
| 55 | useEnumNumbers, emitUnpopulated bool |
| 56 | indent string |
| 57 | useProtoNames bool |
| 58 | verifier func(json string) |
| 59 | }{ |
| 60 | { |
| 61 | verifier: func(json string) { |
| 62 | if !strings.Contains(json, "ONE") { |
| 63 | t.Errorf(`strings.Contains(%q, "ONE") = false; want true`, json) |
| 64 | } |
| 65 | if want := "uint64Value"; !strings.Contains(json, want) { |
| 66 | t.Errorf(`strings.Contains(%q, %q) = false; want true`, json, want) |
| 67 | } |
| 68 | }, |
| 69 | }, |
| 70 | { |
| 71 | useEnumNumbers: true, |
| 72 | verifier: func(json string) { |
| 73 | if strings.Contains(json, "ONE") { |
| 74 | t.Errorf(`strings.Contains(%q, "ONE") = true; want false`, json) |
| 75 | } |
| 76 | }, |
| 77 | }, |
| 78 | { |
| 79 | emitUnpopulated: true, |
| 80 | verifier: func(json string) { |