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 <jon@dow.land>
This commit is contained in:
Jonathan Dowland 2023-10-28 21:51:28 +01:00 committed by Simon Michael
parent 8bfa382c68
commit aaf50c165c
3 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1 @@
"62","2022-12-15","","Zettle_*Robert W. Bell","liabilities:jon:expenses:snacks","£ 7.90","£ 8.90"
1 62 2022-12-15 Zettle_*Robert W. Bell liabilities:jon:expenses:snacks £ 7.90 £ 8.90

View File

@ -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

View File

@ -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
>=