From c3755f9a5291bfd359f52ed10b79bbf6bb8be8a4 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 4 Sep 2018 07:40:27 -0700 Subject: [PATCH] tests: AccountName -> easytest --- hledger-lib/Hledger/Data.hs | 6 +- hledger-lib/Hledger/Data/AccountName.hs | 86 +++++++++++++++++-------- 2 files changed, 62 insertions(+), 30 deletions(-) diff --git a/hledger-lib/Hledger/Data.hs b/hledger-lib/Hledger/Data.hs index baed3aad5..49e4409c5 100644 --- a/hledger-lib/Hledger/Data.hs +++ b/hledger-lib/Hledger/Data.hs @@ -52,8 +52,7 @@ import Hledger.Utils.Test tests_Hledger_Data = TestList [ - tests_Hledger_Data_AccountName - ,tests_Hledger_Data_Amount + tests_Hledger_Data_Amount ,tests_Hledger_Data_Journal ,tests_Hledger_Data_Ledger ,tests_Hledger_Data_Posting @@ -65,5 +64,6 @@ tests_Hledger_Data = TestList ] easytests_Data = tests "Data" [ - easytests_Journal + easytests_AccountName + ,easytests_Journal ] diff --git a/hledger-lib/Hledger/Data/AccountName.hs b/hledger-lib/Hledger/Data/AccountName.hs index 3a5b867ae..6a6c63561 100644 --- a/hledger-lib/Hledger/Data/AccountName.hs +++ b/hledger-lib/Hledger/Data/AccountName.hs @@ -9,8 +9,38 @@ hierarchy. -} -module Hledger.Data.AccountName +module Hledger.Data.AccountName ( + accountLeafName + ,accountNameComponents + ,accountNameDrop + ,accountNameFromComponents + ,accountNameLevel + ,accountNameToAccountOnlyRegex + ,accountNameToAccountRegex + ,accountNameTreeFrom + ,accountRegexToAccountName + ,accountSummarisedName + ,acctsep + ,acctsepchar + ,clipAccountName + ,clipOrEllipsifyAccountName + ,elideAccountName + ,escapeName + ,expandAccountName + ,expandAccountNames + ,isAccountNamePrefixOf +-- ,isAccountRegex + ,isSubAccountNameOf + ,parentAccountName + ,parentAccountNames + ,subAccountNamesFrom + ,topAccountNames + ,unbudgetedAccountName + ,easytests_AccountName +) where + +import Data.CallStack import Data.List #if !(MIN_VERSION_base(4,11,0)) import Data.Monoid @@ -21,7 +51,7 @@ import Data.Tree import Text.Printf import Hledger.Data.Types -import Hledger.Utils +import Hledger.Utils hiding (is) -- $setup -- >>> :set -XOverloadedStrings @@ -123,7 +153,7 @@ accountNameTreeFrom accts = accounttreesfrom as = [Node a (accounttreesfrom $ subs a) | a <- as] subs = subAccountNamesFrom (expandAccountNames accts) -nullaccountnametree = Node "root" [] +--nullaccountnametree = Node "root" [] -- | Elide an account name to fit in the specified width. -- From the ledger 2.6 news: @@ -193,33 +223,35 @@ accountNameToAccountOnlyRegex a = printf "^%s$" $ escapeName a -- XXX pack accountRegexToAccountName :: Regexp -> AccountName accountRegexToAccountName = T.pack . regexReplace "^\\^(.*?)\\(:\\|\\$\\)$" "\\1" -- XXX pack --- | Does this string look like an exact account-matching regular expression ? -isAccountRegex :: String -> Bool -isAccountRegex s = take 1 s == "^" && take 5 (reverse s) == ")$|:(" +-- -- | Does this string look like an exact account-matching regular expression ? +--isAccountRegex :: String -> Bool +--isAccountRegex s = take 1 s == "^" && take 5 (reverse s) == ")$|:(" -tests_Hledger_Data_AccountName = TestList - [ - "accountNameTreeFrom" ~: do - accountNameTreeFrom ["a"] `is` Node "root" [Node "a" []] - accountNameTreeFrom ["a","b"] `is` Node "root" [Node "a" [], Node "b" []] - accountNameTreeFrom ["a","a:b"] `is` Node "root" [Node "a" [Node "a:b" []]] - accountNameTreeFrom ["a:b:c"] `is` Node "root" [Node "a" [Node "a:b" [Node "a:b:c" []]]] +is :: (Eq a, Show a, HasCallStack) => a -> a -> Test () +is = flip expectEq' - ,"expandAccountNames" ~: +easytests_AccountName = tests "AccountName" [ + tests "accountNameTreeFrom" [ + accountNameTreeFrom ["a"] `is` Node "root" [Node "a" []] + ,accountNameTreeFrom ["a","b"] `is` Node "root" [Node "a" [], Node "b" []] + ,accountNameTreeFrom ["a","a:b"] `is` Node "root" [Node "a" [Node "a:b" []]] + ,accountNameTreeFrom ["a:b:c"] `is` Node "root" [Node "a" [Node "a:b" [Node "a:b:c" []]]] + ] + ,tests "expandAccountNames" [ expandAccountNames ["assets:cash","assets:checking","expenses:vacation"] `is` ["assets","assets:cash","assets:checking","expenses","expenses:vacation"] - - ,"isAccountNamePrefixOf" ~: do - "assets" `isAccountNamePrefixOf` "assets" `is` False - "assets" `isAccountNamePrefixOf` "assets:bank" `is` True - "assets" `isAccountNamePrefixOf` "assets:bank:checking" `is` True - "my assets" `isAccountNamePrefixOf` "assets:bank" `is` False - - ,"isSubAccountNameOf" ~: do - "assets" `isSubAccountNameOf` "assets" `is` False - "assets:bank" `isSubAccountNameOf` "assets" `is` True - "assets:bank:checking" `isSubAccountNameOf` "assets" `is` False - "assets:bank" `isSubAccountNameOf` "my assets" `is` False - + ] + ,tests "isAccountNamePrefixOf" [ + "assets" `isAccountNamePrefixOf` "assets" `is` False + ,"assets" `isAccountNamePrefixOf` "assets:bank" `is` True + ,"assets" `isAccountNamePrefixOf` "assets:bank:checking" `is` True + ,"my assets" `isAccountNamePrefixOf` "assets:bank" `is` False + ] + ,tests "isSubAccountNameOf" [ + "assets" `isSubAccountNameOf` "assets" `is` False + ,"assets:bank" `isSubAccountNameOf` "assets" `is` True + ,"assets:bank:checking" `isSubAccountNameOf` "assets" `is` False + ,"assets:bank" `isSubAccountNameOf` "my assets" `is` False + ] ]