refactor: create macro assert_pretty_eval

this makes the tests so much shorter in the code, it's nice
This commit is contained in:
electria 2026-10-19 12:24:52 -07:00
commit 780f0c6f4c
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -81,49 +81,57 @@ pub fn get_significant_digits(n: &Float) -> usize {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
const MAX_DIGITS: usize = 10;
fn eval(s: &str) -> Float { fn eval(s: &str) -> Float {
crate::nodes::eval(s).unwrap() crate::nodes::eval(s).unwrap()
} }
macro_rules! assert_pretty_eval {
($both:expr) => {
assert_pretty_eval!($both, $both, "should be the same as the input")
};
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
std::assert_matches!(pretty_eval($left, 10).as_deref(), Ok($( $pattern )|+) $( if $guard )?)
// assert_pretty_eval!($left, $( $pattern )|+ $( if $guard )?, "")
};
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => {
std::assert_matches!(pretty_eval($left, 10).as_deref(), Ok($( $pattern )|+) $( if $guard )?, $($arg)+)
};
}
#[test] #[test]
fn character_readdition() { fn character_readdition() {
assert_eq!(pretty_eval(" = 11", MAX_DIGITS).unwrap(), " = 11"); assert_pretty_eval!(" = 11");
assert_eq!(pretty_eval(" &= 11", MAX_DIGITS).unwrap(), " &= 11"); assert_pretty_eval!(" &= 11");
assert_eq!(pretty_eval(" & 11", MAX_DIGITS).unwrap(), " & 11"); assert_pretty_eval!(" & 11");
assert_eq!(pretty_eval(" & 11 \\", MAX_DIGITS).unwrap(), " & 11 \\"); assert_pretty_eval!(" & 11 \\");
} }
#[test] #[test]
fn basic_output_up_to_999() { fn basic_output_up_to_999() {
assert_eq!(pretty_eval("1", MAX_DIGITS).unwrap(), "1"); assert_pretty_eval!("1");
assert_eq!(pretty_eval("10", MAX_DIGITS).unwrap(), "10"); assert_pretty_eval!("10");
assert_eq!(pretty_eval("100", MAX_DIGITS).unwrap(), "100"); assert_pretty_eval!("100");
assert_eq!(pretty_eval("999", MAX_DIGITS).unwrap(), "999"); assert_pretty_eval!("999");
assert_eq!(pretty_eval("1000", MAX_DIGITS).unwrap(), "1 * 10^3"); assert_pretty_eval!("1000", "1 * 10^3");
assert_eq!(pretty_eval("11.111111", MAX_DIGITS).unwrap(), "11.111111"); assert_pretty_eval!("11.111111");
} }
#[test] #[test]
fn plus_minus() { fn plus_minus() {
assert_eq!( assert_pretty_eval!(
pretty_eval("y & = plus.minus sqrt(15.5) \\", MAX_DIGITS).unwrap(), "y & = plus.minus sqrt(15.5) \\",
"y & = plus.minus 3.937003937 \\" "y & = plus.minus 3.937003937 \\"
); );
} }
#[test] #[test]
fn minus() { fn minus() {
assert_eq!(pretty_eval("-11", MAX_DIGITS).unwrap(), "-11"); assert_pretty_eval!("-11");
} }
#[test] #[test]
fn complex_prefix() { fn complex_prefix() {
assert_eq!( assert_pretty_eval!("plus.minus 5 & = 5");
pretty_eval("plus.minus 5 & = 5", MAX_DIGITS).unwrap(),
"plus.minus 5 & = 5"
);
} }
#[test] #[test]