1 module Write.Test where
4 import Test.Tasty.HUnit
8 import Data.Decimal (DecimalRaw(..))
9 import Data.Either (Either(..))
10 import Data.Function (($), (.))
11 import Data.Functor ((<$>))
12 import Data.Monoid (Monoid(..))
13 import Data.Text (Text)
14 import qualified Data.Text as Text
15 import qualified Data.Text.Lazy as TL
16 import Data.Tuple (uncurry)
17 import qualified Data.Strict as S
19 import qualified Hcompta as H
20 import qualified Hcompta.LCC as LCC
22 import Read.Test (test, read_gram)
25 tests = testGroup "Write"
27 let (==>) (inp::Text) exp =
31 { LCC.style_write_color = False
32 , LCC.style_write_align = True } .
33 LCC.write_date <$>) <$>
34 read_gram LCC.g_date inp
35 @?= Right (S.Right exp) in
39 { LCC.style_write_color = False
40 , LCC.style_write_align = True }
41 (LCC.write_date H.date_epoch)
43 , "2000-01-01" ==> "2000-01-01"
44 , "2000-01-01_12:34:51_CET" ==> "2000-01-01_11:34:51"
45 , "2000-01-01_12:34:51+01:10" ==> "2000-01-01_11:24:51"
46 , "2000-01-01_12:34:51-01:10" ==> "2000-01-01_13:44:51"
47 , "2000-01-01_01:02:03" ==> "2000-01-01_01:02:03"
48 , "2000-01-01_01:02" ==> "2000-01-01_01:02"
49 , "2000-01-01_01:00" ==> "2000-01-01_01:00"
51 , testGroup "Amount" $
52 let (<==) (inp::Text) exp =
56 { LCC.style_write_color = False
57 , LCC.style_write_align = True }
58 (LCC.write_amount exp)
59 @?= TL.fromStrict inp in
65 , LCC.amount { LCC.amount_quantity = Decimal 2 0 } )
68 , LCC.amount { LCC.amount_quantity = Decimal 0 123 } )
71 , LCC.amount { LCC.amount_quantity = Decimal 0 (- 123) } )
73 ( mempty { LCC.style_amount_fractioning = S.Just '.' }
74 , LCC.amount { LCC.amount_quantity = Decimal 1 123 } )
77 { LCC.style_amount_fractioning = S.Just '.'
78 , LCC.style_amount_grouping_integral = S.Just $ LCC.Style_Amount_Grouping ',' [3]
80 , LCC.amount { LCC.amount_quantity = Decimal 2 123456 })
81 , "123,456,789,01,2.3456789" <==
83 { LCC.style_amount_fractioning = S.Just '.'
84 , LCC.style_amount_grouping_integral = S.Just $ LCC.Style_Amount_Grouping ',' [1, 2, 3]
86 , LCC.amount { LCC.amount_quantity = Decimal 7 1234567890123456789 } )
87 , "1234567.8_90_123_456_789" <==
89 { LCC.style_amount_fractioning = S.Just '.'
90 , LCC.style_amount_grouping_fractional = S.Just $ LCC.Style_Amount_Grouping '_' [1, 2, 3]
92 , LCC.amount { LCC.amount_quantity = Decimal 12 1234567890123456789 })
93 , "1,2,3,4,5,6,7,89,012.3456789" <==
95 { LCC.style_amount_fractioning = S.Just '.'
96 , LCC.style_amount_grouping_integral = S.Just $ LCC.Style_Amount_Grouping ',' [3, 2, 1]
98 , LCC.amount { LCC.amount_quantity = Decimal 7 1234567890123456789 })
99 , "1234567.890_12_3_4_5_6_7_8_9" <==
101 { LCC.style_amount_fractioning = S.Just '.'
102 , LCC.style_amount_grouping_fractional = S.Just $ LCC.Style_Amount_Grouping '_' [3, 2, 1]
104 , LCC.amount { LCC.amount_quantity = Decimal 12 1234567890123456789 })
105 , testGroup "Width" $
106 let (==>) (inp::Text) =
108 LCC.width_amount <$> read_gram LCC.g_amount inp
109 @?= Right (Text.length inp) in
118 , "123,456,789,01,2.3456789"
119 , "1234567.8_90_123_456_789"
120 , "1,2,3,4,5,6,7,89,012.3456789"
121 , "1234567.890_12_3_4_5_6_7_8_9"
122 , "1000000.000_00_0_0_0_0_0_0_0"
133 , testGroup "Account" $
138 { LCC.style_write_color = False
139 , LCC.style_write_align = True } .
140 LCC.write_account <$>
141 read_gram LCC.g_account inp
142 @?= Right (TL.fromStrict inp) in
146 , testGroup "Transaction" $
148 let inp = Text.intercalate "\n" i in
149 let exp = Text.intercalate "\n" e in
153 { LCC.style_write_color = False
154 , LCC.style_write_align = True } .
155 (uncurry LCC.write_transaction) <$>) <$>
156 read_gram (LCC.g_get $
157 (\txn context_write_amounts ->
159 { LCC.context_write_account_ref = True
160 , LCC.context_write_max_posting_width = 0
161 , LCC.context_write_amounts
163 ) <$> LCC.g_transaction
165 @?= Right (S.Right (TL.fromStrict exp))
167 [ [ "2000-01-01 some wording"
171 [ "2000-01-01 some wording"
175 , [ "2000-01-01 some wording"
179 , " ; second comment"
182 [ "2000-01-01 some wording"
186 , " ; second comment"
192 { LCC.style_write_color = False
193 , LCC.style_write_align = True }
194 (LCC.write_transaction