Skip to content
Permalink
Browse files

;tests: use "test" alias for testCase everywhere

Easier to type and read, and a little clearer to my eyes;
"testCase" implies a single case, but it can contain many assertions.
  • Loading branch information...
simonmichael committed Nov 28, 2019
1 parent 030b633 commit 2b2a0b3cf89af8ef7101e058639d89762293badb
@@ -698,10 +698,16 @@ About testing in the hledger project, as of 201809.
tests. These are mostly in hledger-lib, with a few in hledger.

Our unit tests use the
[tasty](http://hackage.haskell.org/package/tasty) test runner
[tasty](http://hackage.haskell.org/package/tasty) test runner,
[tasty-hunit](http://hackage.haskell.org/package/tasty-hunit) HUnit-style tests,
and some helpers from
[Hledger.Utils.Test](https://github.com/simonmichael/hledger/blob/master/hledger-lib/Hledger/Utils/Test.hs).
We would like them to be:
[Hledger.Utils.Test](https://github.com/simonmichael/hledger/blob/master/hledger-lib/Hledger/Utils/Test.hs),
such as:

- `tests` and `test` aliases for `testGroup` and `testCase`
- `assert*` helpers for constructing various kinds of assertions

We would like our unit tests to be:

- easy to read (clear, concise)
- easy to write (low boilerplate, low cognitive load)
@@ -227,20 +227,20 @@ accountRegexToAccountName = T.pack . regexReplace "^\\^(.*?)\\(:\\|\\$\\)$" "\\1
--isAccountRegex s = take 1 s == "^" && take 5 (reverse s) == ")$|:("

tests_AccountName = tests "AccountName" [
testCase "accountNameTreeFrom" $ do
test "accountNameTreeFrom" $ do
accountNameTreeFrom ["a"] @?= Node "root" [Node "a" []]
accountNameTreeFrom ["a","b"] @?= Node "root" [Node "a" [], Node "b" []]
accountNameTreeFrom ["a","a:b"] @?= Node "root" [Node "a" [Node "a:b" []]]
accountNameTreeFrom ["a:b:c"] @?= Node "root" [Node "a" [Node "a:b" [Node "a:b:c" []]]]
,testCase "expandAccountNames" $ do
,test "expandAccountNames" $ do
expandAccountNames ["assets:cash","assets:checking","expenses:vacation"] @?=
["assets","assets:cash","assets:checking","expenses","expenses:vacation"]
,testCase "isAccountNamePrefixOf" $ do
,test "isAccountNamePrefixOf" $ do
"assets" `isAccountNamePrefixOf` "assets" @?= False
"assets" `isAccountNamePrefixOf` "assets:bank" @?= True
"assets" `isAccountNamePrefixOf` "assets:bank:checking" @?= True
"my assets" `isAccountNamePrefixOf` "assets:bank" @?= False
,testCase "isSubAccountNameOf" $ do
,test "isSubAccountNameOf" $ do
"assets" `isSubAccountNameOf` "assets" @?= False
"assets:bank" `isSubAccountNameOf` "assets" @?= True
"assets:bank:checking" `isSubAccountNameOf` "assets" @?= False
@@ -735,21 +735,21 @@ mixedAmountTotalPriceToUnitPrice (Mixed as) = Mixed $ map amountTotalPriceToUnit
tests_Amount = tests "Amount" [
tests "Amount" [

testCase "costOfAmount" $ do
test "costOfAmount" $ do
costOfAmount (eur 1) @?= eur 1
costOfAmount (eur 2){aprice=Just $ UnitPrice $ usd 2} @?= usd 4
costOfAmount (eur 1){aprice=Just $ TotalPrice $ usd 2} @?= usd 2
costOfAmount (eur (-1)){aprice=Just $ TotalPrice $ usd 2} @?= usd (-2)

,testCase "isZeroAmount" $ do
,test "isZeroAmount" $ do
assertBool "" $ isZeroAmount amount
assertBool "" $ isZeroAmount $ usd 0

,testCase "negating amounts" $ do
,test "negating amounts" $ do
negate (usd 1) @?= (usd 1){aquantity= -1}
let b = (usd 1){aprice=Just $ UnitPrice $ eur 2} in negate b @?= b{aquantity= -1}

,testCase "adding amounts without prices" $ do
,test "adding amounts without prices" $ do
(usd 1.23 + usd (-1.23)) @?= usd 0
(usd 1.23 + usd (-1.23)) @?= usd 0
(usd (-1.23) + usd (-1.23)) @?= usd (-2.46)
@@ -760,22 +760,22 @@ tests_Amount = tests "Amount" [
-- adding different commodities assumes conversion rate 1
assertBool "" $ isZeroAmount (usd 1.23 - eur 1.23)

,testCase "showAmount" $ do
,test "showAmount" $ do
showAmount (usd 0 + gbp 0) @?= "0"

]

,tests "MixedAmount" [

testCase "adding mixed amounts to zero, the commodity and amount style are preserved" $
test "adding mixed amounts to zero, the commodity and amount style are preserved" $
sum (map (Mixed . (:[]))
[usd 1.25
,usd (-1) `withPrecision` 3
,usd (-0.25)
])
@?= Mixed [usd 0 `withPrecision` 3]

,testCase "adding mixed amounts with total prices" $ do
,test "adding mixed amounts with total prices" $ do
sum (map (Mixed . (:[]))
[usd 1 @@ eur 1
,usd (-2) @@ eur 1
@@ -784,32 +784,32 @@ tests_Amount = tests "Amount" [
,usd (-2) @@ eur 1
]

,testCase "showMixedAmount" $ do
,test "showMixedAmount" $ do
showMixedAmount (Mixed [usd 1]) @?= "$1.00"
showMixedAmount (Mixed [usd 1 `at` eur 2]) @?= "$1.00 @ €2.00"
showMixedAmount (Mixed [usd 0]) @?= "0"
showMixedAmount (Mixed []) @?= "0"
showMixedAmount missingmixedamt @?= ""

,testCase "showMixedAmountWithoutPrice" $ do
,test "showMixedAmountWithoutPrice" $ do
let a = usd 1 `at` eur 2
showMixedAmountWithoutPrice (Mixed [a]) @?= "$1.00"
showMixedAmountWithoutPrice (Mixed [a, -a]) @?= "0"

,tests "normaliseMixedAmount" [
testCase "a missing amount overrides any other amounts" $
test "a missing amount overrides any other amounts" $
normaliseMixedAmount (Mixed [usd 1, missingamt]) @?= missingmixedamt
,testCase "unpriced same-commodity amounts are combined" $
,test "unpriced same-commodity amounts are combined" $
normaliseMixedAmount (Mixed [usd 0, usd 2]) @?= Mixed [usd 2]
,testCase "amounts with same unit price are combined" $
,test "amounts with same unit price are combined" $
normaliseMixedAmount (Mixed [usd 1 `at` eur 1, usd 1 `at` eur 1]) @?= Mixed [usd 2 `at` eur 1]
,testCase "amounts with different unit prices are not combined" $
,test "amounts with different unit prices are not combined" $
normaliseMixedAmount (Mixed [usd 1 `at` eur 1, usd 1 `at` eur 2]) @?= Mixed [usd 1 `at` eur 1, usd 1 `at` eur 2]
,testCase "amounts with total prices are not combined" $
,test "amounts with total prices are not combined" $
normaliseMixedAmount (Mixed [usd 1 @@ eur 1, usd 1 @@ eur 1]) @?= Mixed [usd 1 @@ eur 1, usd 1 @@ eur 1]
]

,testCase "normaliseMixedAmountSquashPricesForDisplay" $ do
,test "normaliseMixedAmountSquashPricesForDisplay" $ do
normaliseMixedAmountSquashPricesForDisplay (Mixed []) @?= Mixed [nullamt]
assertBool "" $ isZeroMixedAmount $ normaliseMixedAmountSquashPricesForDisplay
(Mixed [usd 10
@@ -1296,7 +1296,7 @@ Right samplejournal = journalBalanceTransactions False $

tests_Journal = tests "Journal" [

testCase "journalDateSpan" $
test "journalDateSpan" $
journalDateSpan True nulljournal{
jtxns = [nulltransaction{tdate = parsedate "2014/02/01"
,tpostings = [posting{pdate=Just (parsedate "2014/01/10")}]
@@ -1315,16 +1315,16 @@ tests_Journal = tests "Journal" [
journalAccountNamesMatching q = filter (q `matchesAccount`) . journalAccountNames
namesfrom qfunc = journalAccountNamesMatching (qfunc j) j
in [
testCase "assets" $ assertEqual "" (namesfrom journalAssetAccountQuery) ["assets","assets:bank","assets:bank:checking","assets:bank:saving","assets:cash"]
,testCase "liabilities" $ assertEqual "" (namesfrom journalLiabilityAccountQuery) ["liabilities","liabilities:debts"]
,testCase "equity" $ assertEqual "" (namesfrom journalEquityAccountQuery) []
,testCase "income" $ assertEqual "" (namesfrom journalRevenueAccountQuery) ["income","income:gifts","income:salary"]
,testCase "expenses" $ assertEqual "" (namesfrom journalExpenseAccountQuery) ["expenses","expenses:food","expenses:supplies"]
test "assets" $ assertEqual "" (namesfrom journalAssetAccountQuery) ["assets","assets:bank","assets:bank:checking","assets:bank:saving","assets:cash"]
,test "liabilities" $ assertEqual "" (namesfrom journalLiabilityAccountQuery) ["liabilities","liabilities:debts"]
,test "equity" $ assertEqual "" (namesfrom journalEquityAccountQuery) []
,test "income" $ assertEqual "" (namesfrom journalRevenueAccountQuery) ["income","income:gifts","income:salary"]
,test "expenses" $ assertEqual "" (namesfrom journalExpenseAccountQuery) ["expenses","expenses:food","expenses:supplies"]
]

,tests "journalBalanceTransactions" [

testCase "balance-assignment" $ do
test "balance-assignment" $ do
let ej = journalBalanceTransactions True $
--2019/01/01
-- (a) = 1
@@ -1335,7 +1335,7 @@ tests_Journal = tests "Journal" [
let Right j = ej
(jtxns j & head & tpostings & head & pamount) @?= Mixed [num 1]

,testCase "same-day-1" $ do
,test "same-day-1" $ do
assertRight $ journalBalanceTransactions True $
--2019/01/01
-- (a) = 1
@@ -1346,7 +1346,7 @@ tests_Journal = tests "Journal" [
,transaction "2019/01/01" [ vpost' "a" (num 1) (balassert (num 2)) ]
]}

,testCase "same-day-2" $ do
,test "same-day-2" $ do
assertRight $ journalBalanceTransactions True $
--2019/01/01
-- (a) 2 = 2
@@ -1364,7 +1364,7 @@ tests_Journal = tests "Journal" [
,transaction "2019/01/01" [ post' "a" (num 0) (balassert (num 1)) ]
]}

,testCase "out-of-order" $ do
,test "out-of-order" $ do
assertRight $ journalBalanceTransactions True $
--2019/1/2
-- (a) 1 = 2
@@ -1386,7 +1386,7 @@ tests_Journal = tests "Journal" [
-- 2019/09/26
-- (a) 1000,000
--
testCase "1091a" $ do
test "1091a" $ do
commodityStylesFromAmounts [
nullamt{aquantity=1000, astyle=AmountStyle L False 3 (Just ',') Nothing}
,nullamt{aquantity=1000, astyle=AmountStyle L False 2 (Just '.') (Just (DigitGroups ',' [3]))}
@@ -1398,7 +1398,7 @@ tests_Journal = tests "Journal" [
("", AmountStyle L False 3 (Just '.') (Just (DigitGroups ',' [3])))
])
-- same journal, entries in reverse order
,testCase "1091b" $ do
,test "1091b" $ do
commodityStylesFromAmounts [
nullamt{aquantity=1000, astyle=AmountStyle L False 2 (Just '.') (Just (DigitGroups ',' [3]))}
,nullamt{aquantity=1000, astyle=AmountStyle L False 3 (Just ',') Nothing}
@@ -110,7 +110,7 @@ ledgerCommodities = M.keys . jinferredcommodities . ljournal

tests_Ledger =
tests "Ledger" [
testCase "ledgerFromJournal" $ do
test "ledgerFromJournal" $ do
length (ledgerPostings $ ledgerFromJournal Any nulljournal) @?= 0
length (ledgerPostings $ ledgerFromJournal Any samplejournal) @?= 13
length (ledgerPostings $ ledgerFromJournal (Depth 2) samplejournal) @?= 7
@@ -392,32 +392,32 @@ commentAddTagNextLine cmt (t,v) =

tests_Posting = tests "Posting" [

testCase "accountNamePostingType" $ do
test "accountNamePostingType" $ do
accountNamePostingType "a" @?= RegularPosting
accountNamePostingType "(a)" @?= VirtualPosting
accountNamePostingType "[a]" @?= BalancedVirtualPosting

,testCase "accountNameWithoutPostingType" $ do
,test "accountNameWithoutPostingType" $ do
accountNameWithoutPostingType "(a)" @?= "a"

,testCase "accountNameWithPostingType" $ do
,test "accountNameWithPostingType" $ do
accountNameWithPostingType VirtualPosting "[a]" @?= "(a)"

,testCase "joinAccountNames" $ do
,test "joinAccountNames" $ do
"a" `joinAccountNames` "b:c" @?= "a:b:c"
"a" `joinAccountNames` "(b:c)" @?= "(a:b:c)"
"[a]" `joinAccountNames` "(b:c)" @?= "[a:b:c]"
"" `joinAccountNames` "a" @?= "a"

,testCase "concatAccountNames" $ do
,test "concatAccountNames" $ do
concatAccountNames [] @?= ""
concatAccountNames ["a","(b)","[c:d]"] @?= "(a:b:c:d)"

,testCase "commentAddTag" $ do
,test "commentAddTag" $ do
commentAddTag "" ("a","") @?= "a: "
commentAddTag "[1/2]" ("a","") @?= "[1/2], a: "

,testCase "commentAddTagNextLine" $ do
,test "commentAddTagNextLine" $ do
commentAddTagNextLine "" ("a","") @?= "\na: "
commentAddTagNextLine "[1/2]" ("a","") @?= "[1/2]\na: "

@@ -145,7 +145,7 @@ formatStringTester fs value expected = actual @?= expected

tests_StringFormat = tests "StringFormat" [

testCase "formatStringHelper" $ do
test "formatStringHelper" $ do
formatStringTester (FormatLiteral " ") "" " "
formatStringTester (FormatField False Nothing Nothing DescriptionField) "description" "description"
formatStringTester (FormatField False (Just 20) Nothing DescriptionField) "description" " description"
@@ -155,7 +155,7 @@ tests_StringFormat = tests "StringFormat" [
formatStringTester (FormatField True (Just 20) (Just 20) DescriptionField) "description" "description "
formatStringTester (FormatField True Nothing (Just 3) DescriptionField) "description" "des"

,let s `gives` expected = testCase s $ parseStringFormat s @?= Right expected
,let s `gives` expected = test s $ parseStringFormat s @?= Right expected
in tests "parseStringFormat" [
"" `gives` (defaultStringFormatStyle [])
, "D" `gives` (defaultStringFormatStyle [FormatLiteral "D"])
@@ -174,6 +174,6 @@ tests_StringFormat = tests "StringFormat" [
,FormatLiteral " "
,FormatField False Nothing (Just 10) TotalField
])
, testCase "newline not parsed" $ assertLeft $ parseStringFormat "\n"
, test "newline not parsed" $ assertLeft $ parseStringFormat "\n"
]
]

0 comments on commit 2b2a0b3

Please sign in to comment.
You can’t perform that action at this time.