|
1 | 1 | use std::str::from_utf8;
|
2 | 2 |
|
3 | 3 | use quick_xml::events::{BytesCData, BytesEnd, BytesStart, BytesText, Event::*};
|
| 4 | +use quick_xml::name::QName; |
4 | 5 | use quick_xml::reader::Reader;
|
5 | 6 |
|
6 | 7 | use pretty_assertions::assert_eq;
|
@@ -265,3 +266,98 @@ mod double_dash {
|
265 | 266 | assert_eq!(r.read_event().unwrap(), End(BytesEnd::new("hello")));
|
266 | 267 | }
|
267 | 268 | }
|
| 269 | + |
| 270 | +/// This tests checks that read_to_end() correctly returns span even when |
| 271 | +/// text is trimmed from both sides |
| 272 | +mod read_to_end { |
| 273 | + use super::*; |
| 274 | + |
| 275 | + mod borrowed { |
| 276 | + use super::*; |
| 277 | + use pretty_assertions::assert_eq; |
| 278 | + |
| 279 | + #[test] |
| 280 | + fn text() { |
| 281 | + let mut r = Reader::from_str("<tag> text </tag>"); |
| 282 | + // ^0 ^5 ^11 |
| 283 | + r.config_mut().trim_text(true); |
| 284 | + |
| 285 | + assert_eq!(r.read_event().unwrap(), Start(BytesStart::new("tag"))); |
| 286 | + assert_eq!(r.read_to_end(QName(b"tag")).unwrap(), 5..11); |
| 287 | + assert_eq!(r.read_event().unwrap(), Eof); |
| 288 | + } |
| 289 | + |
| 290 | + #[test] |
| 291 | + fn tag() { |
| 292 | + let mut r = Reader::from_str("<tag> <nested/> </tag>"); |
| 293 | + // ^0 ^5 ^16 |
| 294 | + r.config_mut().trim_text(true); |
| 295 | + |
| 296 | + assert_eq!(r.read_event().unwrap(), Start(BytesStart::new("tag"))); |
| 297 | + assert_eq!(r.read_to_end(QName(b"tag")).unwrap(), 5..16); |
| 298 | + assert_eq!(r.read_event().unwrap(), Eof); |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + mod buffered { |
| 303 | + use super::*; |
| 304 | + use pretty_assertions::assert_eq; |
| 305 | + |
| 306 | + #[test] |
| 307 | + fn text() { |
| 308 | + let mut r = Reader::from_str("<tag> text </tag>"); |
| 309 | + // ^0 ^5 ^11 |
| 310 | + r.config_mut().trim_text(true); |
| 311 | + |
| 312 | + let mut buf = Vec::new(); |
| 313 | + assert_eq!( |
| 314 | + r.read_event_into(&mut buf).unwrap(), |
| 315 | + Start(BytesStart::new("tag")) |
| 316 | + ); |
| 317 | + assert_eq!(r.read_to_end_into(QName(b"tag"), &mut buf).unwrap(), 5..11); |
| 318 | + assert_eq!(r.read_event_into(&mut buf).unwrap(), Eof); |
| 319 | + } |
| 320 | + |
| 321 | + #[test] |
| 322 | + fn tag() { |
| 323 | + let mut r = Reader::from_str("<tag> <nested/> </tag>"); |
| 324 | + // ^0 ^5 ^16 |
| 325 | + r.config_mut().trim_text(true); |
| 326 | + |
| 327 | + let mut buf = Vec::new(); |
| 328 | + assert_eq!( |
| 329 | + r.read_event_into(&mut buf).unwrap(), |
| 330 | + Start(BytesStart::new("tag")) |
| 331 | + ); |
| 332 | + assert_eq!(r.read_to_end_into(QName(b"tag"), &mut buf).unwrap(), 5..16); |
| 333 | + assert_eq!(r.read_event_into(&mut buf).unwrap(), Eof); |
| 334 | + } |
| 335 | + } |
| 336 | +} |
| 337 | + |
| 338 | +/// This tests checks that read_text() correctly returns text even when |
| 339 | +/// text is trimmed from both sides |
| 340 | +mod read_text { |
| 341 | + use super::*; |
| 342 | + use pretty_assertions::assert_eq; |
| 343 | + |
| 344 | + #[test] |
| 345 | + fn text() { |
| 346 | + let mut r = Reader::from_str("<tag> text </tag>"); |
| 347 | + r.config_mut().trim_text(true); |
| 348 | + |
| 349 | + assert_eq!(r.read_event().unwrap(), Start(BytesStart::new("tag"))); |
| 350 | + assert_eq!(r.read_text(QName(b"tag")).unwrap(), " text "); |
| 351 | + assert_eq!(r.read_event().unwrap(), Eof); |
| 352 | + } |
| 353 | + |
| 354 | + #[test] |
| 355 | + fn tag() { |
| 356 | + let mut r = Reader::from_str("<tag> <nested/> </tag>"); |
| 357 | + r.config_mut().trim_text(true); |
| 358 | + |
| 359 | + assert_eq!(r.read_event().unwrap(), Start(BytesStart::new("tag"))); |
| 360 | + assert_eq!(r.read_text(QName(b"tag")).unwrap(), " <nested/> "); |
| 361 | + assert_eq!(r.read_event().unwrap(), Eof); |
| 362 | + } |
| 363 | +} |
0 commit comments