1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
4 module Write.Test where
6 import Control.Applicative (Applicative(..), (<*))
7 import Control.Monad (Monad(..))
10 import Data.Decimal (DecimalRaw(..))
11 import Data.Either (either, rights)
12 import Data.Function (($), (.), const, id)
13 import Data.Functor ((<$>))
14 import Data.Maybe (Maybe(..))
15 import Data.Monoid (Monoid(..))
16 import Data.Text (Text)
17 import qualified Data.Text as Text
18 import qualified Data.Text.Lazy as TL
20 import Test.Tasty.HUnit
21 import qualified Text.Parsec as R hiding
34 import qualified Text.Parsec.Combinator.CorrectSourcePosWithTab as R
35 import qualified Text.Parsec.Error.Custom as R
36 -- import Text.Show (Show(..))
38 import qualified Hcompta as H
39 import qualified Hcompta.Ledger as Ledger
40 import qualified Hcompta.Ledger.Lib.Parsec as R
43 tests = testGroup "Write"
44 [ testGroup "write_date" $
45 let (==>) (txt::Text) e =
46 testCase (Text.unpack txt) $
50 { Ledger.write_style_color = False
51 , Ledger.write_style_align = True } .
53 rights [R.runParserWithError
54 (Ledger.read_date id Nothing <* R.eof) () "" txt])
56 [ testCase "date_epoch" $
59 { Ledger.write_style_color = False
60 , Ledger.write_style_align = True }
61 (Ledger.write_date H.date_epoch)
63 , "2000-01-01" ==> "2000-01-01"
64 , "2000-01-01_12:34:51_CET" ==> "2000-01-01_11:34:51"
65 , "2000-01-01_12:34:51+01:10" ==> "2000-01-01_11:24:51"
66 , "2000-01-01_12:34:51-01:10" ==> "2000-01-01_13:44:51"
67 , "2000-01-01_01:02:03" ==> "2000-01-01_01:02:03"
68 , "2000-01-01_01:02" ==> "2000-01-01_01:02"
69 , "2000-01-01_01:00" ==> "2000-01-01_01:00"
71 , testGroup "write_amount" $
72 let (<==) (txt::Text) e =
73 testCase (Text.unpack txt) $
77 { Ledger.write_style_color = False
78 , Ledger.write_style_align = True } $
79 Ledger.write_amount e)
80 (TL.fromStrict txt) in
86 , Ledger.amount { Ledger.amount_quantity = Decimal 2 0 } )
89 , Ledger.amount { Ledger.amount_quantity = Decimal 0 123 } )
92 , Ledger.amount { Ledger.amount_quantity = Decimal 0 (- 123) } )
94 ( mempty { Ledger.amount_style_fractioning = Just '.' }
95 , Ledger.amount { Ledger.amount_quantity = Decimal 1 123 } )
98 { Ledger.amount_style_fractioning = Just '.'
99 , Ledger.amount_style_grouping_integral = Just $ Ledger.Amount_Style_Grouping ',' [3]
101 , Ledger.amount { Ledger.amount_quantity = Decimal 2 123456 })
102 , "123,456,789,01,2.3456789" <==
104 { Ledger.amount_style_fractioning = Just '.'
105 , Ledger.amount_style_grouping_integral = Just $ Ledger.Amount_Style_Grouping ',' [1, 2, 3]
107 , Ledger.amount { Ledger.amount_quantity = Decimal 7 1234567890123456789 } )
108 , "1234567.8_90_123_456_789" <==
110 { Ledger.amount_style_fractioning = Just '.'
111 , Ledger.amount_style_grouping_fractional = Just $ Ledger.Amount_Style_Grouping '_' [1, 2, 3]
113 , Ledger.amount { Ledger.amount_quantity = Decimal 12 1234567890123456789 })
114 , "1,2,3,4,5,6,7,89,012.3456789" <==
116 { Ledger.amount_style_fractioning = Just '.'
117 , Ledger.amount_style_grouping_integral = Just $ Ledger.Amount_Style_Grouping ',' [3, 2, 1]
119 , Ledger.amount { Ledger.amount_quantity = Decimal 7 1234567890123456789 })
120 , "1234567.890_12_3_4_5_6_7_8_9" <==
122 { Ledger.amount_style_fractioning = Just '.'
123 , Ledger.amount_style_grouping_fractional = Just $ Ledger.Amount_Style_Grouping '_' [3, 2, 1]
125 , Ledger.amount { Ledger.amount_quantity = Decimal 12 1234567890123456789 })
127 , testGroup "write_amount_length" $
128 let (==>) (txt::Text) =
129 testCase (Text.unpack txt) $
131 (Ledger.write_amount_length <$>
132 rights [R.runParser (Ledger.read_amount <* R.eof) () "" txt])
142 , "123,456,789,01,2.3456789"
143 , "1234567.8_90_123_456_789"
144 , "1,2,3,4,5,6,7,89,012.3456789"
145 , "1234567.890_12_3_4_5_6_7_8_9"
146 , "1000000.000_00_0_0_0_0_0_0_0"
157 , testGroup "write_account" $
159 testCase (Text.unpack txt) $
161 (let read (t::Text) =
163 (Ledger.read_account <* R.eof)
167 { Ledger.write_style_color = False
168 , Ledger.write_style_align = True } <$>
170 let Ledger.Posting_Typed ty ac = Ledger.read_posting_type a in
171 return $ Ledger.write_account ty ac)
173 [TL.fromStrict txt] in
179 , testGroup "write_transaction" $
180 let (==>) (txt::Text) =
181 testCase (Text.unpack txt) .
183 let write (txn, ctx) =
186 { Ledger.write_style_color = False
187 , Ledger.write_style_align = True } $
188 let jnl = Ledger.context_read_journal ctx in
189 let sty = Ledger.journal_amount_styles jnl in
190 Ledger.write_transaction sty txn in
192 (const []) {-(pure . TL.pack . show)-}
195 (R.and_state (Ledger.read_transaction <* R.newline <* R.eof))
196 ( Ledger.context_read Ledger.charted Ledger.journal
197 ::Ledger.Context_Read Ledger.Transaction [Ledger.Transaction] )
200 [ "2000-01-01 some wording"
204 [ "2000-01-01 some wording"
209 [ "2000-01-01 some wording"
213 , " ; second comment"
216 [ "2000-01-01 some wording"
220 , " ; second comment"
224 [ "2000-01-01 some wording"
231 { Ledger.write_style_color = False
232 , Ledger.write_style_align = True }
233 (Ledger.write_transaction