Skip to content

Commit

Permalink
ledger-post-fill: Throw when balance but missing amount
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienCassou committed May 4, 2024
1 parent 3036d9b commit 0c9c847
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 4 additions & 3 deletions ledger-post.el
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,12 @@ might want to insert it anyway."
(pcase-let* ((`(,begin ,end) (ledger-navigate-find-xact-extents (point)))
(`(,total . ,missing-count) (ledger-post-xact-total))
(missing-amount (ledger-negate-commodity total))
(missing-non-zero (> (abs (car missing-amount)) 0.0001)))
(amounts-balance (< (abs (car missing-amount)) 0.0001)))
(pcase missing-count
(0 (when missing-non-zero
(0 (unless amounts-balance
(user-error "Postings do not balance, but no posting to fill")))
(1 (when missing-non-zero
(1 (if amounts-balance
(user-error "Missing amount but amounts balance already")
(goto-char begin)
(cl-loop
while (re-search-forward ledger-post-line-regexp end t)
Expand Down
12 changes: 12 additions & 0 deletions test/post-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,18 @@ http://bugs.ledger-cli.org/show_bug.cgi?id=946"
(cadr (should-error (ledger-post-fill)))
"Postings do not balance, but no posting to fill")))

;; missing amount but amounts balance already
(ledger-tests-with-temp-file
"\
2013-05-01 foo
Expenses:Foo $-10
Expenses:Baz $5
Expenses:Bar $5
Expenses:Bla
"
(should (string-equal
(cadr (should-error (ledger-post-fill)))
"Missing amount but amounts balance already"))))

(provide 'post-test)

Expand Down

0 comments on commit 0c9c847

Please sign in to comment.