Skip to content

Commit db867f3

Browse files
committed
msgpack: add test for modifying struct fields
1 parent 8c6798c commit db867f3

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

packages/std/src/msgpack.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,68 @@ mod tests {
123123
let deserialized: SomeMsg = from_msgpack(serialized).unwrap();
124124
assert_eq!(deserialized, msg);
125125
}
126+
127+
#[test]
128+
#[should_panic(expected = "invalid type: string \\\"foo\\\", expected u32")]
129+
fn deserialize_different_field_order_unsupported() {
130+
#[derive(Serialize, Deserialize, Debug, PartialEq)]
131+
struct TestV1 {
132+
a: String,
133+
b: u32,
134+
c: u64,
135+
}
136+
137+
#[derive(Serialize, Deserialize, Debug, PartialEq)]
138+
struct TestV2 {
139+
b: u32,
140+
c: u64,
141+
a: String,
142+
}
143+
144+
let v1 = TestV1 {
145+
a: "foo".to_string(),
146+
b: 42,
147+
c: 18446744073709551615,
148+
};
149+
150+
let v2: TestV2 = from_msgpack(to_msgpack_vec(&v1).unwrap()).unwrap();
151+
assert_eq!(
152+
v2,
153+
TestV2 {
154+
b: 42,
155+
c: 18446744073709551615,
156+
a: "foo".to_string()
157+
}
158+
);
159+
}
160+
161+
#[test]
162+
fn deserialize_new_fields() {
163+
// fields can be added, but only to the end of the struct
164+
165+
#[derive(Serialize, Deserialize, Debug, PartialEq)]
166+
struct TestV1 {
167+
a: String,
168+
}
169+
170+
#[derive(Serialize, Deserialize, Debug, PartialEq)]
171+
struct TestV2 {
172+
a: String,
173+
#[serde(default)]
174+
b: u32,
175+
}
176+
177+
let v1 = TestV1 {
178+
a: "foo".to_string(),
179+
};
180+
let v2: TestV2 = from_msgpack(to_msgpack_vec(&v1).unwrap()).unwrap();
181+
182+
assert_eq!(
183+
v2,
184+
TestV2 {
185+
a: "foo".to_string(),
186+
b: 0
187+
}
188+
);
189+
}
126190
}

0 commit comments

Comments
 (0)