Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ilex: non-prefixed Digital rules panic on (some) invalid inputs #8

Open
adrianmgg opened this issue Feb 28, 2024 · 0 comments
Open

Comments

@adrianmgg
Copy link

ilex version: 0.5.0

Digital rules that don't have prefixes panic on some invalid inputs, from the unwrap here

let prefix = chunk.prefix.unwrap();

example:

use ilex::{rule::Digital, Lexeme};

#[ilex::spec]
struct NoPrefix {
    #[named]
    #[rule(Digital::new(10))]
    number: Lexeme<Digital>,
}

#[ilex::spec]
struct HasPrefix {
    #[named]
    #[rule(Digital::new(10).prefix("$"))]
    number: Lexeme<Digital>,
}

macro_rules! test {
    ($testname:ident, $spec:expr, $input:literal, $check:expr) => {
        #[test]
        fn $testname() {
            let ctx = ilex::Context::new();
            let report = ctx.new_report();
            let result = ctx.new_file("filename", $input).lex($spec, &report);
            assert!($check(&result));
        }
    };
}

test!(noprefix_validinput, NoPrefix::get().spec(), "123", Result::is_ok);
test!(hasprefix_validinput, HasPrefix::get().spec(), "$123", Result::is_ok);
test!(noprefix_badinput_abc, NoPrefix::get().spec(), "abc", Result::is_err);
test!(hasprefix_badinput_abc, HasPrefix::get().spec(), "abc", Result::is_err);
test!(hasprefix_badinput_prefixabc, HasPrefix::get().spec(), "$abc", Result::is_err);
test!(noprefix_badinput_other, NoPrefix::get().spec(), "#", Result::is_err);
test!(hasprefix_badinput_other, HasPrefix::get().spec(), "#", Result::is_err);
running 7 tests
test noprefix_validinput ... ok
test hasprefix_validinput ... ok
test noprefix_badinput_abc ... ok
test hasprefix_badinput_abc ... ok
test hasprefix_badinput_prefixabc ... ok
test noprefix_badinput_other ... FAILED
test hasprefix_badinput_other ... ok

failures:

---- noprefix_badinput_other stdout ----
thread 'noprefix_badinput_other' panicked at C:\Users\amgg\.cargo\registry\src\index.crates.io-6f17d22bba15001f\ilex-0.5.0\src\rt\emit2.rs:540:41:
called `Option::unwrap()` on a `None` value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant