Skip to content

Commit

Permalink
chore: add test coverage for validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarber authored and dginev committed Jan 16, 2020
1 parent 27050bf commit 03d02c9
Showing 1 changed file with 61 additions and 29 deletions.
90 changes: 61 additions & 29 deletions tests/schema_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use libxml::schemas::SchemaValidationContext;

use libxml::parser::Parser;


static SCHEMA: &'static str = r#"<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
Expand All @@ -22,7 +21,6 @@ static SCHEMA: &'static str = r#"<?xml version="1.0"?>
</xs:schema>
"#;


static XML: &'static str = r#"<?xml version="1.0"?>
<note>
<to>Tove</to>
Expand All @@ -32,38 +30,72 @@ static XML: &'static str = r#"<?xml version="1.0"?>
</note>
"#;

static INVALID_XML: &'static str = r#"<?xml version="1.0"?>
<note>
<bad>Tove</bad>
<another>Jani</another>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
"#;

#[test]
fn schema_from_string()
{
let xml = Parser::default()
.parse_string(XML)
.expect("Expected to be able to parse XML Document from string");

let mut xsdparser = SchemaParserContext::from_buffer(SCHEMA);
let xsd = SchemaValidationContext::from_parser(&mut xsdparser);

if let Err(errors) = xsd
{
for err in &errors {
println!("{}", err.message());
}

panic!("Failed to parse schema");
fn schema_from_string() {
let xml = Parser::default()
.parse_string(XML)
.expect("Expected to be able to parse XML Document from string");

let mut xsdparser = SchemaParserContext::from_buffer(SCHEMA);
let xsd = SchemaValidationContext::from_parser(&mut xsdparser);

if let Err(errors) = xsd {
for err in &errors {
println!("{}", err.message());
}

let mut xsdvalidator = xsd.unwrap();
panic!("Failed to parse schema");
}

let mut xsdvalidator = xsd.unwrap();

// loop over more than one validation to test for leaks in the error handling callback interactions
for _ in 0..5 {
if let Err(errors) = xsdvalidator.validate_document(&xml) {
for err in &errors {
println!("{}", err.message());
}

panic!("Invalid XML accoding to XSD schema");
}
}
}

#[test]
fn schema_from_string_generates_errors() {
let xml = Parser::default()
.parse_string(INVALID_XML)
.expect("Expected to be able to parse XML Document from string");

let mut xsdparser = SchemaParserContext::from_buffer(SCHEMA);
let xsd = SchemaValidationContext::from_parser(&mut xsdparser);

if let Err(errors) = xsd {
for err in &errors {
println!("{}", err.message());
}

// loop over more than one validation to test for leaks in the error handling callback interactions
for _ in 0..5
{
if let Err(errors) = xsdvalidator.validate_document(&xml)
{
for err in &errors {
println!("{}", err.message());
}
panic!("Failed to parse schema");
}

panic!("Invalid XML accoding to XSD schema");
}
let mut xsdvalidator = xsd.unwrap();
for _ in 0..5 {
if let Err(errors) = xsdvalidator.validate_document(&xml) {
for err in &errors {
assert_eq!(
"Element 'bad': This element is not expected. Expected is ( to ).\n",
err.message()
);
}
}
}
}

0 comments on commit 03d02c9

Please sign in to comment.