From 68cc3f7342ce1f4223d4d6ecf43feaf34adc82cd Mon Sep 17 00:00:00 2001 From: Bazaah Date: Wed, 2 Jun 2021 18:10:09 +0000 Subject: [PATCH] lib/scanner: improve tokens! macro error reporting --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/scanner/tests/macros.rs | 24 +++++++++++++++++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 84d85e4..9a017a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,6 +9,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "anyhow" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b" + [[package]] name = "ctor" version = "0.1.20" @@ -107,5 +113,6 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" name = "yary" version = "0.1.0" dependencies = [ + "anyhow", "pretty_assertions", ] diff --git a/Cargo.toml b/Cargo.toml index 8650ba8..5ee344f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ edition = "2018" [dependencies] [dev-dependencies] +anyhow = "1" pretty_assertions = "0.7" diff --git a/src/scanner/tests/macros.rs b/src/scanner/tests/macros.rs index d259e1c..5815189 100644 --- a/src/scanner/tests/macros.rs +++ b/src/scanner/tests/macros.rs @@ -6,7 +6,15 @@ /// A message to print on failure macro_rules! tokens { ($scanner:expr => $($id:tt $expected:expr $(=> $msg:tt)?),+ ) => { - $( tokens!(@unwrap $id $scanner => $expected $(=> $msg)? ) );+ + let mut f = || -> std::result::Result<(), ::anyhow::Error> { + $( tokens!(@unwrap $id $scanner => $expected $(=> $msg)? ); )+ + + Ok(()) + }; + + if let Err(e) = f() { + panic!("tokens! error: {}", e) + } }; // <-- PRIVATE VARIANTS --> @@ -25,11 +33,21 @@ macro_rules! tokens { }; // Variant for token assert, no message (@token $scanner:expr => $expected:expr) => { - assert_eq!($scanner.next().expect("Unexpected end of events"), $expected) + let event = $scanner + .next() + .ok_or_else( + || anyhow::anyhow!("Unexpected end of tokens, was expecting: {:?} ~{}", $expected, $scanner.buffer) + )?; + + assert_eq!(event, $expected) }; // Variant for token assert, no with message (@token $scanner:expr => $expected:expr, $msg:tt) => { - let event = $scanner.next().expect("Unexpected end of events"); + let event = $scanner + .next() + .ok_or_else( + || anyhow::anyhow!("Unexpected end of tokens, {}: {:?} ~{}", $msg, $expected, $scanner.buffer) + )?; assert_eq!(event, $expected, $msg) };