Skip to content

Commit 1fc4cb1

Browse files
committed
Add tests for deserializing primitives from self-closed tag
1 parent c67b82b commit 1fc4cb1

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

tests/serde-de.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,95 @@ mod trivial {
178178
}
179179

180180
macro_rules! in_struct {
181+
// string, char and bool have some specifics in some tests
182+
(string: $type:ty = $value:expr, $expected:expr) => {
183+
in_struct!(
184+
string: $type = $value, $expected;
185+
186+
#[test]
187+
fn wrapped_empty() {
188+
let item: $type = from_str("<root/>").unwrap();
189+
let expected: $type = "".into();
190+
assert_eq!(item, expected);
191+
}
192+
193+
#[test]
194+
fn field_empty() {
195+
let item: Field<$type> = from_str("<root><value/></root>").unwrap();
196+
assert_eq!(item, Field { value: "".into() });
197+
}
198+
);
199+
};
200+
(char_: $type:ty = $value:expr, $expected:expr) => {
201+
in_struct!(
202+
char_: $type = $value, $expected;
203+
204+
#[test]
205+
fn wrapped_empty() {
206+
match from_str::<$type>("<root/>") {
207+
Err(DeError::Custom(msg)) => assert_eq!(msg, r#"invalid value: string "", expected a character"#),
208+
x => panic!("Expected `Err(Custom(_))`, but got `{:?}`", x),
209+
}
210+
}
211+
212+
#[test]
213+
fn field_empty() {
214+
match from_str::<Field<$type>>("<root><value/></root>") {
215+
Err(DeError::Custom(msg)) => assert_eq!(msg, r#"invalid value: string "", expected a character"#),
216+
x => panic!("Expected `Err(Custom(_))`, but got `{:?}`", x),
217+
}
218+
}
219+
);
220+
};
221+
($name:ident: bool = $value:expr, $expected:expr) => {
222+
in_struct!(
223+
$name: bool = $value, $expected;
224+
225+
#[test]
226+
fn wrapped_empty() {
227+
match from_str::<bool>("<root/>") {
228+
Err(DeError::Custom(msg)) => assert_eq!(msg, r#"invalid type: string "", expected a boolean"#),
229+
x => panic!("Expected `Err(Custom(_))`, but got `{:?}`", x),
230+
}
231+
}
232+
233+
#[test]
234+
fn field_empty() {
235+
match from_str::<Field<bool>>("<root><value/></root>") {
236+
Err(DeError::Custom(msg)) => assert_eq!(msg, r#"invalid type: string "", expected a boolean"#),
237+
x => panic!("Expected `Err(Custom(_))`, but got `{:?}`", x),
238+
}
239+
}
240+
);
241+
};
181242
($name:ident: $type:ty = $value:expr, $expected:expr) => {
243+
in_struct!(
244+
$name: $type = $value, $expected;
245+
246+
#[test]
247+
fn wrapped_empty() {
248+
match from_str::<$type>("<root/>") {
249+
Err(DeError::Custom(msg)) => assert_eq!(msg, concat!(r#"invalid type: string "", expected "#, stringify!($type))),
250+
x => panic!("Expected `Err(Custom(_))`, but got `{:?}`", x),
251+
}
252+
}
253+
254+
#[test]
255+
fn field_empty() {
256+
match from_str::<Field<$type>>("<root><value/></root>") {
257+
Err(DeError::Custom(msg)) => assert_eq!(msg, concat!(r#"invalid type: string "", expected "#, stringify!($type))),
258+
x => panic!("Expected `Err(Custom(_))`, but got `{:?}`", x),
259+
}
260+
}
261+
);
262+
};
263+
($name:ident: $type:ty = $value:expr, $expected:expr; $($specific_tests:item)*) => {
182264
mod $name {
183265
use super::*;
184266
use pretty_assertions::assert_eq;
185267

268+
$($specific_tests)*
269+
186270
#[test]
187271
fn wrapped() {
188272
let item: $type = from_str(&format!("<root>{}</root>", $value)).unwrap();
@@ -278,6 +362,14 @@ mod trivial {
278362
}
279363
}
280364

365+
#[test]
366+
fn text_empty() {
367+
match from_str::<Trivial<$type>>("<root/>") {
368+
Err(DeError::Custom(msg)) => assert_eq!(msg, "missing field `$text`"),
369+
x => panic!("Expected `Err(Custom(_))`, but got `{:?}`", x),
370+
}
371+
}
372+
281373
/// Tests deserialization from top-level tag content: `<root>...content...</root>`
282374
#[test]
283375
fn text() {

0 commit comments

Comments
 (0)