Skip to content

Commit

Permalink
Start writing some tests for decoding functionality
Browse files Browse the repository at this point in the history
Fix up descriptions on some decoding functions
  • Loading branch information
dralley committed Aug 14, 2022
1 parent 380826e commit 6666237
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
9 changes: 3 additions & 6 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ impl Decoder {
}
}

/// Decodes the provided bytes using the specified encoding, ignoring the BOM
/// if it is present in the `bytes`.
/// Decodes the provided bytes using the specified encoding.
///
/// Returns an error in case of malformed sequences in the `bytes`.
/// Returns an error in case of malformed or non-representable sequences in the `bytes`.
#[cfg(feature = "encoding")]
pub fn decode<'b>(bytes: &'b [u8], encoding: &'static Encoding) -> Result<Cow<'b, str>> {
encoding
Expand All @@ -119,7 +118,7 @@ pub fn decode<'b>(bytes: &'b [u8], encoding: &'static Encoding) -> Result<Cow<'b
/// Decodes a slice with an unknown encoding, removing the BOM if it is present
/// in the bytes.
///
/// Returns an error in case of malformed sequences in the `bytes`.
/// Returns an error in case of malformed or non-representable sequences in the `bytes`.
#[cfg(feature = "encoding")]
pub fn decode_with_bom_removal<'b>(bytes: &'b [u8]) -> Result<Cow<'b, str>> {
if let Some(encoding) = detect_encoding(bytes) {
Expand Down Expand Up @@ -185,5 +184,3 @@ pub fn detect_encoding(bytes: &[u8]) -> Option<&'static Encoding> {
_ => None,
}
}

// TODO: add some tests for functions
3 changes: 3 additions & 0 deletions tests/documents/utf8.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0"?>
<project name="project-name">
</project>
47 changes: 47 additions & 0 deletions tests/encodings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#[cfg(feature = "encoding")]
mod decode {
use encoding_rs::{UTF_16BE, UTF_16LE, UTF_8};
use quick_xml::encoding::*;
use std::borrow::Cow;

static UTF16BE_TEXT_WITH_BOM: &[u8] = include_bytes!("./documents/utf16be.xml");
static UTF16LE_TEXT_WITH_BOM: &[u8] = include_bytes!("./documents/utf16le.xml");
static UTF8_TEXT_WITH_BOM: &[u8] = include_bytes!("./documents/utf8.xml");

static UTF8_TEXT: &str = r#"<?xml version="1.0"?>
<project name="project-name">
</project>
"#;

#[test]
fn test_removes_bom() {
// No BOM
assert_eq!(
decode_with_bom_removal(UTF8_TEXT.as_bytes()).unwrap(),
Cow::Borrowed(UTF8_TEXT)
);
// BOM
assert_eq!(
decode_with_bom_removal(UTF8_TEXT_WITH_BOM).unwrap(),
Cow::Borrowed(UTF8_TEXT)
);
assert_eq!(
decode_with_bom_removal(UTF16BE_TEXT_WITH_BOM).unwrap(),
Cow::Borrowed(UTF8_TEXT).into_owned()
);
assert_eq!(
decode_with_bom_removal(UTF16LE_TEXT_WITH_BOM).unwrap(),
Cow::Borrowed(UTF8_TEXT).into_owned()
);
}

#[test]
fn test_detect_encoding() {
// No BOM
assert_eq!(detect_encoding(UTF8_TEXT.as_bytes()), Some(UTF_8));
// BOM
assert_eq!(detect_encoding(UTF8_TEXT_WITH_BOM), Some(UTF_8));
assert_eq!(detect_encoding(UTF16BE_TEXT_WITH_BOM), Some(UTF_16BE));
assert_eq!(detect_encoding(UTF16LE_TEXT_WITH_BOM), Some(UTF_16LE));
}
}
18 changes: 0 additions & 18 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,6 @@ fn test_comment_starting_with_gt() {
}
}

#[test]
#[cfg(feature = "encoding")]
fn test_koi8_r_encoding() {
let src = include_bytes!("documents/opennews_all.rss").as_ref();
let mut buf = vec![];
let mut r = Reader::from_reader(src);
r.trim_text(true).expand_empty_elements(false);
loop {
match r.read_event_into(&mut buf) {
Ok(Text(e)) => {
e.unescape().unwrap();
}
Ok(Eof) => break,
_ => (),
}
}
}

#[test]
fn fuzz_53() {
let data: &[u8] = b"\xe9\x00\x00\x00\x00\x00\x00\x00\x00\
Expand Down

0 comments on commit 6666237

Please sign in to comment.