diff --git a/src/formatting.rs b/src/formatting.rs index c1853da..bb67376 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -81,49 +81,57 @@ pub fn get_significant_digits(n: &Float) -> usize { #[cfg(test)] mod tests { use super::*; - const MAX_DIGITS: usize = 10; fn eval(s: &str) -> Float { 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] fn character_readdition() { - assert_eq!(pretty_eval(" = 11", MAX_DIGITS).unwrap(), " = 11"); - assert_eq!(pretty_eval(" &= 11", MAX_DIGITS).unwrap(), " &= 11"); - assert_eq!(pretty_eval(" & 11", MAX_DIGITS).unwrap(), " & 11"); - assert_eq!(pretty_eval(" & 11 \\", MAX_DIGITS).unwrap(), " & 11 \\"); + assert_pretty_eval!(" = 11"); + assert_pretty_eval!(" &= 11"); + assert_pretty_eval!(" & 11"); + assert_pretty_eval!(" & 11 \\"); } #[test] fn basic_output_up_to_999() { - assert_eq!(pretty_eval("1", MAX_DIGITS).unwrap(), "1"); - assert_eq!(pretty_eval("10", MAX_DIGITS).unwrap(), "10"); - assert_eq!(pretty_eval("100", MAX_DIGITS).unwrap(), "100"); - assert_eq!(pretty_eval("999", MAX_DIGITS).unwrap(), "999"); - assert_eq!(pretty_eval("1000", MAX_DIGITS).unwrap(), "1 * 10^3"); + assert_pretty_eval!("1"); + assert_pretty_eval!("10"); + assert_pretty_eval!("100"); + assert_pretty_eval!("999"); + assert_pretty_eval!("1000", "1 * 10^3"); - assert_eq!(pretty_eval("11.111111", MAX_DIGITS).unwrap(), "11.111111"); + assert_pretty_eval!("11.111111"); } #[test] fn plus_minus() { - assert_eq!( - pretty_eval("y & = plus.minus sqrt(15.5) \\", MAX_DIGITS).unwrap(), + assert_pretty_eval!( + "y & = plus.minus sqrt(15.5) \\", "y & = plus.minus 3.937003937 \\" ); } #[test] fn minus() { - assert_eq!(pretty_eval("-11", MAX_DIGITS).unwrap(), "-11"); + assert_pretty_eval!("-11"); } #[test] fn complex_prefix() { - assert_eq!( - pretty_eval("plus.minus 5 & = 5", MAX_DIGITS).unwrap(), - "plus.minus 5 & = 5" - ); + assert_pretty_eval!("plus.minus 5 & = 5"); } #[test]