Skip to content

Commit e7f30d2

Browse files
authored
Merge pull request #434 from Mingun/serde-tests-cleanup
Remove excessive serde tests and fix a bug
2 parents a5da628 + 85a569d commit e7f30d2

File tree

3 files changed

+40
-134
lines changed

3 files changed

+40
-134
lines changed

Changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
- [#421]: Fixed unknown bug in serde deserialization of externally tagged enums
5959
when an enum variant represented as a `Text` event (i.e. `<xml>tag</xml>`)
6060
and a document encoding is not an UTF-8
61+
- [#434]: Fixed incorrect error generated in some cases by serde deserializer
6162

6263
### Misc Changes
6364

@@ -158,6 +159,7 @@
158159
- [#363]: Add tests for `Reader::read_event_impl` to ensure that proper events generated for corresponding inputs
159160
- [#407]: Improved benchmark suite to cover whole-document parsing, escaping and unescaping text
160161
- [#418]: Parameterized macrobenchmarks and comparative benchmarks, added throughput measurements via criterion
162+
- [#434]: Added more tests for serde deserialier
161163

162164
[#8]: https://github.com/Mingun/fast-xml/pull/8
163165
[#9]: https://github.com/Mingun/fast-xml/pull/9
@@ -178,6 +180,7 @@
178180
[#418]: https://github.com/tafia/quick-xml/pull/418
179181
[#421]: https://github.com/tafia/quick-xml/pull/421
180182
[#423]: https://github.com/tafia/quick-xml/pull/423
183+
[#434]: https://github.com/tafia/quick-xml/pull/434
181184
[#437]: https://github.com/tafia/quick-xml/pull/437
182185

183186
## 0.23.0 -- 2022-05-08

src/de/map.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,13 @@ where
298298
};
299299
key.map(Some)
300300
}
301-
_ => Ok(None),
301+
// Stop iteration after reaching a closing tag
302+
DeEvent::End(e) if e.name() == self.start.name() => Ok(None),
303+
// This is a unmatched closing tag, so the XML is invalid
304+
DeEvent::End(e) => Err(DeError::UnexpectedEnd(e.name().as_ref().to_owned())),
305+
// We cannot get `Eof` legally, because we always inside of the
306+
// opened tag `self.start`
307+
DeEvent::Eof => Err(DeError::UnexpectedEof),
302308
}
303309
}
304310
}

tests/serde-de.rs

+30-133
Original file line numberDiff line numberDiff line change
@@ -43,115 +43,6 @@ fn string_borrow() {
4343
assert_eq!(borrowed_item.text, "Hello world");
4444
}
4545

46-
#[derive(Debug, Deserialize, PartialEq)]
47-
struct Item {
48-
name: String,
49-
source: String,
50-
}
51-
52-
#[test]
53-
fn multiple_roots_attributes() {
54-
let item: Vec<Item> = from_str(
55-
r#"
56-
<item name="hello1" source="world1.rs" />
57-
<item name="hello2" source="world2.rs" />
58-
"#,
59-
)
60-
.unwrap();
61-
assert_eq!(
62-
item,
63-
vec![
64-
Item {
65-
name: "hello1".to_string(),
66-
source: "world1.rs".to_string(),
67-
},
68-
Item {
69-
name: "hello2".to_string(),
70-
source: "world2.rs".to_string(),
71-
},
72-
]
73-
);
74-
}
75-
76-
#[test]
77-
fn nested_collection() {
78-
#[derive(Debug, Deserialize, PartialEq)]
79-
struct Project {
80-
name: String,
81-
82-
#[serde(rename = "item", default)]
83-
items: Vec<Item>,
84-
}
85-
86-
let project: Project = from_str(
87-
r#"
88-
<project name="my_project">
89-
<item name="hello1" source="world1.rs" />
90-
<item name="hello2" source="world2.rs" />
91-
</project>
92-
"#,
93-
)
94-
.unwrap();
95-
assert_eq!(
96-
project,
97-
Project {
98-
name: "my_project".to_string(),
99-
items: vec![
100-
Item {
101-
name: "hello1".to_string(),
102-
source: "world1.rs".to_string(),
103-
},
104-
Item {
105-
name: "hello2".to_string(),
106-
source: "world2.rs".to_string(),
107-
},
108-
],
109-
}
110-
);
111-
}
112-
113-
#[test]
114-
fn collection_of_enums() {
115-
#[derive(Debug, Deserialize, PartialEq)]
116-
enum MyEnum {
117-
A(String),
118-
B { name: String, flag: bool },
119-
C,
120-
}
121-
122-
#[derive(Debug, Deserialize, PartialEq)]
123-
struct MyEnums {
124-
// TODO: This should be #[serde(flatten)], but right now serde don't support flattening of sequences
125-
// See https://github.com/serde-rs/serde/issues/1905
126-
#[serde(rename = "$value")]
127-
items: Vec<MyEnum>,
128-
}
129-
130-
let s = r#"
131-
<enums>
132-
<A>test</A>
133-
<B name="hello" flag="t" />
134-
<C />
135-
</enums>
136-
"#;
137-
138-
let project: MyEnums = from_str(s).unwrap();
139-
140-
assert_eq!(
141-
project,
142-
MyEnums {
143-
items: vec![
144-
MyEnum::A("test".to_string()),
145-
MyEnum::B {
146-
name: "hello".to_string(),
147-
flag: true,
148-
},
149-
MyEnum::C,
150-
],
151-
}
152-
);
153-
}
154-
15546
/// Test for https://github.com/tafia/quick-xml/issues/231
15647
#[test]
15748
fn implicit_value() {
@@ -3589,6 +3480,18 @@ macro_rules! maplike_errors {
35893480
mod non_closed {
35903481
use super::*;
35913482

3483+
/// For struct we expect that error about not closed tag appears
3484+
/// earlier than error about missing fields
3485+
#[test]
3486+
fn missing_field() {
3487+
let data = from_str::<$type>(r#"<root>"#);
3488+
3489+
match data {
3490+
Err(DeError::UnexpectedEof) => (),
3491+
_ => panic!("Expected `UnexpectedEof`, found {:?}", data),
3492+
}
3493+
}
3494+
35923495
#[test]
35933496
fn attributes() {
35943497
let data = from_str::<$type>(r#"<root float="42" string="answer">"#);
@@ -3624,6 +3527,18 @@ macro_rules! maplike_errors {
36243527
use super::*;
36253528
use quick_xml::Error::EndEventMismatch;
36263529

3530+
/// For struct we expect that error about mismatched tag appears
3531+
/// earlier than error about missing fields
3532+
#[test]
3533+
fn missing_field() {
3534+
let data = from_str::<$type>(r#"<root></mismatched>"#);
3535+
3536+
match data {
3537+
Err(DeError::InvalidXml(EndEventMismatch { .. })) => (),
3538+
_ => panic!("Expected `InvalidXml(EndEventMismatch)`, found {:?}", data),
3539+
}
3540+
}
3541+
36273542
#[test]
36283543
fn attributes() {
36293544
let data = from_str::<$type>(
@@ -3922,6 +3837,12 @@ mod flatten_struct {
39223837
mod enum_ {
39233838
use super::*;
39243839

3840+
#[derive(Debug, Deserialize, PartialEq)]
3841+
struct Nested {
3842+
//TODO: change to f64 after fixing https://github.com/serde-rs/serde/issues/1183
3843+
float: String,
3844+
}
3845+
39253846
mod externally_tagged {
39263847
use super::*;
39273848
use pretty_assertions::assert_eq;
@@ -3947,12 +3868,6 @@ mod enum_ {
39473868
},
39483869
}
39493870

3950-
#[derive(Debug, Deserialize, PartialEq)]
3951-
struct Nested {
3952-
//TODO: change to f64 after fixing https://github.com/serde-rs/serde/issues/1183
3953-
float: String,
3954-
}
3955-
39563871
/// Workaround for serde bug https://github.com/serde-rs/serde/issues/1904
39573872
#[derive(Debug, Deserialize, PartialEq)]
39583873
enum Workaround {
@@ -4120,12 +4035,6 @@ mod enum_ {
41204035
value: bool,
41214036
}
41224037

4123-
#[derive(Debug, Deserialize, PartialEq)]
4124-
struct Nested {
4125-
//TODO: change to f64 after fixing https://github.com/serde-rs/serde/issues/1183
4126-
float: String,
4127-
}
4128-
41294038
mod unit {
41304039
use super::*;
41314040
use pretty_assertions::assert_eq;
@@ -4306,12 +4215,6 @@ mod enum_ {
43064215
},
43074216
}
43084217

4309-
#[derive(Debug, Deserialize, PartialEq)]
4310-
struct Nested {
4311-
//TODO: change to f64 after fixing https://github.com/serde-rs/serde/issues/1183
4312-
float: String,
4313-
}
4314-
43154218
/// Workaround for serde bug https://github.com/serde-rs/serde/issues/1904
43164219
#[derive(Debug, Deserialize, PartialEq)]
43174220
#[serde(tag = "tag", content = "content")]
@@ -4524,12 +4427,6 @@ mod enum_ {
45244427
},
45254428
}
45264429

4527-
#[derive(Debug, Deserialize, PartialEq)]
4528-
struct Nested {
4529-
//TODO: change to f64 after fixing https://github.com/serde-rs/serde/issues/1183
4530-
float: String,
4531-
}
4532-
45334430
/// Workaround for serde bug https://github.com/serde-rs/serde/issues/1904
45344431
#[derive(Debug, Deserialize, PartialEq)]
45354432
#[serde(untagged)]

0 commit comments

Comments
 (0)