| 1429 | } |
| 1430 | |
| 1431 | func TestJSON(t *testing.T) { |
| 1432 | m := &pb2.MyMessage{ |
| 1433 | Count: proto.Int32(4), |
| 1434 | Pet: []string{"bunny", "kitty"}, |
| 1435 | Inner: &pb2.InnerMessage{ |
| 1436 | Host: proto.String("cauchy"), |
| 1437 | }, |
| 1438 | Bikeshed: pb2.MyMessage_GREEN.Enum(), |
| 1439 | } |
| 1440 | const want = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}` |
| 1441 | |
| 1442 | b, err := json.Marshal(m) |
| 1443 | if err != nil { |
| 1444 | t.Fatalf("json.Marshal failed: %v", err) |
| 1445 | } |
| 1446 | s := string(b) |
| 1447 | if s != want { |
| 1448 | t.Errorf("got %s\nwant %s", s, want) |
| 1449 | } |
| 1450 | |
| 1451 | received := new(pb2.MyMessage) |
| 1452 | if err := json.Unmarshal(b, received); err != nil { |
| 1453 | t.Fatalf("json.Unmarshal failed: %v", err) |
| 1454 | } |
| 1455 | if !proto.Equal(received, m) { |
| 1456 | t.Fatalf("got %s, want %s", received, m) |
| 1457 | } |
| 1458 | |
| 1459 | // Test unmarshaling of JSON with symbolic enum name. |
| 1460 | const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}` |
| 1461 | received.Reset() |
| 1462 | if err := json.Unmarshal([]byte(old), received); err != nil { |
| 1463 | t.Fatalf("json.Unmarshal failed: %v", err) |
| 1464 | } |
| 1465 | if !proto.Equal(received, m) { |
| 1466 | t.Fatalf("got %s, want %s", received, m) |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | func TestBadWireType(t *testing.T) { |
| 1471 | b := []byte{7<<3 | 6} // field 7, wire type 6 |