Skip to content

Commit

Permalink
chore: add test for Keyword::lookup_keyword completeness (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Feb 24, 2023
1 parent 93d83bf commit 269269c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/noirc_frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
24 changes: 24 additions & 0 deletions crates/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ impl AsRef<str> 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,
Expand Down Expand Up @@ -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<SpannedToken>);

impl<'a> From<Tokens>
Expand Down

0 comments on commit 269269c

Please sign in to comment.