From bb985f917a96b1baa6a04a396baf3c611ad374d9 Mon Sep 17 00:00:00 2001 From: electria Date: Tue, 6 Oct 2026 16:21:18 -0700 Subject: [PATCH] refactor: create submodules for tests --- src/nodes/eval.rs | 54 +++++++++++++++-------------- src/nodes/parse.rs | 84 +++++++++++++++++++++++++--------------------- 2 files changed, 73 insertions(+), 65 deletions(-) diff --git a/src/nodes/eval.rs b/src/nodes/eval.rs index 5980b6e..7be6bf7 100644 --- a/src/nodes/eval.rs +++ b/src/nodes/eval.rs @@ -192,32 +192,34 @@ impl Function { } #[cfg(test)] -use super::PREC; -#[cfg(test)] -use std::assert_matches; +mod tests { + use super::super::PREC; + use super::*; + use std::assert_matches; -#[test] -fn test() { - assert_eq!( - Nodes(vec![ - Node::Function(Function::Sine), - Node::ParenthesisStart, - Node::Number(Float::with_val(PREC, 2)), - Node::ParenthesisEnd, - Node::Operator(Operator::Plus), - Node::Number(Float::with_val(PREC, 1)) - ]) - .evaluate() - .unwrap() - .to_f64(), - 1.9092974268256817, - ); -} + #[test] + fn basic() { + assert_eq!( + Nodes(vec![ + Node::Function(Function::Sine), + Node::ParenthesisStart, + Node::Number(Float::with_val(PREC, 2)), + Node::ParenthesisEnd, + Node::Operator(Operator::Plus), + Node::Number(Float::with_val(PREC, 1)) + ]) + .evaluate() + .unwrap() + .to_f64(), + 1.9092974268256817, + ); + } -#[test] -fn test_zerosized() { - assert_matches!( - Nodes(vec![Node::ParenthesisStart, Node::ParenthesisEnd]).evaluate(), - Err(Error::CollapseFailed(Nodes(v))) if v.is_empty() - ) + #[test] + fn zerosized() { + assert_matches!( + Nodes(vec![Node::ParenthesisStart, Node::ParenthesisEnd]).evaluate(), + Err(Error::CollapseFailed(Nodes(v))) if v.is_empty() + ) + } } diff --git a/src/nodes/parse.rs b/src/nodes/parse.rs index 31a2088..0405be0 100644 --- a/src/nodes/parse.rs +++ b/src/nodes/parse.rs @@ -195,43 +195,49 @@ impl FromStr for Nodes { } } -#[test] -fn test() { - assert_eq!( - Nodes::from_str("sin(2) + 1").unwrap().as_ref(), - &[ - Node::Function(Function::Sine), - Node::ParenthesisStart, - Node::from(2), - Node::ParenthesisEnd, - Node::Operator(Operator::Plus), - Node::from(1), - ], - ) -} -#[test] -fn test_e() { - assert_eq!( - Nodes::from_str("5e * 5").unwrap().as_ref(), - &[ - Node::from(5), - Node::Operator(Operator::Mult), - Node::from(f64::consts::E), - Node::Operator(Operator::Mult), - Node::from(5), - ], - ) -} -#[test] -fn test_unary_op() { - assert_eq!( - Nodes::from_str("5(-5)").unwrap().as_ref(), - &[ - Node::from(5), - Node::Operator(Operator::Mult), - Node::ParenthesisStart, - Node::from(-5), - Node::ParenthesisEnd, - ], - ) +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn basic() { + assert_eq!( + Nodes::from_str("sin(2) + 1").unwrap().as_ref(), + &[ + Node::Function(Function::Sine), + Node::ParenthesisStart, + Node::from(2), + Node::ParenthesisEnd, + Node::Operator(Operator::Plus), + Node::from(1), + ], + ) + } + + #[test] + fn e_constant() { + assert_eq!( + Nodes::from_str("5e * 5").unwrap().as_ref(), + &[ + Node::from(5), + Node::Operator(Operator::Mult), + Node::from(f64::consts::E), + Node::Operator(Operator::Mult), + Node::from(5), + ], + ) + } + #[test] + fn unary_op() { + assert_eq!( + Nodes::from_str("5(-5)").unwrap().as_ref(), + &[ + Node::from(5), + Node::Operator(Operator::Mult), + Node::ParenthesisStart, + Node::from(-5), + Node::ParenthesisEnd, + ], + ) + } }