1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
{-# LANGUAGE CPP #-}
{-|
A 'BalanceData is a data type tracking a number of postings, exclusive, and inclusive balance
for given date ranges.
-}
module Hledger.Data.BalanceData
( mapBalanceData
, opBalanceData
, tests_BalanceData
) where
import Test.Tasty (testGroup)
import Test.Tasty.HUnit ((@?=), testCase)
import Hledger.Data.Amount
import Hledger.Data.Types
instance Show BalanceData where
showsPrec d (BalanceData e i n) =
showParen (d > 10) $
showString "BalanceData"
. showString "{ bdexcludingsubs = " . showString (wbUnpack (showMixedAmountB defaultFmt e))
. showString ", bdincludingsubs = " . showString (wbUnpack (showMixedAmountB defaultFmt i))
. showString ", bdnumpostings = " . shows n
. showChar '}'
instance Semigroup BalanceData where
BalanceData e i n <> BalanceData e' i' n' = BalanceData (maPlus e e') (maPlus i i') (n + n')
instance Monoid BalanceData where
mempty = BalanceData nullmixedamt nullmixedamt 0
-- | Apply an operation to both 'MixedAmount' in an 'BalanceData'.
mapBalanceData :: (MixedAmount -> MixedAmount) -> BalanceData -> BalanceData
mapBalanceData f a = a{bdexcludingsubs = f $ bdexcludingsubs a, bdincludingsubs = f $ bdincludingsubs a}
-- | Merge two 'BalanceData', using the given operation to combine their amounts.
opBalanceData :: (MixedAmount -> MixedAmount -> MixedAmount) -> BalanceData -> BalanceData -> BalanceData
opBalanceData f a b = a{bdexcludingsubs = f (bdexcludingsubs a) (bdexcludingsubs b), bdincludingsubs = f (bdincludingsubs a) (bdincludingsubs b)}
-- tests
tests_BalanceData = testGroup "BalanceData" [
testCase "opBalanceData maPlus" $ do
opBalanceData maPlus (BalanceData (mixed [usd 1]) (mixed [usd 2]) 5) (BalanceData (mixed [usd 3]) (mixed [usd 4]) 0)
@?= BalanceData (mixed [usd 4]) (mixed [usd 6]) 5,
testCase "opBalanceData maMinus" $ do
opBalanceData maMinus (BalanceData (mixed [usd 1]) (mixed [usd 2]) 5) (BalanceData (mixed [usd 3]) (mixed [usd 4]) 0)
@?= BalanceData (mixed [usd (-2)]) (mixed [usd (-2)]) 5
]
|