Skip to content

Commit

Permalink
add some test for empty postings #82
Browse files Browse the repository at this point in the history
  • Loading branch information
frosklis committed Aug 29, 2021
1 parent cc47f7a commit 2151427
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/commands/roi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 33 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,13 @@ impl Display for TimeParseError {

#[derive(Debug)]
pub enum LedgerError {
EmptyPostingShouldBeLast,
AliasNotInList(String),
TooManyEmptyPostings(usize),
}
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())
Expand Down Expand Up @@ -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::<LedgerError>().unwrap();
match ledger_error {
LedgerError::TooManyEmptyPostings(x) => (),
other => {
dbg!(other);
panic!("Too many empty postings");
}
}
}
}
}

0 comments on commit 2151427

Please sign in to comment.