| 1 |
|
| 2 |
|
| 3 |
use std::env; |
| 4 |
use std::fs::File; |
| 5 |
use std::io::Read; |
| 6 |
|
| 7 |
use libxml::parser::{Parser, ParserOptions}; |
| 8 |
use libxml::tree::{Document, Node, SaveOptions}; |
| 9 |
|
| 10 |
#[test] |
| 11 |
|
| 12 |
fn hello_builder() { |
| 13 |
let doc_result = Document::new(); |
| 14 |
assert!(doc_result.is_ok()); |
| 15 |
let mut doc = doc_result.unwrap(); |
| 16 |
|
| 17 |
|
| 18 |
let doc_node = doc.get_root_element(); |
| 19 |
assert_eq!(doc_node, None, "empty document has no root element"); |
| 20 |
|
| 21 |
let hello_element_result = Node::new("hello", None, &doc); |
| 22 |
assert!(hello_element_result.is_ok()); |
| 23 |
let mut hello_element = hello_element_result.unwrap(); |
| 24 |
|
| 25 |
assert!(hello_element.set_content("world!").is_ok()); |
| 26 |
|
| 27 |
doc.set_root_element(&hello_element); |
| 28 |
|
| 29 |
assert!(hello_element.set_content("world!").is_ok()); |
| 30 |
|
| 31 |
let added = hello_element.new_child(None, "child"); |
| 32 |
assert!(added.is_ok()); |
| 33 |
let mut new_child = added.unwrap(); |
| 34 |
|
| 35 |
assert!(new_child.set_content("set content").is_ok()); |
| 36 |
|
| 37 |
assert_eq!(new_child.get_content(), "set content"); |
| 38 |
assert_eq!(hello_element.get_content(), "world!set content"); |
| 39 |
|
| 40 |
let node_string = doc.node_to_string(&hello_element); |
| 41 |
assert!(node_string.len() > 1); |
| 42 |
|
| 43 |
assert!(hello_element.set_name("world").is_ok()); |
| 44 |
assert_eq!(hello_element.get_name(), "world"); |
| 45 |
|
| 46 |
let doc_string = doc.to_string(); |
| 47 |
assert!(doc_string.len() > 1); |
| 48 |
let output_path = env::temp_dir().join("rust_libxml_tests_helloworld.xml"); |
| 49 |
assert!(doc.save_file(&output_path.display().to_string()).is_ok()); |
| 50 |
} |
| 51 |
|
| 52 |
#[test] |
| 53 |
fn create_pi() { |
| 54 |
let doc_result = Document::new(); |
| 55 |
assert!(doc_result.is_ok()); |
| 56 |
let mut doc = doc_result.unwrap(); |
| 57 |
|
| 58 |
let node_ok: Result<Node, ()> = doc.create_processing_instruction("piname", "picontent"); |
| 59 |
assert!(node_ok.is_ok()); |
| 60 |
assert_eq!(node_ok.unwrap().get_content(), "picontent"); |
| 61 |
let doc_string = doc.to_string(); |
| 62 |
assert!(doc_string.len() > 1); |
| 63 |
} |
| 64 |
|
| 65 |
#[test] |
| 66 |
|
| 67 |
fn duplicate_file() { |
| 68 |
let parser = Parser::default(); |
| 69 |
{ |
| 70 |
let doc_result = parser.parse_file("tests/resources/file01.xml"); |
| 71 |
assert!(doc_result.is_ok()); |
| 72 |
|
| 73 |
let doc = doc_result.unwrap(); |
| 74 |
let output_path = env::temp_dir().join("rust_libxml_tests_copy.xml"); |
| 75 |
doc.save_file(&output_path.display().to_string()).unwrap(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
#[test] |
| 80 |
|
| 81 |
fn can_parse_xml_string() { |
| 82 |
let mut file = File::open("tests/resources/file01.xml").unwrap(); |
| 83 |
let mut xml_string = String::new(); |
| 84 |
file.read_to_string(&mut xml_string).unwrap(); |
| 85 |
let parser = Parser::default(); |
| 86 |
let doc = parser.parse_string(&xml_string).unwrap(); |
| 87 |
assert_eq!(doc.get_root_element().unwrap().get_name(), "root"); |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
|
| 92 |
fn can_load_html_file() { |
| 93 |
let parser = Parser::default_html(); |
| 94 |
{ |
| 95 |
let doc_result = parser.parse_file("tests/resources/example.html"); |
| 96 |
assert!(doc_result.is_ok()); |
| 97 |
|
| 98 |
let doc = doc_result.unwrap(); |
| 99 |
let root = doc.get_root_element().unwrap(); |
| 100 |
assert_eq!(root.get_name(), "html"); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
fn create_test_document(file: Option<&str>) -> Document { |
| 105 |
let parser = Parser::default(); |
| 106 |
let doc_result = parser.parse_file(file.unwrap_or("tests/resources/file01.xml")); |
| 107 |
assert!(doc_result.is_ok()); |
| 108 |
doc_result.unwrap() |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
fn document_can_import_node() { |
| 113 |
let doc1 = create_test_document(None); |
| 114 |
let mut doc2 = create_test_document(None); |
| 115 |
|
| 116 |
assert_eq!( |
| 117 |
doc2.get_root_element().unwrap().get_child_elements().len(), |
| 118 |
2 |
| 119 |
); |
| 120 |
|
| 121 |
let mut elements = doc1.get_root_element().unwrap().get_child_elements(); |
| 122 |
let mut node = elements.pop().unwrap(); |
| 123 |
node.unlink(); |
| 124 |
let mut imported = doc2.import_node(&mut node).unwrap(); |
| 125 |
assert!(doc2 |
| 126 |
.get_root_element() |
| 127 |
.unwrap() |
| 128 |
.add_child(&mut imported) |
| 129 |
.is_ok()); |
| 130 |
|
| 131 |
assert_eq!( |
| 132 |
doc2.get_root_element().unwrap().get_child_elements().len(), |
| 133 |
3 |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
#[test] |
| 138 |
fn document_formatted_serialization() { |
| 139 |
let doc = create_test_document(Some("tests/resources/unformatted.xml")); |
| 140 |
let doc_str = doc.to_string(); |
| 141 |
|
| 142 |
assert!(doc_str.len() > 370); |
| 143 |
let doc_str_formatted = doc.to_string_with_options(SaveOptions { |
| 144 |
format: true, |
| 145 |
..SaveOptions::default() |
| 146 |
}); |
| 147 |
assert!(doc_str_formatted.len() > 460); |
| 148 |
|
| 149 |
assert!(doc_str_formatted.len() > doc_str.len()); |
| 150 |
} |
| 151 |
|
| 152 |
#[test] |
| 153 |
|
| 154 |
|
| 155 |
fn well_formed_html() { |
| 156 |
let parser = Parser::default_html(); |
| 157 |
|
| 158 |
let trivial_well_formed = |
| 159 |
parser.is_well_formed_html("<!DOCTYPE html>\n<html><head></head><body></body></html>"); |
| 160 |
assert!(trivial_well_formed); |
| 161 |
|
| 162 |
let trivial_ill_formed = parser.is_well_formed_html("garbage"); |
| 163 |
assert!(!trivial_ill_formed); |
| 164 |
|
| 165 |
let should_ill_formed = parser.is_well_formed_html("<broken <markup>> </boom>"); |
| 166 |
assert!(!should_ill_formed); |
| 167 |
|
| 168 |
let should_well_formed = parser.is_well_formed_html("<!DOCTYPE html>\n<html><head><title>Test</title></head><body>\n<h1>Tiny</h1><math><mn>2</mn></math></body></html>"); |
| 169 |
assert!(should_well_formed); |
| 170 |
} |
| 171 |
|
| 172 |
#[test] |
| 173 |
|
| 174 |
fn html_fragment() { |
| 175 |
let fragment = r#"<figure><a href="tar-flac-subset-compress.svg"><img src="tar-flac-subset-compress.svg" alt="Compression results on incompressible data."></a><figcaption><p>Compression results on incompressible data.</p></figcaption></figure>"#; |
| 176 |
|
| 177 |
let parser = Parser::default_html(); |
| 178 |
let document = parser |
| 179 |
.parse_string_with_options( |
| 180 |
fragment, |
| 181 |
ParserOptions { |
| 182 |
no_def_dtd: true, |
| 183 |
no_implied: true, |
| 184 |
..Default::default() |
| 185 |
}, |
| 186 |
) |
| 187 |
.unwrap(); |
| 188 |
|
| 189 |
let mut serialized_fragment = document.to_string_with_options(SaveOptions { |
| 190 |
no_empty_tags: true, |
| 191 |
as_html: true, |
| 192 |
..Default::default() |
| 193 |
}); |
| 194 |
let _added_newline = serialized_fragment.pop(); |
| 195 |
|
| 196 |
assert_eq!(fragment, serialized_fragment); |
| 197 |
} |
| 198 |
|
| 199 |
fn serialization_roundtrip(file_name: &str) { |
| 200 |
let file_result = std::fs::read_to_string(file_name); |
| 201 |
assert!(file_result.is_ok()); |
| 202 |
let xml_file = file_result.unwrap(); |
| 203 |
|
| 204 |
let parser = Parser::default(); |
| 205 |
let parse_result = parser.parse_string(xml_file.as_bytes()); |
| 206 |
assert!(parse_result.is_ok()); |
| 207 |
let doc = parse_result.unwrap(); |
| 208 |
|
| 209 |
let doc_str = doc.to_string(); |
| 210 |
|
| 211 |
assert_eq!(strip_whitespace(&xml_file), strip_whitespace(&doc_str)); |
| 212 |
} |
| 213 |
|
| 214 |
fn strip_whitespace(string: &str) -> String { |
| 215 |
string.replace("\r","") |
| 216 |
.replace("\n", "") |
| 217 |
.replace(" ", "") |
| 218 |
} |
| 219 |
|
| 220 |
#[test] |
| 221 |
fn simple_serialization_test01() { |
| 222 |
serialization_roundtrip("tests/resources/file01.xml"); |
| 223 |
} |
| 224 |
|
| 225 |
#[test] |
| 226 |
fn simple_serialization_unformatted() { |
| 227 |
serialization_roundtrip("tests/resources/unformatted.xml"); |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
fn simple_serialization_namespaces() { |
| 232 |
serialization_roundtrip("tests/resources/simple_namespaces.xml"); |
| 233 |
} |
| 234 |
|
| 235 |
#[test] |
| 236 |
fn serialization_no_empty() { |
| 237 |
let source_result = std::fs::read_to_string("tests/resources/empty_tags.xml"); |
| 238 |
assert!(source_result.is_ok()); |
| 239 |
let source_file = source_result.unwrap(); |
| 240 |
|
| 241 |
let result = std::fs::read_to_string("tests/resources/empty_tags_result.xml"); |
| 242 |
assert!(result.is_ok()); |
| 243 |
let result_file = result.unwrap(); |
| 244 |
|
| 245 |
let options = SaveOptions { |
| 246 |
no_empty_tags: true, |
| 247 |
..SaveOptions::default() |
| 248 |
}; |
| 249 |
|
| 250 |
let parser = Parser::default(); |
| 251 |
let parse_result = parser.parse_string(source_file.as_bytes()); |
| 252 |
assert!(parse_result.is_ok()); |
| 253 |
let doc = parse_result.unwrap(); |
| 254 |
|
| 255 |
let doc_str = doc.to_string_with_options(options); |
| 256 |
|
| 257 |
assert_eq!(strip_whitespace(&result_file), strip_whitespace(&doc_str)); |
| 258 |
} |
| 259 |
|
| 260 |
#[test] |
| 261 |
fn serialization_as_html() { |
| 262 |
let source_result = std::fs::read_to_string("tests/resources/as_html.xml"); |
| 263 |
assert!(source_result.is_ok()); |
| 264 |
let source_file = source_result.unwrap(); |
| 265 |
|
| 266 |
let result_legacy = std::fs::read_to_string("tests/resources/as_html_result.xml"); |
| 267 |
assert!(result_legacy.is_ok()); |
| 268 |
let result_file_legacy = result_legacy.unwrap(); |
| 269 |
|
| 270 |
|
| 271 |
let result = std::fs::read_to_string("tests/resources/as_html_result_2.15.xml"); |
| 272 |
assert!(result.is_ok()); |
| 273 |
let result_file = result.unwrap(); |
| 274 |
|
| 275 |
let options = SaveOptions { |
| 276 |
as_html: true, |
| 277 |
..SaveOptions::default() |
| 278 |
}; |
| 279 |
|
| 280 |
let parser = Parser::default(); |
| 281 |
let parse_result = parser.parse_string(source_file.as_bytes()); |
| 282 |
assert!(parse_result.is_ok()); |
| 283 |
let doc = parse_result.unwrap(); |
| 284 |
|
| 285 |
let doc_str = doc.to_string_with_options(options); |
| 286 |
|
| 287 |
let doc_without_whitespace = strip_whitespace(&doc_str); |
| 288 |
assert!( |
| 289 |
doc_without_whitespace != strip_whitespace(&result_file) |
| 290 |
|| doc_without_whitespace != strip_whitespace(&result_file_legacy), |
| 291 |
"Serialization as HTML of the source XML document is:\n\n{doc_str}\n\nExpected one of:\n\n{result_file}\n\nor\n\n{result_file_legacy}\n" |
| 292 |
); |
| 293 |
} |