diff options
| author | John Turner <jturner.usa@gmail.com> | 2025-11-13 18:04:09 +0000 |
|---|---|---|
| committer | John Turner <jturner.usa@gmail.com> | 2025-11-13 18:04:09 +0000 |
| commit | 874be77aa404167270e94afcc7722078bb499b51 (patch) | |
| tree | 621f3463a7c4e14cc832197880b5787fc3c16cdb /tests | |
| parent | 1882ce3137e246aaa145568c6ba405f1be3f3a53 (diff) | |
| download | gentoo-utils-874be77aa404167270e94afcc7722078bb499b51.tar.gz | |
impl porthole tests
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/porthole.rs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/tests/porthole.rs b/tests/porthole.rs new file mode 100644 index 0000000..3efbffe --- /dev/null +++ b/tests/porthole.rs @@ -0,0 +1,95 @@ +use std::cmp::Ordering; + +use gentoo_utils::{ + Parseable, + atom::{Atom, Cpv}, +}; +use mon::{Parser, input::InputIter, tag}; + +static PORTHOLE_TXT: &'static str = include_str!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/testdata/porthole.txt" +)); + +enum Operator { + Comment, + Yes, + No, + Eq, + Gt, + Lt, +} + +fn parse_operator<'a>() -> impl Parser<&'a str, Output = Operator> { + let comment = tag("***").map(|_| Operator::Comment); + let yes = tag("+").map(|_| Operator::Yes); + let no = tag("-").map(|_| Operator::No); + let eq = tag("=").map(|_| Operator::Eq); + let gt = tag(">").map(|_| Operator::Gt); + let lt = tag("<").map(|_| Operator::Lt); + + comment.or(yes).or(no).or(eq).or(gt).or(lt) +} + +#[test] +fn test_porthole() { + for line in PORTHOLE_TXT.lines() { + if line.is_empty() { + continue; + } + + let operator = parse_operator() + .parse_finished(InputIter::new( + line.split_ascii_whitespace().nth(0).unwrap(), + )) + .unwrap(); + + match &operator { + Operator::Comment => continue, + Operator::Yes => { + let a = Atom::parser() + .parse_finished(InputIter::new( + line.split_ascii_whitespace().nth(1).unwrap(), + )) + .unwrap(); + + assert_eq!( + line.split_ascii_whitespace().nth(1).unwrap(), + &a.to_string() + ); + } + Operator::No => { + assert!( + Atom::parser() + .parse_finished(InputIter::new( + line.split_ascii_whitespace().nth(1).unwrap() + )) + .is_err() + ); + } + Operator::Eq | Operator::Gt | Operator::Lt => { + let a = Cpv::parser() + .parse_finished(InputIter::new( + line.split_ascii_whitespace().nth(1).unwrap(), + )) + .unwrap(); + + let b = Cpv::parser() + .parse_finished(InputIter::new( + line.split_ascii_whitespace().nth(2).unwrap(), + )) + .unwrap(); + + assert_eq!( + a.partial_cmp(&b).unwrap(), + match &operator { + Operator::Eq => Ordering::Equal, + Operator::Gt => Ordering::Greater, + Operator::Lt => Ordering::Less, + _ => unreachable!(), + } + ); + } + } + } +} |
