# * balance assertions and balance assignments

# ** 1. test some balance assertions
<
2013/1/1
  a    $1  =$1
  b        =-$1

2013/1/2
  a    $1  =$2
  b   $-1  =$-2

2013/1/3
  a    $1  = $3
  b   $-1  = $-3
  (b)  $1  = $-2

$ hledger -f - stats
> /Transactions/
>=0

# ** 2. same entries as 1 but different parse order, assertion should still pass based on date
<
2013/1/1
  a    $1  =$1
  b        =-$1

2013/1/3
  a    $1  = $3
  b   $-1  = $-3
  (b)  $1  = $-2

2013/1/2
  a    $1  =$2
  b   $-1  =$-2

$ hledger -f - stats
> /Transactions/
>=0

# ** 3. like 1 but switch order of postings in last entry,
# assertion should fail and exit code should be non zero
<
2013/1/1
  a    $1  =$1
  b        =-$1

2013/1/2
  a    $1  =$2
  b   $-1  =$-2

2013/1/3
  a    $1  = $3
  (b)  $1  = $-2
  b   $-1  = $-3

$ hledger -f - stats
>2 /Error: -:11:12/
>=1

# ** 4. should also work without commodity symbols
<
2013/1/1
  (a)    1  =1

$ hledger -f - stats
> /Transactions/
>=0

# ** 5. should work for fractional amount with trailing zeros
<
2013/1/1
  a    $1.20  =$1.20
  b           =-$1.20

2013/1/2
  a    $0.10  =$1.3
  b           =-$1.3

2013/1/3
  a    $0.7   =$2
  b           =-$2

$ hledger -f - stats
> /Transactions/
>=0

# ** 6. assertions currently check only a single commodity's balance, like Ledger
<
1/2
  (a)  A1
  (a)  B1 = A1
  (a)  0  = A1
  (a)  C0 = D0

$ hledger -f - stats
> /Transactions/
>=0

# ** 7. balances should accumulate (#195)
<
1/1
    (a)   1F = 1F
    (a)   2G = 2G

1/2
    (a)   3F = 4F

$ hledger -f - stats
> !/assertion failed/
>=0

# ** 8. what should happen here ? Currently,
# in a, 3.4 EUR @@ $5.6 and -3.4 EUR cancel out (wrong ?)
# in b, 
# 
# <
# 2013/1/1
#   a    $1.20
#   a    3.4 EUR @@ $5.6
#   b  

# 2013/1/2
#   a    -3.4 EUR
#   b

# 2013/1/3
#   a    $0.1   =$1.30
#   b           =-$1.30

# $ hledger -f - stats
# > /Transactions/
# >=0

# ** 8. Using balance assignment to set balances.
<
2013/1/1
  a    $1.20
  b

2013/1/2
  a           =$1.3
  b

2013/1/2
  a           $10 =$11.3
  b         =$-11.3

$ hledger -f - stats
> /Transactions/
>=0


# ** 9. Multiple assertions for an account in the same transaction.
<
2013/1/1
  a    $1  =$1
  b        =-$1

2013/1/2
  a    $1  =$2
  b   $-1  =$-2

2013/1/3
  a    $2  = $4
  b   $-1  = $-3
  b   $-1  = $-4

$ hledger -f - stats
> /Transactions/
>=0

# ** 10. Multiple assertions and assignments for an account in the same transaction.
<
2013/1/1
  a    $1  =$1
  b        =-$1

2013/1/3
  a    $6  = $7
  b   $-1  = $-2
  b   $-1  = $-3
  b   $-7    = $-10
  b   $-1
  b   $-1  = $-12
  b

2013/1/4
  a    $0 = $7
  b   $0   = $-7

2013/1/5
  (c)    100 A

2013/1/5
  c      = 50 B
  c      = 50 A

$ hledger -f - stats
> /Transactions/
>=0

# ** 11. Assignments and virtual postings
<
2013/1/1
  b
  [a]    1$
  (b)     = $14
  [b]
  a      4$


2013/1/2
  [a]      = $5
  b        = $9

$ hledger -f - stats
> /Transactions/
>=0
# ** 12. Having both assignments and posting dates is not supported.
<
2013/1/1
  a    $1  =$1
  b         =$-1  ; date:2012/1/1

$ hledger -f - stats
>2  /Balance assignments and custom posting dates may not be combined/
>=1

# ** 13. Posting Date
<

2011/5/5
  [a]        = -10

2013/1/1
  a    1 = -12
  b
  c   = 5

2014/1/1
  a      ; date:2012/1/1
  d     3 = 3

2015/1/1
  [a]      ; date:2011/1/1
  [d]    10


$ hledger -f - stats
> /Transactions/
>=0

# ** 14. Mix different commodities
<
2016/1/1
    a      $1
    b      -1 zorkmids

2016/1/2
    a     $-1 = $0
    b

$ hledger -f - stats
> /Transactions/
>=0

# ** 15. Mix different commodities and assignments
<
2016/1/1
    a      $1
    b      -1 zorkmids

2016/1/4
    [a]    = $1


2016/1/5
    [a]    = -1 zorkmids

2016/1/2
    a
    b      = 0 zorkmids

$ hledger -f - stats
> /Transactions/
>=0

# ** 16. Total assertions (==) parse correctly
<
2016/1/1
    a      $1
    b

2016/1/2
    a         == $1

$ hledger -f - stats
> /Transactions/
>=0

# ** 17. Total assertions consider entire multicommodity amount
<
2016/1/1
    a      $1
    b

2016/1/2
    a       1 zorkmids
    b

2016/1/3
    a       0 == $1

$ hledger -f - stats
>2 /Error: -:10:15:/
>=1

# ** 18. Mix different commodities and total assignments
<
2016/1/1
    a      $1
    a      -1 zorkmids
    b

2016/1/2
    a         == $1
    b      -1 zorkmids

2016/1/3
    b       0 = $-1
    b       0 = 0 zorkmids

$ hledger -f - stats
> /Transactions/
>=0

# ** 19. Cost is ignored when checking balance assertions.
<
2023-01-01
    (a)         1A @ 1B = 1A

$ hledger -f- check

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

$ 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

# ** 22. Balance assignments may have a cost, and it's used for the posting amount.
<
2019/01/01
    (a)                 = 1A @ 2B

$ hledger -f- print --explicit
>
2019-01-01
    (a)         1A @ 2B = 1A @ 2B

>=0

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

2019/01/01
    (a)             $0.006

2019/01/02
    (a)             $1.00  = $1.006

$ hledger -f- print
> /2019/
>=0

# ** 24. This fails
<
commodity $1000.00

2019/01/01
    (a)             $0.006

2019/01/02
    (a)             $1.00  = $1.01

$ hledger -f- print
>2 /Balance assertion failed/
>=1

# ** 25. This fails
<
commodity $1000.00

2019/01/01
    (a)             $0.006

2019/01/02
    (a)             $1.00  = $1.0061

$ hledger -f- print
>2 /Balance assertion failed/
>=1

# ** 26. Inclusive assertions include balances from subaccounts.
<
2019/1/1
  (a)    X1
  (a)    Y3
  (a:b)  Y7
  (a)    0 =*  X1
  (a)    0 =*  Y10
  (a:b)  0 =*  Y7
  (a:b)  0 ==*  Y7

$ hledger -f- print
>
2019-01-01
    (a)                X1
    (a)                Y3
    (a:b)              Y7
    (a)                 0 =* X1
    (a)                 0 =* Y10
    (a:b)               0 =* Y7
    (a:b)               0 ==* Y7

>=0

# ** 27. Inclusive balance assignments also work (#1207).
<
2020-01-25
    (a:aa)   1

2020-01-25
    (a:bb)   1

2020-01-25
    (a)      1

2020-01-31
    (a)        ==* 1

$ hledger -f- print -x
>
2020-01-25
    (a:aa)               1

2020-01-25
    (a:bb)               1

2020-01-25
    (a)               1

2020-01-31
    (a)              -2 ==* 1

>=0

# ** 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
	assets:eur  €10
	equity

2022-01-02
	assets:eur      = €1 @ $1
	assets:usd

$ hledger -f- print -x date:2022-01-02
2022-01-02
    assets:eur            €-10
    assets:eur         €1 @ $1 = €1 @ $1
    assets:usd             $-1
    assets:usd             €10

>=

# ** 29. -10 A should be inferred for a.
# And the 0 B balaance assertion should ignore costs and succeed,
# even though the balance is 1 B @@ 10 A - 1 B. (#2150)
<
2024-01-01
    a            10 A
    e

2024-01-02
    b     1 B @@ 10 A
    a                 = 0 A

2024-01-03
    b            -1 B = 0 B
    e

$ hledger -f - check
