MCPcopy
hub / github.com/gogo/protobuf / TestJSON

Function TestJSON

proto/all_test.go:1477–1514  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1475}
1476
1477func TestJSON(t *testing.T) {
1478 m := &MyMessage{
1479 Count: Int32(4),
1480 Pet: []string{"bunny", "kitty"},
1481 Inner: &InnerMessage{
1482 Host: String("cauchy"),
1483 },
1484 Bikeshed: MyMessage_GREEN.Enum(),
1485 }
1486 const expected = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}`
1487
1488 b, err := json.Marshal(m)
1489 if err != nil {
1490 t.Fatalf("json.Marshal failed: %v", err)
1491 }
1492 s := string(b)
1493 if s != expected {
1494 t.Errorf("got %s\nwant %s", s, expected)
1495 }
1496
1497 received := new(MyMessage)
1498 if err := json.Unmarshal(b, received); err != nil {
1499 t.Fatalf("json.Unmarshal failed: %v", err)
1500 }
1501 if !Equal(received, m) {
1502 t.Fatalf("got %s, want %s", received, m)
1503 }
1504
1505 // Test unmarshalling of JSON with symbolic enum name.
1506 const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}`
1507 received.Reset()
1508 if err := json.Unmarshal([]byte(old), received); err != nil {
1509 t.Fatalf("json.Unmarshal failed: %v", err)
1510 }
1511 if !Equal(received, m) {
1512 t.Fatalf("got %s, want %s", received, m)
1513 }
1514}
1515
1516func TestBadWireType(t *testing.T) {
1517 b := []byte{7<<3 | 6} // field 7, wire type 6

Callers

nothing calls this directly

Calls 7

Int32Function · 0.85
StringFunction · 0.85
EqualFunction · 0.85
MarshalMethod · 0.65
UnmarshalMethod · 0.65
ResetMethod · 0.65
EnumMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…