From 838a5e328ac0f8178e9932a9b09a280074b2b938 Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Thu, 8 Aug 2024 01:22:24 +0100 Subject: [PATCH] test: Add tests for parsing errors --- src/filter/parser.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/filter/parser.rs b/src/filter/parser.rs index 08fb8a8..ed0d913 100644 --- a/src/filter/parser.rs +++ b/src/filter/parser.rs @@ -194,6 +194,7 @@ mod tests { #[case("null", FilterValue::Null)] #[case("[]", FilterValue::Tuple(vec![]))] #[case("[true]", FilterValue::Tuple(vec![true.into()]))] + #[case("[\ntrue,\n]", FilterValue::Tuple(vec![true.into()]))] #[case("[true, false, \"test\", 123, null]", FilterValue::Tuple(vec![true.into(), false.into(), "test".into(), 123.into(), FilterValue::Null]))] fn parsing_literals(#[case] input: &str, #[case] value: FilterValue) { let tokens = crate::filter::lexer::Scanner::new(input); @@ -240,4 +241,38 @@ mod tests { Err(e) => panic!("Error: {}", e), } } + + #[rstest] + #[case( + "true false", + "Your filter expression contained an unexpected 'false' at line 1, column 6." + )] + #[case( + "true ==", + "We reached the end of your filter expression while waiting for a [true, false, \"string\", number, (group), or property.name]." + )] + #[case( + "(true", + "When attempting to parse a grouped filter expression starting at line 1, column 1, we didn't find the closing ')' where we expected to." + )] + #[case( + "[true, false", + "When attempting to parse a list filter expression starting at line 1, column 1, we didn't find the closing ']' where we expected to." + )] + #[case( + ")", + "While parsing your filter, we found an unexpected ')' at line 1, column 1." + )] + fn invalid_filters(#[case] input: &str, #[case] message: &str) { + let tokens = crate::filter::lexer::Scanner::new(input); + match Parser::parse(tokens.into_iter()) { + Ok(expr) => panic!("Expected an error, got {:?}", expr), + Err(e) => assert!( + e.to_string().contains(message), + "Expected error message to contain '{}', got '{}'", + message, + e + ), + } + } }