Skip to content

Commit

Permalink
fix: failing balance assertions with cost show correct highlight #2083
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Sep 11, 2023
1 parent 4a2da0b commit 1ef48fd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
3 changes: 2 additions & 1 deletion hledger-lib/Hledger/Data/Balancing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,8 @@ journalBalanceTransactions bopts' j' =
-- This stores the balanced transactions in case 2 but not in case 1.
balanceTransactionAndCheckAssertionsB :: Either Posting Transaction -> Balancing s ()
balanceTransactionAndCheckAssertionsB (Left p@Posting{}) =
-- update the account's running balance and check the balance assertion if any
-- Update the account's running balance and check the balance assertion if any.
-- Note, cost is ignored when checking balance assertions, currently.
void . addAmountAndCheckAssertionB $ postingStripPrices p
balanceTransactionAndCheckAssertionsB (Right t@Transaction{tpostings=ps}) = do
-- make sure we can handle the balance assignments
Expand Down
5 changes: 4 additions & 1 deletion hledger-lib/Hledger/Data/Errors.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Data.Text (Text)
import qualified Data.Text as T

import Hledger.Data.Transaction (showTransaction)
import Hledger.Data.Posting (postingStripPrices)
import Hledger.Data.Types
import Hledger.Utils
import Data.Maybe
Expand Down Expand Up @@ -118,7 +119,9 @@ makePostingErrorExcerpt p findpostingerrorcolumns =
Just t -> (f, errabsline, merrcols, ex)
where
(SourcePos f tl _) = fst $ tsourcepos t
mpindex = transactionFindPostingIndex (==p) t
-- p had cost removed in balanceTransactionAndCheckAssertionsB,
-- must remove them from t's postings too (#2083)
mpindex = transactionFindPostingIndex ((==p).postingStripPrices) t
errrelline = case mpindex of
Nothing -> 0
Just pindex ->
Expand Down
42 changes: 28 additions & 14 deletions hledger/test/journal/balance-assertions.test
Original file line number Diff line number Diff line change
Expand Up @@ -336,19 +336,33 @@ $ hledger -f - stats
> /Transactions/
>=0

# 19. Balance assertions may have a price, but it's ignored
# 19. Cost is ignored when checking balance assertions.
<
2019/01/01
(a) 1A @ 1B = 1A @ 2B
2023-01-01
(a) 1A @ 1B = 1A

$ hledger -f- print
>
2019-01-01
$ hledger -f- check

# 20. The asserted balance may have a cost, also ignored
<
2023-01-01
(a) 1A @ 1B = 1A @ 2B

>=0
$ hledger -f- check

# 21. A cost does not disrupt highlighting in the balance assertion error message.
<
2023-01-01
(a) 1A @ 1B = 5A

$ hledger -f- check
>2 /
2 \| \(a\) 1A @ 1B = 5A
\| \^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^
/
>=1

# 20. Balance assignments may have a price, and it's used for the posting amount.
# 22. Balance assignments may have a cost, and it's used for the posting amount.
<
2019/01/01
(a) = 1A @ 2B
Expand All @@ -360,7 +374,7 @@ $ hledger -f- print --explicit

>=0

# 21. The exact amounts are compared; display precision does not affect assertions.
# 23. The exact amounts are compared; display precision does not affect assertions.
<
commodity $1000.00

Expand All @@ -374,7 +388,7 @@ $ hledger -f- print
> /2019/
>=0

# 22. This fails
# 24. This fails
<
commodity $1000.00

Expand All @@ -388,7 +402,7 @@ $ hledger -f- print
>2 /a difference of.*0\.004/
>=1

# 23. This fails
# 25. This fails
<
commodity $1000.00

Expand All @@ -402,7 +416,7 @@ $ hledger -f- print
>2 /a difference of.*0\.0001/
>=1

# 24. Inclusive assertions include balances from subaccounts.
# 26. Inclusive assertions include balances from subaccounts.
<
2019/1/1
(a) X1
Expand All @@ -426,7 +440,7 @@ $ hledger -f- print

>=0

# 25. Inclusive balance assignments also work (#1207).
# 27. Inclusive balance assignments also work (#1207).
<
2020-01-25
(a:aa) 1
Expand Down Expand Up @@ -456,7 +470,7 @@ $ hledger -f- print -x

>=0

# 26. When balance assignment with a cost generates two postings in one commodity,
# 28. When balance assignment with a cost generates two postings in one commodity,
# the balance assertion appears only on the last, not both. (#1965)
<
2022-01-01
Expand Down

0 comments on commit 1ef48fd

Please sign in to comment.