func TestJSONPbEncoderFields(t *testing.T) { var m runtime.JSONPb for _, fixt := range fieldFixtures { var buf bytes.Buffer enc := m.NewEncoder(&buf) if err := enc.Encode(fixt.data); err != nil { t.Errorf("enc.Encode(%#v) failed with %v; want success", fixt.data, err) } if got, want := buf.String(), fixt.json; got != want { t.Errorf("enc.Encode(%#v) = %q; want %q", fixt.data, got, want) } } m.EnumsAsInts = true buf, err := m.Marshal(examplepb.NumericEnum_ONE) if err != nil { t.Errorf("m.Marshal(%#v) failed with %v; want success", examplepb.NumericEnum_ONE, err) } if got, want := string(buf), "1"; got != want { t.Errorf("m.Marshal(%#v) = %q; want %q", examplepb.NumericEnum_ONE, got, want) } }
func TestJSONPbMarshalFields(t *testing.T) { var m runtime.JSONPb for _, spec := range []struct { val interface{} want string }{} { buf, err := m.Marshal(spec.val) if err != nil { t.Errorf("m.Marshal(%#v) failed with %v; want success", spec.val, err) } if got, want := string(buf), spec.want; got != want { t.Errorf("m.Marshal(%#v) = %q; want %q", spec.val, got, want) } } m.EnumsAsInts = true buf, err := m.Marshal(examplepb.NumericEnum_ONE) if err != nil { t.Errorf("m.Marshal(%#v) failed with %v; want success", examplepb.NumericEnum_ONE, err) } if got, want := string(buf), "1"; got != want { t.Errorf("m.Marshal(%#v) = %q; want %q", examplepb.NumericEnum_ONE, got, want) } }
func TestJSONPbMarshal(t *testing.T) { msg := examplepb.ABitOfEverything{ Uuid: "6EC2446F-7E89-4127-B3E6-5C05E6BECBA7", Nested: []*examplepb.ABitOfEverything_Nested{ { Name: "foo", Amount: 12345, }, }, Uint64Value: 0xFFFFFFFFFFFFFFFF, EnumValue: examplepb.NumericEnum_ONE, OneofValue: &examplepb.ABitOfEverything_OneofString{ OneofString: "bar", }, MapValue: map[string]examplepb.NumericEnum{ "a": examplepb.NumericEnum_ONE, "b": examplepb.NumericEnum_ZERO, }, } for _, spec := range []struct { enumsAsInts, emitDefaults bool indent string origName bool verifier func(json string) }{ { verifier: func(json string) { if strings.ContainsAny(json, " \t\r\n") { t.Errorf("strings.ContainsAny(%q, %q) = true; want false", json, " \t\r\n") } if !strings.Contains(json, "ONE") { t.Errorf(`strings.Contains(%q, "ONE") = false; want true`, json) } if want := "uint64Value"; !strings.Contains(json, want) { t.Errorf(`strings.Contains(%q, %q) = false; want true`, json, want) } }, }, { enumsAsInts: true, verifier: func(json string) { if strings.Contains(json, "ONE") { t.Errorf(`strings.Contains(%q, "ONE") = true; want false`, json) } }, }, { emitDefaults: true, verifier: func(json string) { if want := `"sfixed32Value"`; !strings.Contains(json, want) { t.Errorf(`strings.Contains(%q, %q) = false; want true`, json, want) } }, }, { indent: "\t\t", verifier: func(json string) { if want := "\t\t\"amount\":"; !strings.Contains(json, want) { t.Errorf(`strings.Contains(%q, %q) = false; want true`, json, want) } }, }, { origName: true, verifier: func(json string) { if want := "uint64_value"; !strings.Contains(json, want) { t.Errorf(`strings.Contains(%q, %q) = false; want true`, json, want) } }, }, } { m := runtime.JSONPb{ EnumsAsInts: spec.enumsAsInts, EmitDefaults: spec.emitDefaults, Indent: spec.indent, OrigName: spec.origName, } buf, err := m.Marshal(&msg) if err != nil { t.Errorf("m.Marshal(%v) failed with %v; want success; spec=%v", &msg, err, spec) } var got examplepb.ABitOfEverything if err := jsonpb.UnmarshalString(string(buf), &got); err != nil { t.Errorf("jsonpb.UnmarshalString(%q, &got) failed with %v; want success; spec=%v", string(buf), err, spec) } if want := msg; !reflect.DeepEqual(got, want) { t.Errorf("got = %v; want %v; spec=%v", &got, &want, spec) } if spec.verifier != nil { spec.verifier(string(buf)) } } }