Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

63 lines
1.8 KiB

// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
use googletest::prelude::*;
use unittest_proto::TestAllTypes;
#[test]
fn serialize_zero_length() {
let mut msg = TestAllTypes::new();
let serialized = msg.serialize();
assert_that!(serialized.len(), eq(0));
let serialized = msg.as_view().serialize();
assert_that!(serialized.len(), eq(0));
let serialized = msg.as_mut().serialize();
assert_that!(serialized.len(), eq(0));
}
#[test]
fn serialize_deserialize_message() {
let mut msg = TestAllTypes::new();
msg.set_optional_int64(42);
msg.set_optional_bool(true);
msg.set_optional_bytes(b"serialize deserialize test");
let serialized = msg.serialize();
let mut msg2 = TestAllTypes::new();
assert!(msg2.deserialize(&serialized).is_ok());
assert_that!(msg.optional_int64(), eq(msg2.optional_int64()));
assert_that!(msg.optional_bool(), eq(msg2.optional_bool()));
assert_that!(msg.optional_bytes(), eq(msg2.optional_bytes()));
}
#[test]
fn deserialize_empty() {
let mut msg = TestAllTypes::new();
assert!(msg.deserialize(&[]).is_ok());
}
#[test]
fn deserialize_error() {
let mut msg = TestAllTypes::new();
let data = b"not a serialized proto";
assert!(msg.deserialize(&*data).is_err());
}
#[test]
fn set_bytes_with_serialized_data() {
let mut msg = TestAllTypes::new();
msg.set_optional_int64(42);
msg.set_optional_bool(true);
let mut msg2 = TestAllTypes::new();
msg2.set_optional_bytes(msg.serialize());
assert_that!(msg2.optional_bytes(), eq(msg.serialize().as_ref()));
}