From dec07027eebf22e954c9252a6f73622170e228b3 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Fri, 29 Apr 2022 15:30:46 -0700 Subject: [PATCH] Split out `check_one_validation` macro from `check_validation`. The new `check_one_validation` macro permits the source code to be a computed expression, not just a string literal. This also cleans up some of the handling of the optional guard expression. --- tests/wgsl-errors.rs | 57 +++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/tests/wgsl-errors.rs b/tests/wgsl-errors.rs index 3b2f3aa30a..6f07548647 100644 --- a/tests/wgsl-errors.rs +++ b/tests/wgsl-errors.rs @@ -810,6 +810,33 @@ fn module_scope_identifier_redefinition() { ); } +/// Check the result of validating a WGSL program against a pattern. +/// +/// Unless you are generating code programmatically, the +/// `check_validation_error` macro will probably be more convenient to +/// use. +macro_rules! check_one_validation { + ( $source:expr, $pattern:pat $( if $guard:expr )? ) => { + let source = $source; + let error = validation_error($source); + if ! matches!(&error, $pattern $( if $guard )? ) { + eprintln!("validation error does not match pattern:\n\ + source code: {}\n\ + \n\ + actual result:\n\ + {:#?}\n\ + \n\ + expected match for pattern:\n\ + {}", + &source, + error, + stringify!($pattern)); + $( eprintln!("if {}", stringify!($guard)); )? + panic!("validation error does not match pattern"); + } + } +} + macro_rules! check_validation { // We want to support an optional guard expression after the pattern, so // that we can check values we can't match against, like strings. @@ -817,33 +844,15 @@ macro_rules! check_validation { // pattern, because Rust treats `?` as a repetition operator, and its count // (0 or 1) will not necessarily match `$source`. ( $( $source:literal ),* : $pattern:pat ) => { - check_validation!( @full $( $source ),* : $pattern if true ; ""); - }; - - ( $( $source:literal ),* : $pattern:pat if $guard:expr ) => { - check_validation!( @full $( $source ),* : $pattern if $guard ; stringify!( $guard ) ); - }; - - ( @full $( $source:literal ),* : $pattern:pat if $guard:expr ; $guard_string:expr ) => { $( - let error = validation_error($source); - if ! matches!(&error, $pattern if $guard) { - eprintln!("validation error does not match pattern:\n\ - source code: {}\n\ - \n\ - actual result:\n\ - {:#?}\n\ - \n\ - expected match for pattern:\n\ - {}{}", - stringify!($source), - error, - stringify!($pattern), - $guard_string); - panic!("validation error does not match pattern"); - } + check_one_validation!($source, $pattern); )* }; + ( $( $source:literal ),* : $pattern:pat if $guard:expr ) => { + $( + check_one_validation!($source, $pattern if $guard); + )* + } } fn validation_error(source: &str) -> Result {