From 057d03ad86f18e3bb3866b20901d8d4e892dd3d6 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Thu, 13 May 2021 10:29:03 -0700 Subject: [PATCH] [wgsl-in]: Add tests for array and struct validation. --- tests/wgsl-errors.rs | 101 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/tests/wgsl-errors.rs b/tests/wgsl-errors.rs index face95d911..5a03f47857 100644 --- a/tests/wgsl-errors.rs +++ b/tests/wgsl-errors.rs @@ -74,3 +74,104 @@ fn invalid_scalar_width() { "###, ); } + +macro_rules! check_validation_error { + ( $( $source:literal ),* : $pattern:pat ) => { + $( + let error = validation_error($source); + if ! matches!(error, $pattern) { + eprintln!("validation error does not match pattern:\n\ + {:?}\n\ + \n\ + expected match for pattern:\n\ + {}", + error, + stringify!($pattern)); + panic!("validation error does not match pattern"); + } + )* + } +} + +fn validation_error(source: &str) -> Result { + let module = naga::front::wgsl::parse_str(source).expect("expected WGSL parse to succeed"); + naga::valid::Validator::new( + naga::valid::ValidationFlags::all(), + naga::valid::Capabilities::empty(), + ) + .validate(&module) +} + +#[test] +fn invalid_arrays() { + check_validation_error! { + "type Bad = array, 4>;", + "type Bad = array;", + "type Bad = array, 4>;": + Err(naga::valid::ValidationError::Type { + error: naga::valid::TypeError::InvalidArrayBaseType(_), + .. + }) + } + + check_validation_error! { + r#" + [[block]] struct Block { value: f32; }; + type Bad = array; + "#: + Err(naga::valid::ValidationError::Type { + error: naga::valid::TypeError::NestedBlock, + .. + }) + } + + check_validation_error! { + r#" + type Bad = [[stride(2)]] array; + "#: + Err(naga::valid::ValidationError::Type { + error: naga::valid::TypeError::InsufficientArrayStride { stride: 2, base_size: 4 }, + .. + }) + } + + check_validation_error! { + "type Bad = array;", + r#" + let length: f32 = 2.718; + type Bad = array; + "#: + Err(naga::valid::ValidationError::Type { + error: naga::valid::TypeError::InvalidArraySizeConstant(_), + .. + }) + } +} + +#[test] +fn invalid_structs() { + check_validation_error! { + "struct Bad { data: sampler; };", + "struct Bad { data: texture_2d; };": + Err(naga::valid::ValidationError::Type { + error: naga::valid::TypeError::InvalidData(_), + .. + }) + } + + check_validation_error! { + "[[block]] struct Bad { data: ptr; };": + Err(naga::valid::ValidationError::Type { + error: naga::valid::TypeError::InvalidBlockType(_), + .. + }) + } + + check_validation_error! { + "struct Bad { data: array; other: f32; };": + Err(naga::valid::ValidationError::Type { + error: naga::valid::TypeError::InvalidDynamicArray(_, _), + .. + }) + } +}