From 269269cff09d30a9e338a07df3d36ee42406f7d0 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Fri, 24 Feb 2023 11:40:32 +0000 Subject: [PATCH] chore: add test for Keyword::lookup_keyword completeness (#906) --- Cargo.lock | 21 +++++++++++++++++++++ crates/noirc_frontend/Cargo.toml | 4 ++++ crates/noirc_frontend/src/lexer/token.rs | 24 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index aade42f6e6..42476cea2a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2083,6 +2083,8 @@ dependencies = [ "noirc_errors", "rustc-hash", "smol_str", + "strum", + "strum_macros", "thiserror", ] @@ -2986,6 +2988,25 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "strum" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" + +[[package]] +name = "strum_macros" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "subtle" version = "2.4.1" diff --git a/crates/noirc_frontend/Cargo.toml b/crates/noirc_frontend/Cargo.toml index 254cd6a311..b05601c8ab 100644 --- a/crates/noirc_frontend/Cargo.toml +++ b/crates/noirc_frontend/Cargo.toml @@ -17,3 +17,7 @@ chumsky.workspace = true thiserror.workspace = true smol_str.workspace = true rustc-hash = "1.1.0" + +[dev-dependencies] +strum = "0.24" +strum_macros = "0.24" \ No newline at end of file diff --git a/crates/noirc_frontend/src/lexer/token.rs b/crates/noirc_frontend/src/lexer/token.rs index 75df6e526e..7e6d9b88b3 100644 --- a/crates/noirc_frontend/src/lexer/token.rs +++ b/crates/noirc_frontend/src/lexer/token.rs @@ -402,6 +402,7 @@ impl AsRef for Attribute { } #[derive(PartialEq, Eq, Hash, Debug, Copy, Clone, PartialOrd, Ord)] +#[cfg_attr(test, derive(strum_macros::EnumIter))] // Special Keywords allowed in the target language pub enum Keyword { As, @@ -502,6 +503,29 @@ impl Keyword { } } +#[cfg(test)] +mod keywords { + use strum::IntoEnumIterator; + + use super::{Keyword, Token}; + + #[test] + fn lookup_consistency() { + for keyword in Keyword::iter() { + let resolved_token = + Keyword::lookup_keyword(&format!("{keyword}")).unwrap_or_else(|| { + panic!("Keyword::lookup_keyword couldn't find Keyword {}", keyword) + }); + + assert_eq!( + resolved_token, + Token::Keyword(keyword), + "Keyword::lookup_keyword returns unexpected Keyword" + ) + } + } +} + pub struct Tokens(pub Vec); impl<'a> From