File: serialization_test.rs

package info (click to toggle)
protobuf 3.25.4-2
  • links: PTS
  • area: main
  • in suites:
  • size: 45,944 kB
  • sloc: cpp: 204,199; java: 87,622; ansic: 81,204; objc: 58,434; cs: 27,303; python: 22,799; php: 11,340; ruby: 8,637; pascal: 3,325; xml: 2,333; sh: 1,331; makefile: 538; lisp: 86; awk: 17
file content (39 lines) | stat: -rw-r--r-- 1,200 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 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::proto2_unittest::TestAllTypes;

#[test]
fn serialize_deserialize_message() {
    let mut msg = TestAllTypes::new();
    msg.optional_int64_set(Some(42));
    msg.optional_bool_set(Some(true));
    msg.optional_bytes_mut().set(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());
}