|
| 1 | +#[cfg(feature = "encoding")] |
| 2 | +mod decode { |
| 3 | + use encoding_rs::{UTF_16BE, UTF_16LE, UTF_8}; |
| 4 | + use quick_xml::encoding::*; |
| 5 | + use std::borrow::Cow; |
| 6 | + |
| 7 | + static UTF16BE_TEXT_WITH_BOM: &[u8] = include_bytes!("./documents/utf16be.xml"); |
| 8 | + static UTF16LE_TEXT_WITH_BOM: &[u8] = include_bytes!("./documents/utf16le.xml"); |
| 9 | + static UTF8_TEXT_WITH_BOM: &[u8] = include_bytes!("./documents/utf8.xml"); |
| 10 | + |
| 11 | + static UTF8_TEXT: &str = r#"<?xml version="1.0"?> |
| 12 | +<project name="project-name"> |
| 13 | +</project> |
| 14 | +"#; |
| 15 | + |
| 16 | + #[test] |
| 17 | + fn test_removes_bom() { |
| 18 | + // No BOM |
| 19 | + assert_eq!( |
| 20 | + decode_with_bom_removal(UTF8_TEXT.as_bytes()).unwrap(), |
| 21 | + Cow::Borrowed(UTF8_TEXT) |
| 22 | + ); |
| 23 | + // BOM |
| 24 | + assert_eq!( |
| 25 | + decode_with_bom_removal(UTF8_TEXT_WITH_BOM).unwrap(), |
| 26 | + Cow::Borrowed(UTF8_TEXT) |
| 27 | + ); |
| 28 | + assert_eq!( |
| 29 | + decode_with_bom_removal(UTF16BE_TEXT_WITH_BOM).unwrap(), |
| 30 | + Cow::Borrowed(UTF8_TEXT).into_owned() |
| 31 | + ); |
| 32 | + assert_eq!( |
| 33 | + decode_with_bom_removal(UTF16LE_TEXT_WITH_BOM).unwrap(), |
| 34 | + Cow::Borrowed(UTF8_TEXT).into_owned() |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + #[test] |
| 39 | + fn test_detect_encoding() { |
| 40 | + // No BOM |
| 41 | + assert_eq!(detect_encoding(UTF8_TEXT.as_bytes()), Some(UTF_8)); |
| 42 | + // BOM |
| 43 | + assert_eq!(detect_encoding(UTF8_TEXT_WITH_BOM), Some(UTF_8)); |
| 44 | + assert_eq!(detect_encoding(UTF16BE_TEXT_WITH_BOM), Some(UTF_16BE)); |
| 45 | + assert_eq!(detect_encoding(UTF16LE_TEXT_WITH_BOM), Some(UTF_16LE)); |
| 46 | + } |
| 47 | +} |
0 commit comments