Debian Codegraph @

tests/codec_tests.rs

raw symbols SCIP
1 //! BOM parsing tests
2 //!
3 use libxml::parser::{Parser, XmlParseError};
4 use libxml::tree::Document;
5 use std::fs;
6 use std::io;
7 use std::io::prelude::*;
8
9 // HELPERS
10
11 ///Read the entire file to a byte vector. Similar to read_to_string with
12 ///no encoding assumption.
13 fn read_to_end(path: &str) -> io::Result<Vec<u8>> {
14 let mut buffer = Vec::new();
15 let mut file = fs::File::open(path)?;
16 file.read_to_end(&mut buffer)?;
17 Ok(buffer)
18 }
19
20 ///Generate a unittest for a document result from parsing a variant of file01.
21 fn file01_test(doc_result: Result<Document, XmlParseError>) {
22 assert!(doc_result.is_ok());
23 let doc = doc_result.unwrap();
24 let root = doc.get_root_element().unwrap();
25
26 // Tests
27 let root_children = root.get_child_nodes();
28 assert_eq!(root_children.len(), 5, "file01 root has five child nodes");
29 let mut element_children = root.get_child_elements();
30 assert_eq!(
31 element_children.len(),
32 2,
33 "file01 root has two child elements"
34 );
35 assert_eq!(element_children.pop().unwrap().get_name(), "child");
36 assert_eq!(element_children.pop().unwrap().get_name(), "child");
37 assert!(element_children.is_empty());
38 }
39
40 ///Run a test for both the file and the path of file01.
41 fn run_test(path: &str) {
42 let parser = Parser::default();
43 file01_test(parser.parse_file(path));
44
45 let input = read_to_end(path).unwrap();
46 file01_test(parser.parse_string(&input));
47 }
48
49 // ENCODINGS
50
51 #[test]
52 fn utf8_test() {
53 run_test("tests/resources/file01.xml");
54 }
55
56 #[test]
57 fn utf16le_test() {
58 run_test("tests/resources/file01_utf16le.xml");
59 }
60
61 #[test]
62 fn utf16be_test() {
63 run_test("tests/resources/file01_utf16be.xml");
64 }
65
66 // BOM
67
68 #[test]
69 fn utf8_bom_test() {
70 run_test("tests/resources/file01_utf8_bom.xml");
71 }
72
73 #[test]
74 fn utf16le_bom_test() {
75 run_test("tests/resources/file01_utf16le_bom.xml");
76 }
77
78 #[test]
79 fn utf16be_bom_test() {
80 run_test("tests/resources/file01_utf16be_bom.xml");
81 }
82
83 // UNICODE PATHS
84
85 #[test]
86 fn nonbmp_path_test() {
87 run_test("tests/resources/file01_🔥🔥🔥.xml");
88 }