From 2151427fb9511f374233768da0c6feded7e069f2 Mon Sep 17 00:00:00 2001 From: Claudio Noguera Date: Sun, 29 Aug 2021 23:25:17 +0200 Subject: [PATCH] add some test for empty postings #82 --- src/commands/roi.rs | 2 +- src/error.rs | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/commands/roi.rs b/src/commands/roi.rs index 74577cf..06b5f8d 100644 --- a/src/commands/roi.rs +++ b/src/commands/roi.rs @@ -206,7 +206,7 @@ pub fn execute( // Period: 5.41 years. // Annualized TWR: 10.12% let mut total_twr = 1.0; - let mut irr = irr(&cash_flows); + let irr = irr(&cash_flows); for p in periods.iter() { total_twr *= 1.0 + p.twr().to_f64().unwrap(); // dbg!(&total_twr); diff --git a/src/error.rs b/src/error.rs index fff74f1..4d90fed 100644 --- a/src/error.rs +++ b/src/error.rs @@ -44,7 +44,6 @@ impl Display for TimeParseError { #[derive(Debug)] pub enum LedgerError { - EmptyPostingShouldBeLast, AliasNotInList(String), TooManyEmptyPostings(usize), } @@ -52,9 +51,6 @@ impl Error for LedgerError {} impl Display for LedgerError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { - LedgerError::EmptyPostingShouldBeLast => { - write!(f, "{}", "Empty posting should be last".red()) - } LedgerError::AliasNotInList(x) => write!(f, "Alias not found: {}", x), LedgerError::TooManyEmptyPostings(_) => { write!(f, "{}", "Too many empty postings".red()) @@ -118,3 +114,36 @@ impl<'a> fmt::Display for ColoredStrings<'a> { }) } } + +#[cfg(test)] +mod tests { + use structopt::StructOpt; + + use super::LedgerError; + use crate::{parser::Tokenizer, CommonOpts}; + + #[test] + fn error_empty_posting_last() { + let mut tokenizer = Tokenizer::from( + "2021-06-05 Flight + Assets:Checking + Expenses:Comissions + Expenses:Travel 200 EUR" + .to_string(), + ); + let options = CommonOpts::from_iter(["", "-f", ""].iter()); + let parsed = tokenizer.tokenize(&options); + let ledger = parsed.to_ledger(&options); + assert!(ledger.is_err()); + if let Err(err) = ledger { + let ledger_error = err.downcast_ref::().unwrap(); + match ledger_error { + LedgerError::TooManyEmptyPostings(x) => (), + other => { + dbg!(other); + panic!("Too many empty postings"); + } + } + } + } +}