From ecc7ef37629b77db48260738ddb73def97ad8344 Mon Sep 17 00:00:00 2001 From: Mingun Date: Thu, 4 Jul 2024 20:33:21 +0500 Subject: [PATCH] Actually check that .error_position() is correct Check position for zero actually does not check anything because this is start position before parse. We move position by 1 by using Text event failures (36): syntax::tag::unclosed3::ns_reader::async_tokio syntax::tag::unclosed3::ns_reader::borrowed syntax::tag::unclosed3::ns_reader::buffered syntax::tag::unclosed3::reader::async_tokio syntax::tag::unclosed3::reader::borrowed syntax::tag::unclosed3::reader::buffered syntax::tag::unclosed4::ns_reader::async_tokio syntax::tag::unclosed4::ns_reader::borrowed syntax::tag::unclosed4::ns_reader::buffered syntax::tag::unclosed4::reader::async_tokio syntax::tag::unclosed4::reader::borrowed syntax::tag::unclosed4::reader::buffered syntax::tag::unclosed5::ns_reader::async_tokio syntax::tag::unclosed5::ns_reader::borrowed syntax::tag::unclosed5::ns_reader::buffered syntax::tag::unclosed5::reader::async_tokio syntax::tag::unclosed5::reader::borrowed syntax::tag::unclosed5::reader::buffered syntax::tag::unclosed6::ns_reader::async_tokio syntax::tag::unclosed6::ns_reader::borrowed syntax::tag::unclosed6::ns_reader::buffered syntax::tag::unclosed6::reader::async_tokio syntax::tag::unclosed6::reader::borrowed syntax::tag::unclosed6::reader::buffered syntax::tag::unclosed7::ns_reader::async_tokio syntax::tag::unclosed7::ns_reader::borrowed syntax::tag::unclosed7::ns_reader::buffered syntax::tag::unclosed7::reader::async_tokio syntax::tag::unclosed7::reader::borrowed syntax::tag::unclosed7::reader::buffered syntax::tag::unclosed8::ns_reader::async_tokio syntax::tag::unclosed8::ns_reader::borrowed syntax::tag::unclosed8::ns_reader::buffered syntax::tag::unclosed8::reader::async_tokio syntax::tag::unclosed8::reader::borrowed syntax::tag::unclosed8::reader::buffered --- tests/reader-errors.rs | 229 ++++++++++++++++++++++++----------------- 1 file changed, 135 insertions(+), 94 deletions(-) diff --git a/tests/reader-errors.rs b/tests/reader-errors.rs index 34880c15..8f9c578e 100644 --- a/tests/reader-errors.rs +++ b/tests/reader-errors.rs @@ -101,10 +101,16 @@ mod syntax { #[test] fn borrowed() { let mut reader = Reader::from_str($xml); + assert_eq!( + reader + .read_event() + .expect("parser should return `Event::Text`"), + Event::Text(BytesText::new(".")) + ); match reader.read_event() { Err(Error::Syntax(cause)) => assert_eq!( (cause, reader.error_position(), reader.buffer_position()), - ($cause, 0, $pos), + ($cause, 1, $pos), ), x => panic!("Expected `Err(Syntax(_))`, but got {:?}", x), } @@ -120,10 +126,16 @@ mod syntax { fn buffered() { let mut buf = Vec::new(); let mut reader = Reader::from_str($xml); + assert_eq!( + reader + .read_event_into(&mut buf) + .expect("parser should return `Event::Text`"), + Event::Text(BytesText::new(".")) + ); match reader.read_event_into(&mut buf) { Err(Error::Syntax(cause)) => assert_eq!( (cause, reader.error_position(), reader.buffer_position()), - ($cause, 0, $pos), + ($cause, 1, $pos), ), x => panic!("Expected `Err(Syntax(_))`, but got {:?}", x), } @@ -140,10 +152,17 @@ mod syntax { async fn async_tokio() { let mut buf = Vec::new(); let mut reader = Reader::from_str($xml); + assert_eq!( + reader + .read_event_into_async(&mut buf) + .await + .expect("parser should return `Event::Text`"), + Event::Text(BytesText::new(".")) + ); match reader.read_event_into_async(&mut buf).await { Err(Error::Syntax(cause)) => assert_eq!( (cause, reader.error_position(), reader.buffer_position()), - ($cause, 0, $pos), + ($cause, 1, $pos), ), x => panic!("Expected `Err(Syntax(_))`, but got {:?}", x), } @@ -164,10 +183,17 @@ mod syntax { #[test] fn borrowed() { let mut reader = NsReader::from_str($xml); + assert_eq!( + reader + .read_resolved_event() + .expect("parser should return `Event::Text`") + .1, + Event::Text(BytesText::new(".")) + ); match reader.read_resolved_event() { Err(Error::Syntax(cause)) => assert_eq!( (cause, reader.error_position(), reader.buffer_position()), - ($cause, 0, $pos), + ($cause, 1, $pos), ), x => panic!("Expected `Err(Syntax(_))`, but got {:?}", x), } @@ -184,10 +210,17 @@ mod syntax { fn buffered() { let mut buf = Vec::new(); let mut reader = NsReader::from_str($xml); + assert_eq!( + reader + .read_resolved_event_into(&mut buf) + .expect("parser should return `Event::Text`") + .1, + Event::Text(BytesText::new(".")) + ); match reader.read_resolved_event_into(&mut buf) { Err(Error::Syntax(cause)) => assert_eq!( (cause, reader.error_position(), reader.buffer_position()), - ($cause, 0, $pos), + ($cause, 1, $pos), ), x => panic!("Expected `Err(Syntax(_))`, but got {:?}", x), } @@ -205,10 +238,18 @@ mod syntax { async fn async_tokio() { let mut buf = Vec::new(); let mut reader = NsReader::from_str($xml); + assert_eq!( + reader + .read_resolved_event_into_async(&mut buf) + .await + .expect("parser should return `Event::Text`") + .1, + Event::Text(BytesText::new(".")) + ); match reader.read_resolved_event_into_async(&mut buf).await { Err(Error::Syntax(cause)) => assert_eq!( (cause, reader.error_position(), reader.buffer_position()), - ($cause, 0, $pos), + ($cause, 1, $pos), ), x => panic!("Expected `Err(Syntax(_))`, but got {:?}", x), } @@ -232,14 +273,14 @@ mod syntax { mod tag { use super::*; - err!(unclosed1("<") => SyntaxError::UnclosedTag); - err!(unclosed2(" SyntaxError::UnclosedTag); - err!(unclosed3(" SyntaxError::UnclosedTag); - err!(unclosed4("< ") => SyntaxError::UnclosedTag); - err!(unclosed5("<\t") => SyntaxError::UnclosedTag); - err!(unclosed6("<\r") => SyntaxError::UnclosedTag); - err!(unclosed7("<\n") => SyntaxError::UnclosedTag); - err!(unclosed8("< \t\r\nx") => SyntaxError::UnclosedTag); + err!(unclosed1(".<") => SyntaxError::UnclosedTag); + err!(unclosed2(". SyntaxError::UnclosedTag); + err!(unclosed3(". SyntaxError::UnclosedTag); + err!(unclosed4(".< ") => SyntaxError::UnclosedTag); + err!(unclosed5(".<\t") => SyntaxError::UnclosedTag); + err!(unclosed6(".<\r") => SyntaxError::UnclosedTag); + err!(unclosed7(".<\n") => SyntaxError::UnclosedTag); + err!(unclosed8(".< \t\r\nx") => SyntaxError::UnclosedTag); /// Closed tags can be tested only in pair with open tags, because otherwise /// `IllFormedError::UnmatchedEndTag` will be raised @@ -289,25 +330,25 @@ mod syntax { } // Incorrect after-bang symbol is detected early, so buffer_position() stay at `!` - err!(unclosed_bang1(" 1, SyntaxError::InvalidBangMarkup); - err!(unclosed_bang2("") => 1, SyntaxError::InvalidBangMarkup); - err!(unclosed_bang3(" 1, SyntaxError::InvalidBangMarkup); - err!(unclosed_bang4("") => 1, SyntaxError::InvalidBangMarkup); + err!(unclosed_bang1(". 2, SyntaxError::InvalidBangMarkup); + err!(unclosed_bang2(".") => 2, SyntaxError::InvalidBangMarkup); + err!(unclosed_bang3(". 2, SyntaxError::InvalidBangMarkup); + err!(unclosed_bang4(".") => 2, SyntaxError::InvalidBangMarkup); /// https://www.w3.org/TR/xml11/#NT-Comment mod comment { use super::*; - err!(unclosed01(" SyntaxError::UnclosedComment); - err!(unclosed02("") => SyntaxError::UnclosedComment); - err!(unclosed07("") => SyntaxError::UnclosedComment); - err!(unclosed10("") => SyntaxError::UnclosedComment); + err!(unclosed07(".") => SyntaxError::UnclosedComment); + err!(unclosed10(".") => 7: Event::Comment(BytesText::new(""))); ok!(normal2("rest") => 7: Event::Comment(BytesText::new(""))); @@ -317,33 +358,33 @@ mod syntax { mod cdata { use super::*; - err!(unclosed01(" SyntaxError::UnclosedCData); - err!(unclosed02(" SyntaxError::UnclosedCData); - err!(unclosed03(" SyntaxError::UnclosedCData); - err!(unclosed04("") => SyntaxError::UnclosedCData); - err!(unclosed05(" SyntaxError::UnclosedCData); - err!(unclosed06(" SyntaxError::UnclosedCData); - err!(unclosed07("") => SyntaxError::UnclosedCData); - err!(unclosed08(" SyntaxError::UnclosedCData); - err!(unclosed09(" SyntaxError::UnclosedCData); - err!(unclosed10("") => SyntaxError::UnclosedCData); - err!(unclosed11(" SyntaxError::UnclosedCData); - err!(unclosed12(" SyntaxError::UnclosedCData); - err!(unclosed13("") => SyntaxError::UnclosedCData); - err!(unclosed14(" SyntaxError::UnclosedCData); - err!(unclosed15(" SyntaxError::UnclosedCData); - err!(unclosed16("") => SyntaxError::UnclosedCData); - err!(unclosed17(" SyntaxError::UnclosedCData); - err!(unclosed18(" SyntaxError::UnclosedCData); - err!(unclosed19("") => SyntaxError::UnclosedCData); - err!(unclosed20(" SyntaxError::UnclosedCData); - err!(unclosed21(" SyntaxError::UnclosedCData); - err!(unclosed22("") => SyntaxError::UnclosedCData); - err!(unclosed23(" SyntaxError::UnclosedCData); - err!(unclosed24(" SyntaxError::UnclosedCData); - err!(unclosed25("") => SyntaxError::UnclosedCData); - - err!(lowercase("") => SyntaxError::UnclosedCData); + err!(unclosed01(". SyntaxError::UnclosedCData); + err!(unclosed02(". SyntaxError::UnclosedCData); + err!(unclosed03(". SyntaxError::UnclosedCData); + err!(unclosed04(".") => SyntaxError::UnclosedCData); + err!(unclosed05(". SyntaxError::UnclosedCData); + err!(unclosed06(". SyntaxError::UnclosedCData); + err!(unclosed07(".") => SyntaxError::UnclosedCData); + err!(unclosed08(". SyntaxError::UnclosedCData); + err!(unclosed09(". SyntaxError::UnclosedCData); + err!(unclosed10(".") => SyntaxError::UnclosedCData); + err!(unclosed11(". SyntaxError::UnclosedCData); + err!(unclosed12(". SyntaxError::UnclosedCData); + err!(unclosed13(".") => SyntaxError::UnclosedCData); + err!(unclosed14(". SyntaxError::UnclosedCData); + err!(unclosed15(". SyntaxError::UnclosedCData); + err!(unclosed16(".") => SyntaxError::UnclosedCData); + err!(unclosed17(". SyntaxError::UnclosedCData); + err!(unclosed18(". SyntaxError::UnclosedCData); + err!(unclosed19(".") => SyntaxError::UnclosedCData); + err!(unclosed20(". SyntaxError::UnclosedCData); + err!(unclosed21(". SyntaxError::UnclosedCData); + err!(unclosed22(".") => SyntaxError::UnclosedCData); + err!(unclosed23(". SyntaxError::UnclosedCData); + err!(unclosed24(". SyntaxError::UnclosedCData); + err!(unclosed25(".") => SyntaxError::UnclosedCData); + + err!(lowercase(".") => SyntaxError::UnclosedCData); ok!(normal1("") => 12: Event::CData(BytesCData::new(""))); ok!(normal2("rest") => 12: Event::CData(BytesCData::new(""))); @@ -355,29 +396,29 @@ mod syntax { mod doctype { use super::*; - err!(unclosed01(" SyntaxError::UnclosedDoctype); - err!(unclosed02(" SyntaxError::UnclosedDoctype); - err!(unclosed03(" SyntaxError::UnclosedDoctype); - err!(unclosed04("") => SyntaxError::UnclosedDoctype); - err!(unclosed05(" SyntaxError::UnclosedDoctype); - err!(unclosed06(" SyntaxError::UnclosedDoctype); - err!(unclosed07("") => SyntaxError::UnclosedDoctype); - err!(unclosed08(" SyntaxError::UnclosedDoctype); - err!(unclosed09(" SyntaxError::UnclosedDoctype); - err!(unclosed10("") => SyntaxError::UnclosedDoctype); - err!(unclosed11(" SyntaxError::UnclosedDoctype); - err!(unclosed12(" SyntaxError::UnclosedDoctype); - err!(unclosed13("") => SyntaxError::UnclosedDoctype); - err!(unclosed14(" SyntaxError::UnclosedDoctype); - err!(unclosed15(" SyntaxError::UnclosedDoctype); - err!(unclosed16("") => SyntaxError::UnclosedDoctype); - err!(unclosed17(" SyntaxError::UnclosedDoctype); - err!(unclosed18(" SyntaxError::UnclosedDoctype); - err!(unclosed19("") => SyntaxError::UnclosedDoctype); - err!(unclosed20(" SyntaxError::UnclosedDoctype); - err!(unclosed21(" SyntaxError::UnclosedDoctype); + err!(unclosed01(". SyntaxError::UnclosedDoctype); + err!(unclosed02(". SyntaxError::UnclosedDoctype); + err!(unclosed03(". SyntaxError::UnclosedDoctype); + err!(unclosed04(".") => SyntaxError::UnclosedDoctype); + err!(unclosed05(". SyntaxError::UnclosedDoctype); + err!(unclosed06(". SyntaxError::UnclosedDoctype); + err!(unclosed07(".") => SyntaxError::UnclosedDoctype); + err!(unclosed08(". SyntaxError::UnclosedDoctype); + err!(unclosed09(". SyntaxError::UnclosedDoctype); + err!(unclosed10(".") => SyntaxError::UnclosedDoctype); + err!(unclosed11(". SyntaxError::UnclosedDoctype); + err!(unclosed12(". SyntaxError::UnclosedDoctype); + err!(unclosed13(".") => SyntaxError::UnclosedDoctype); + err!(unclosed14(". SyntaxError::UnclosedDoctype); + err!(unclosed15(". SyntaxError::UnclosedDoctype); + err!(unclosed16(".") => SyntaxError::UnclosedDoctype); + err!(unclosed17(". SyntaxError::UnclosedDoctype); + err!(unclosed18(". SyntaxError::UnclosedDoctype); + err!(unclosed19(".") => SyntaxError::UnclosedDoctype); + err!(unclosed20(". SyntaxError::UnclosedDoctype); + err!(unclosed21(". SyntaxError::UnclosedDoctype); // results in IllFormed(MissingDoctypeName), checked below - err!(unclosed22(" SyntaxError::UnclosedDoctype); + err!(unclosed22(". SyntaxError::UnclosedDoctype); // According to the grammar, XML declaration MUST contain at least one space // and an element name, but we do not consider this as a _syntax_ error. @@ -389,16 +430,16 @@ mod syntax { mod pi { use super::*; - err!(unclosed1(" SyntaxError::UnclosedPIOrXmlDecl); - err!(unclosed2(" SyntaxError::UnclosedPIOrXmlDecl); - err!(unclosed3("") => SyntaxError::UnclosedPIOrXmlDecl); - err!(unclosed4(" SyntaxError::UnclosedPIOrXmlDecl); - err!(unclosed5(" SyntaxError::UnclosedPIOrXmlDecl); - err!(unclosed6(" SyntaxError::UnclosedPIOrXmlDecl); - err!(unclosed7(" SyntaxError::UnclosedPIOrXmlDecl); - err!(unclosed8(" SyntaxError::UnclosedPIOrXmlDecl); - err!(unclosed9(" SyntaxError::UnclosedPIOrXmlDecl); - err!(unclosed10(" SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed01(". SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed02(". SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed03(".") => SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed04(". SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed05(". SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed06(". SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed07(". SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed08(". SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed09(". SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed10(". SyntaxError::UnclosedPIOrXmlDecl); // According to the grammar, processing instruction MUST contain a non-empty // target name, but we do not consider this as a _syntax_ error. @@ -412,10 +453,10 @@ mod syntax { mod decl { use super::*; - err!(unclosed1(" SyntaxError::UnclosedPIOrXmlDecl); - err!(unclosed2(" SyntaxError::UnclosedPIOrXmlDecl); - err!(unclosed3(" SyntaxError::UnclosedPIOrXmlDecl); - err!(unclosed4(" SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed1(". SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed2(". SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed3(". SyntaxError::UnclosedPIOrXmlDecl); + err!(unclosed4(". SyntaxError::UnclosedPIOrXmlDecl); // According to the grammar, XML declaration MUST contain at least one space // and `version` attribute, but we do not consider this as a _syntax_ error. @@ -827,9 +868,9 @@ mod ill_formed { // ^= 13 ok!(missing_doctype_name3("") => 15: Event::DocType(BytesText::new("x"))); - err!(unmatched_end_tag1("") => 0: IllFormedError::UnmatchedEndTag("".to_string())); - err!(unmatched_end_tag2("") => 0: IllFormedError::UnmatchedEndTag("end".to_string())); - err!(unmatched_end_tag3("") => 0: IllFormedError::UnmatchedEndTag("end".to_string())); + err2!(unmatched_end_tag1(".") => 1: IllFormedError::UnmatchedEndTag("".to_string())); + err2!(unmatched_end_tag2(".") => 1: IllFormedError::UnmatchedEndTag("end".to_string())); + err2!(unmatched_end_tag3(".") => 1: IllFormedError::UnmatchedEndTag("end".to_string())); ok!(mismatched_end_tag1("") => 7: Event::Start(BytesStart::new("start"))); err2!(mismatched_end_tag2("") => 7: IllFormedError::MismatchedEndTag {