From 158590cc831ece91cf606e57007b2b8c4f603a25 Mon Sep 17 00:00:00 2001 From: Josua Kehlenbach Date: Fri, 29 Mar 2024 19:47:01 +0100 Subject: [PATCH 1/3] adapt tests --- tests/parse_from_file_spec.rs | 4 ++-- tests/parser_spec.rs | 2 +- tests/samples/simple.beancount | 6 ++++++ tests/trx_parser_spec.rs | 4 ++-- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/parse_from_file_spec.rs b/tests/parse_from_file_spec.rs index a4bcaf5..1860df1 100644 --- a/tests/parse_from_file_spec.rs +++ b/tests/parse_from_file_spec.rs @@ -6,8 +6,8 @@ use beancount_parser::BeancountFile; #[rstest] #[case("comments.beancount", 0)] -#[case("simple.beancount", 13)] -#[case("official.beancount", 2151)] +#[case("simple.beancount", 16)] +#[case("official.beancount", 2154)] fn can_parse_example_files(#[case] file_name: &str, #[case] expected_directive_count: usize) { let mut path: PathBuf = "./tests/samples".into(); path.push(file_name); diff --git a/tests/parser_spec.rs b/tests/parser_spec.rs index 7ead1f7..23540b1 100644 --- a/tests/parser_spec.rs +++ b/tests/parser_spec.rs @@ -17,7 +17,7 @@ fn should_succeed_for_valid_input(#[values("", "\n", COMMENTS, SIMPLE, OFFICIAL) #[rstest] #[case("", 0)] -#[case(SIMPLE, 10)] +#[case(SIMPLE, 12)] fn should_find_all_open_directives(#[case] input: &str, #[case] expected_count: usize) { let actual_count = parse::(input) .expect("parsing should succeed") diff --git a/tests/samples/simple.beancount b/tests/samples/simple.beancount index 547899f..5c25ec9 100644 --- a/tests/samples/simple.beancount +++ b/tests/samples/simple.beancount @@ -1,6 +1,7 @@ 2000-01-01 open Assets:AccountsReceivable:Michael USD 2000-01-01 open Assets:MyBank:Checking USD 2000-01-01 open Assets:MyBank:Savings USD +2000-01-01 open Aktiva:MyBank:Checking USD 2000-01-01 open Liabilities:CreditCard:CapitalOne 2000-01-01 open Income:AcmeCorp:Salary 2000-01-01 open Expenses:Taxes:TY2014:Federal @@ -8,7 +9,12 @@ 2000-01-01 open Expenses:Taxes:TY2014:Medicare 2000-01-01 open Expenses:Taxes:TY2014:StateNY 2000-01-01 open Expenses:Taxes:TY2014:SDI +2000-01-01 open Ausgaben:Taxes:TY2014:SDI +2014-10-05 * "Costco" "Shopping for birthday" + Schulden:CreditCard:CapitalOne -45.00 USD + Aktiva:AccountsReceivable:Michael 40.00 USD + Ausgaben:Shopping 2014-10-05 * "Costco" "Shopping for birthday" Liabilities:CreditCard:CapitalOne -45.00 USD diff --git a/tests/trx_parser_spec.rs b/tests/trx_parser_spec.rs index e7c3ffb..4c4048d 100644 --- a/tests/trx_parser_spec.rs +++ b/tests/trx_parser_spec.rs @@ -16,7 +16,7 @@ const OFFICIAL: &str = include_str!("samples/official.beancount"); #[rstest] #[case("", 0)] #[case(COMMENTS, 0)] -#[case(SIMPLE, 3)] +#[case(SIMPLE, 4)] #[case(OFFICIAL, 1096)] fn should_find_all_transactions(#[case] input: &str, #[case] expected_count: usize) { let actual_count = parse_iter::(input) @@ -35,7 +35,7 @@ fn should_find_all_transactions(#[case] input: &str, #[case] expected_count: usi #[rstest] #[case("", 0)] #[case(COMMENTS, 0)] -#[case(SIMPLE, 12)] +#[case(SIMPLE, 15)] #[case(OFFICIAL, 3385)] fn should_find_all_postings(#[case] input: &str, #[case] expected_count: usize) { let actual_count: usize = parse::(input) From 3faf2f5ff7a015d150179e3b259457bb928ff30e Mon Sep 17 00:00:00 2001 From: Josua Kehlenbach Date: Fri, 29 Mar 2024 19:47:41 +0100 Subject: [PATCH 2/3] allow any account name --- src/account.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/account.rs b/src/account.rs index f1567d5..7bc5ae8 100644 --- a/src/account.rs +++ b/src/account.rs @@ -7,8 +7,7 @@ use std::{ }; use nom::{ - branch::alt, - bytes::{complete::tag, complete::take_while}, + bytes::complete::take_while, character::complete::{char, satisfy, space0, space1}, combinator::{all_consuming, cut, iterator, opt, recognize}, multi::many1_count, @@ -192,13 +191,10 @@ pub struct Pad { pub(super) fn parse(input: Span<'_>) -> IResult<'_, Account> { let (input, name) = recognize(preceded( - alt(( - tag("Expenses"), - tag("Assets"), - tag("Liabilities"), - tag("Income"), - tag("Equity"), - )), + preceded( + satisfy(|c: char| c.is_uppercase() || c.is_ascii_digit()), + take_while(|c: char| c.is_alphanumeric() || c == '-'), + ), cut(many1_count(preceded( char(':'), preceded( From 6e2b23994ad876118811624052289d7903b520c3 Mon Sep 17 00:00:00 2001 From: Josua Kehlenbach Date: Fri, 29 Mar 2024 21:55:03 +0100 Subject: [PATCH 3/3] Add explicit test --- tests/parser_spec.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/parser_spec.rs b/tests/parser_spec.rs index 23540b1..5fe9748 100644 --- a/tests/parser_spec.rs +++ b/tests/parser_spec.rs @@ -126,6 +126,7 @@ fn should_parse_balance_assertion_amount( #[case::dash("Assets:Hello-world")] #[case::num_at_end("Assets:Cash2")] #[case::num_at_start("Assets:2Cash")] +#[case::non_standard_name("Ausgaben:A")] fn account_from_str_should_parse_valid_account(#[case] input: &str) { let account: Account = input.parse().unwrap(); assert_eq!(account.as_str(), input);