From 7a69efec7054d1316d294c830507757a47bf6ec4 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 15 Oct 2008 06:32:42 +0000 Subject: [PATCH] match full account name when (any) account pattern contains :, as ledger seems to --- BalanceCommand.hs | 2 +- Ledger/Ledger.hs | 11 ++++++++--- Tests.hs | 10 ++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/BalanceCommand.hs b/BalanceCommand.hs index 3af213a47..f265b99a3 100644 --- a/BalanceCommand.hs +++ b/BalanceCommand.hs @@ -138,7 +138,7 @@ showBalanceReport opts args l = acctsstr ++ totalstr -- select accounts for which we should show balances, based on the options balancereportaccts :: Bool -> [String] -> Ledger -> [Account] balancereportaccts False [] l = topAccounts l - balancereportaccts False pats l = accountsMatching (regexFor pats) l + balancereportaccts False pats l = accountsMatching pats l balancereportaccts True pats l = addsubaccts l $ balancereportaccts False pats l -- add (in tree order) any missing subacccounts to a list of accounts diff --git a/Ledger/Ledger.hs b/Ledger/Ledger.hs index d7ec26663..08856da7f 100644 --- a/Ledger/Ledger.hs +++ b/Ledger/Ledger.hs @@ -68,9 +68,14 @@ accounts l = drop 1 $ flatten $ ledgerAccountTree 9999 l topAccounts :: Ledger -> [Account] topAccounts l = map root $ branches $ ledgerAccountTree 9999 l --- | Accounts in ledger whose leafname matches the pattern, in tree order -accountsMatching :: Regex -> Ledger -> [Account] -accountsMatching pat l = filter (containsRegex pat . accountLeafName . aname) $ accounts l +-- | Accounts in ledger whose name matches the pattern, in tree order. +-- Like ledger (I think), if the pattern contains : we match the full +-- name, otherwise just the leaf name. +accountsMatching :: [String] -> Ledger -> [Account] +accountsMatching pats l = filter (containsRegex (regexFor pats) . name) $ accounts l + where name = if any (elem ':') pats + then aname + else accountLeafName . aname -- | List a ledger account's immediate subaccounts subAccounts :: Ledger -> Account -> [Account] diff --git a/Tests.hs b/Tests.hs index bcd1c817e..8629c6360 100644 --- a/Tests.hs +++ b/Tests.hs @@ -181,6 +181,16 @@ balancecommandtests = \ $-1\n\ \" --" (showBalanceReport [] ["cash","saving"] l) + , + "balance report with multi-part account name" ~: + do + l <- ledgerfromfile "sample.ledger" + assertequal + " $1 expenses:food\n\ + \--------------------\n\ + \ $1\n\ + \" --" + $ showBalanceReport [] ["expenses:food"] l ] -- | Assert a parsed thing equals some expected thing, or print a parse error.