From 84e007577c6065ee023f05d041224aaf20076fa3 Mon Sep 17 00:00:00 2001 From: "Guillem L. Jara" <4lon3ly0@tutanota.com> Date: Thu, 14 May 2026 14:45:21 +0200 Subject: [PATCH] chore(parser): add pattern tests --- parser/src/tests.rs | 60 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/parser/src/tests.rs b/parser/src/tests.rs index cf8376c..0932f23 100644 --- a/parser/src/tests.rs +++ b/parser/src/tests.rs @@ -21,9 +21,9 @@ macro_rules! test_parser { $(end: $end:expr,)? $(begin_file: $begin_file:expr,)? $(end_file: $end_file:expr,)? - $(rules: $rules:expr,)? + $(rules: $rules:expr,)? $(concurrent: $concurrent:expr,)? - $(functions: $functions:expr,)? + $(functions: $functions:expr,)? } ) => { let arena = Bump::new(); @@ -42,14 +42,14 @@ macro_rules! test_parser { let mut concurrent: &[(Option<&str>, Option<&str>)] = &[]; let mut functions: &[(&str, &[&str], &str)] = &[]; - $(loads = &$loads;)? - $(end = &$end;)? - $(begin = &$begin;)? + $(loads = &$loads;)? + $(end = &$end;)? + $(begin = &$begin;)? $(begin_file = &$begin_file;)? - $(end_file = &$end_file;)? - $(rules = &$rules;)? + $(end_file = &$end_file;)? + $(rules = &$rules;)? $(concurrent = &$concurrent;)? - $(functions = &$functions;)? + $(functions = &$functions;)? test_parser!( @internal check |(a, b)| assert_eq!(a.as_bytes(), b.as_ref()); @@ -87,6 +87,10 @@ macro_rules! test_parser { ); }; }; + (is_err!($($code:expr),*)) => { + let arena = Bump::new(); + assert!([$($code),*].into_iter().all(|e| parse(e, &arena).is_err())); + }; (@internal check $lambda:expr; $a:expr => $b:expr) => { assert_eq!($a.len(), $b.len()); $a.into_iter().zip(&$b).for_each($lambda); @@ -135,3 +139,43 @@ fn test_parser_meta_holy_macro() { ], }); } + +#[test] +fn test_parser_valid_patterns() { + let source = " + BEGIN { print } + END { print } + BEGINFILE { print } + ENDFILE { print } + $0 == 1 && /x/ { print } + /abc/ { print } + !$0, x::a ? b : c { print } + awk; + 1 + 1 \n { print } + { print } + "; + const BODY: &str = "(body (Print))"; + test_parser!(source => { + begin: [BODY], + end: [BODY], + begin_file: [BODY], + end_file: [BODY], + rules: [ + (Some("(And (Eq (Record 0) 1) /x/)"), Some(BODY)), + (Some("/abc/"), Some(BODY)), + ( + Some("(Range (Negation (Record 0)) (?: x::a awk::b awk::c))"), + Some(BODY) + ), + (Some("awk::awk"), None), + (Some("(Add 1 1)"), None), + (None, Some(BODY)), + (None, Some(BODY)), + ], + }); +} + +#[test] +fn test_parser_invalid_patterns() { + test_parser!(is_err!("BEGIN", "END", "BEGINFILE", "ENDFILE", "print 1;")); +}