From aaf50c165c2925a22952cb52b5b57807a33d9ac0 Mon Sep 17 00:00:00 2001 From: Jonathan Dowland Date: Sat, 28 Oct 2023 21:51:28 +0100 Subject: [PATCH] test: import: functional tests for match group interpolation Add functional tests for matching substrings in field matchers and interpolating them into the corresponding field assignments. Check the following properties and use-cases: * Use-case 1: matching a portion of a date in a known format (YYY-MM-DD) and writing a comment-command to warp a posting date. Useful for credit cards. * Use-case 2: match a portion of a CSV field and use it as an account assignment. Useful for my byzantine setup with two separate ledgers cross-importing to each other. * Ensure bracketed portions of field matchers are captured. * Ensure bracketed portions of record matchers are captured. * Check match token numerical offset is relative to match group, not the whole rules file. * Check nested matches work. * Ensure match group token expansion works with or without surrounding text. Signed-off-by: Jonathan Dowland --- hledger/test/import/match.csv | 1 + hledger/test/import/match.rules | 16 ++++++++++++++++ hledger/test/import/match.test | 10 ++++++++++ 3 files changed, 27 insertions(+) create mode 100644 hledger/test/import/match.csv create mode 100644 hledger/test/import/match.rules create mode 100644 hledger/test/import/match.test diff --git a/hledger/test/import/match.csv b/hledger/test/import/match.csv new file mode 100644 index 000000000..c9c45b9f4 --- /dev/null +++ b/hledger/test/import/match.csv @@ -0,0 +1 @@ +"62","2022-12-15","","Zettle_*Robert W. Bell","liabilities:jon:expenses:snacks","£ 7.90","£ 8.90" diff --git a/hledger/test/import/match.rules b/hledger/test/import/match.rules new file mode 100644 index 000000000..db087e2f6 --- /dev/null +++ b/hledger/test/import/match.rules @@ -0,0 +1,16 @@ +fields _, date, _, description, account1, amount, _ +# checking brackets in a field matcher are captured... +if %account1 liabilities:jon:(.*) +# ...and interpolated into field assignments (real-life use-case #2) + account1 \1 +# checking brackets in a record matcher are captured, including +# nesting... +if Zettle.*(Robert W. (Bell)).*£ +# ... and the interpolation token numerical offset is local to +# this match group; and the token is interpolated into surrounding +# text + comment1 Bell=\2. +# Match the YYY-MM of a date field and warp the posting date, useful +# for credit cards (real-life use-case #1)) +if %date (....-..)-.. + comment2 date:\1-01 diff --git a/hledger/test/import/match.test b/hledger/test/import/match.test new file mode 100644 index 000000000..728c9b982 --- /dev/null +++ b/hledger/test/import/match.test @@ -0,0 +1,10 @@ +# Various match group interpolation tests. See the comments +# in match.rules for specifics. +$ hledger -f a.j import --rules-file match.rules --dry-run match.csv +; would import 1 new transactions from match.csv: + +2022-12-15 Zettle_*Robert W. Bell + expenses:snacks £ 7.90 ; Bell=Bell. + income:unknown £ -7.90 ; date:2022-12-01 + +>=