@@ -6,7 +6,6 @@ use libxml::schemas::SchemaValidationContext;
66
77use libxml:: parser:: Parser ;
88
9-
109static SCHEMA : & ' static str = r#"<?xml version="1.0"?>
1110<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
1211 <xs:element name="note">
@@ -22,7 +21,6 @@ static SCHEMA: &'static str = r#"<?xml version="1.0"?>
2221</xs:schema>
2322"# ;
2423
25-
2624static XML : & ' static str = r#"<?xml version="1.0"?>
2725<note>
2826 <to>Tove</to>
@@ -32,38 +30,72 @@ static XML: &'static str = r#"<?xml version="1.0"?>
3230</note>
3331"# ;
3432
33+ static INVALID_XML : & ' static str = r#"<?xml version="1.0"?>
34+ <note>
35+ <bad>Tove</bad>
36+ <another>Jani</another>
37+ <heading>Reminder</heading>
38+ <body>Don't forget me this weekend!</body>
39+ </note>
40+ "# ;
3541
3642#[ test]
37- fn schema_from_string ( )
38- {
39- let xml = Parser :: default ( )
40- . parse_string ( XML )
41- . expect ( "Expected to be able to parse XML Document from string" ) ;
42-
43- let mut xsdparser = SchemaParserContext :: from_buffer ( SCHEMA ) ;
44- let xsd = SchemaValidationContext :: from_parser ( & mut xsdparser) ;
45-
46- if let Err ( errors) = xsd
47- {
48- for err in & errors {
49- println ! ( "{}" , err. message( ) ) ;
50- }
51-
52- panic ! ( "Failed to parse schema" ) ;
43+ fn schema_from_string ( ) {
44+ let xml = Parser :: default ( )
45+ . parse_string ( XML )
46+ . expect ( "Expected to be able to parse XML Document from string" ) ;
47+
48+ let mut xsdparser = SchemaParserContext :: from_buffer ( SCHEMA ) ;
49+ let xsd = SchemaValidationContext :: from_parser ( & mut xsdparser) ;
50+
51+ if let Err ( errors) = xsd {
52+ for err in & errors {
53+ println ! ( "{}" , err. message( ) ) ;
5354 }
5455
55- let mut xsdvalidator = xsd. unwrap ( ) ;
56+ panic ! ( "Failed to parse schema" ) ;
57+ }
58+
59+ let mut xsdvalidator = xsd. unwrap ( ) ;
60+
61+ // loop over more than one validation to test for leaks in the error handling callback interactions
62+ for _ in 0 ..5 {
63+ if let Err ( errors) = xsdvalidator. validate_document ( & xml) {
64+ for err in & errors {
65+ println ! ( "{}" , err. message( ) ) ;
66+ }
67+
68+ panic ! ( "Invalid XML accoding to XSD schema" ) ;
69+ }
70+ }
71+ }
72+
73+ #[ test]
74+ fn schema_from_string_generates_errors ( ) {
75+ let xml = Parser :: default ( )
76+ . parse_string ( INVALID_XML )
77+ . expect ( "Expected to be able to parse XML Document from string" ) ;
78+
79+ let mut xsdparser = SchemaParserContext :: from_buffer ( SCHEMA ) ;
80+ let xsd = SchemaValidationContext :: from_parser ( & mut xsdparser) ;
81+
82+ if let Err ( errors) = xsd {
83+ for err in & errors {
84+ println ! ( "{}" , err. message( ) ) ;
85+ }
5686
57- // loop over more than one validation to test for leaks in the error handling callback interactions
58- for _ in 0 ..5
59- {
60- if let Err ( errors) = xsdvalidator. validate_document ( & xml)
61- {
62- for err in & errors {
63- println ! ( "{}" , err. message( ) ) ;
64- }
87+ panic ! ( "Failed to parse schema" ) ;
88+ }
6589
66- panic ! ( "Invalid XML accoding to XSD schema" ) ;
67- }
90+ let mut xsdvalidator = xsd. unwrap ( ) ;
91+ for _ in 0 ..5 {
92+ if let Err ( errors) = xsdvalidator. validate_document ( & xml) {
93+ for err in & errors {
94+ assert_eq ! (
95+ "Element 'bad': This element is not expected. Expected is ( to ).\n " ,
96+ err. message( )
97+ ) ;
98+ }
6899 }
100+ }
69101}
0 commit comments