From 5d8bdb44d8c5c01770c4a5cb03d49fdaa3d838b1 Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Sun, 3 Mar 2024 16:40:49 -0500 Subject: [PATCH] Add test for packed default. --- .../src/common/v2/test_repeated_packed.rs | 15 +++++++++++++++ .../src/common/v2/test_repeated_packed_pb.proto | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed.rs b/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed.rs index d8ecbb02a..8e6191aaa 100644 --- a/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed.rs +++ b/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed.rs @@ -1,3 +1,5 @@ +use protobuf::reflect::Syntax; +use protobuf::MessageFull; use protobuf_test_common::*; use super::test_repeated_packed_pb::*; @@ -83,3 +85,16 @@ fn test_issue_281() { test.values = (0..100).collect(); test_serialize_deserialize_no_hex(&test); } + +#[test] +fn test_write_packed_default() { + let mut test = TestPackedDefault::new(); + test.varints = vec![0, 1, 2, 3, 4, 5]; + + // Proto3 packs primitives by default, proto2 does not. + let expected_hex = match TestPackedDefault::descriptor().file_descriptor().syntax() { + Syntax::Proto2 => "08 00 08 01 08 02 08 03 08 04 08 05", + Syntax::Proto3 => "0a 06 00 01 02 03 04 05", + }; + test_serialize_deserialize(expected_hex, &test); +} diff --git a/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed_pb.proto b/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed_pb.proto index c4b543641..76743ea0d 100644 --- a/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed_pb.proto +++ b/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed_pb.proto @@ -13,3 +13,7 @@ message TestUnpacked { message TestIssue281 { repeated fixed32 values = 1 [packed=true]; } + +message TestPackedDefault { + repeated uint32 varints = 1; +}