|
| 1 | +//! Tests for skipping fields when using prost-derive. |
| 2 | +
|
| 3 | +use crate::alloc::string::ToString; |
| 4 | +use crate::check_serialize_equivalent; |
| 5 | +use alloc::collections::BTreeMap; |
| 6 | +use alloc::string::String; |
| 7 | +use prost::Message; |
| 8 | + |
| 9 | +/// A struct with the same data as another, but with a skipped field, should be equal when encoded. |
| 10 | +#[test] |
| 11 | +fn skipped_field_serial_equality() { |
| 12 | + #[derive(Clone, PartialEq, prost::Message)] |
| 13 | + struct TypeWithoutSkippedField { |
| 14 | + #[prost(string, tag = "1")] |
| 15 | + value: String, |
| 16 | + } |
| 17 | + |
| 18 | + fn create_hashmap() -> BTreeMap<String, String> { |
| 19 | + let mut map = BTreeMap::new(); |
| 20 | + map.insert("key".to_string(), "value".to_string()); |
| 21 | + map |
| 22 | + } |
| 23 | + |
| 24 | + #[derive(Clone, PartialEq, prost::Message)] |
| 25 | + struct TypeWithSkippedField { |
| 26 | + #[prost(string, tag = "1")] |
| 27 | + value: String, |
| 28 | + #[prost(skip, default = "create_hashmap")] |
| 29 | + pub temp_data: BTreeMap<String, String>, // This field will be skipped |
| 30 | + } |
| 31 | + |
| 32 | + let a = TypeWithoutSkippedField { |
| 33 | + value: "hello".to_string(), |
| 34 | + }; |
| 35 | + let b = TypeWithSkippedField { |
| 36 | + value: "hello".to_string(), |
| 37 | + temp_data: create_hashmap(), |
| 38 | + }; |
| 39 | + |
| 40 | + // Encoded forms should be equal |
| 41 | + check_serialize_equivalent(&a, &b); |
| 42 | + |
| 43 | + // Decoded forms should be equal, with the skipped field initialized using the default attribute |
| 44 | + let decoded = TypeWithSkippedField::decode(a.encode_to_vec().as_slice()).unwrap(); |
| 45 | + assert_eq!(b, decoded); |
| 46 | +} |
0 commit comments