1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
4 module Write.Test where
6 import Control.Applicative (Applicative(..), (<*))
9 import Data.Decimal (DecimalRaw(..))
10 import Data.Either (either, rights)
11 import Data.Function (($), (.), const, id)
12 import Data.Functor ((<$>))
13 import Data.Maybe (Maybe(..))
14 import Data.Monoid (Monoid(..))
15 import Data.Text (Text)
16 import qualified Data.Text as Text
17 import qualified Data.Text.Lazy as TL
19 import Test.Tasty.HUnit
20 import qualified Text.Parsec as R hiding
33 import qualified Text.Parsec.Combinator.CorrectSourcePosWithTab as R
34 import qualified Text.Parsec.Error.Custom as R
35 -- import Text.Show (Show(..))
37 import qualified Hcompta as H
38 import qualified Hcompta.JCC as JCC
39 import qualified Hcompta.JCC.Lib.Parsec as R
42 tests = testGroup "Write"
43 [ testGroup "write_date" $
44 let (==>) (txt::Text) e =
45 testCase (Text.unpack txt) $
49 { JCC.write_style_color = False
50 , JCC.write_style_align = True } .
52 rights [R.runParserWithError
53 (JCC.read_date id Nothing <* R.eof) () "" txt])
55 [ testCase "date_epoch" $
58 { JCC.write_style_color = False
59 , JCC.write_style_align = True }
60 (JCC.write_date H.date_epoch)
62 , "2000-01-01" ==> "2000-01-01"
63 , "2000-01-01_12:34:51_CET" ==> "2000-01-01_11:34:51"
64 , "2000-01-01_12:34:51+01:10" ==> "2000-01-01_11:24:51"
65 , "2000-01-01_12:34:51-01:10" ==> "2000-01-01_13:44:51"
66 , "2000-01-01_01:02:03" ==> "2000-01-01_01:02:03"
67 , "2000-01-01_01:02" ==> "2000-01-01_01:02"
68 , "2000-01-01_01:00" ==> "2000-01-01_01:00"
70 , testGroup "write_amount" $
71 let (<==) (txt::Text) e =
72 testCase (Text.unpack txt) $
76 { JCC.write_style_color = False
77 , JCC.write_style_align = True } $
79 (TL.fromStrict txt) in
85 , JCC.amount { JCC.amount_quantity = Decimal 2 0 } )
88 , JCC.amount { JCC.amount_quantity = Decimal 0 123 } )
91 , JCC.amount { JCC.amount_quantity = Decimal 0 (- 123) } )
93 ( mempty { JCC.amount_style_fractioning = Just '.' }
94 , JCC.amount { JCC.amount_quantity = Decimal 1 123 } )
97 { JCC.amount_style_fractioning = Just '.'
98 , JCC.amount_style_grouping_integral = Just $ JCC.Amount_Style_Grouping ',' [3]
100 , JCC.amount { JCC.amount_quantity = Decimal 2 123456 })
101 , "123,456,789,01,2.3456789" <==
103 { JCC.amount_style_fractioning = Just '.'
104 , JCC.amount_style_grouping_integral = Just $ JCC.Amount_Style_Grouping ',' [1, 2, 3]
106 , JCC.amount { JCC.amount_quantity = Decimal 7 1234567890123456789 } )
107 , "1234567.8_90_123_456_789" <==
109 { JCC.amount_style_fractioning = Just '.'
110 , JCC.amount_style_grouping_fractional = Just $ JCC.Amount_Style_Grouping '_' [1, 2, 3]
112 , JCC.amount { JCC.amount_quantity = Decimal 12 1234567890123456789 })
113 , "1,2,3,4,5,6,7,89,012.3456789" <==
115 { JCC.amount_style_fractioning = Just '.'
116 , JCC.amount_style_grouping_integral = Just $ JCC.Amount_Style_Grouping ',' [3, 2, 1]
118 , JCC.amount { JCC.amount_quantity = Decimal 7 1234567890123456789 })
119 , "1234567.890_12_3_4_5_6_7_8_9" <==
121 { JCC.amount_style_fractioning = Just '.'
122 , JCC.amount_style_grouping_fractional = Just $ JCC.Amount_Style_Grouping '_' [3, 2, 1]
124 , JCC.amount { JCC.amount_quantity = Decimal 12 1234567890123456789 })
126 , testGroup "write_amount_length" $
127 let (==>) (txt::Text) =
128 testCase (Text.unpack txt) $
130 (JCC.write_amount_length <$>
131 rights [R.runParser (JCC.read_amount <* R.eof) () "" txt])
141 , "123,456,789,01,2.3456789"
142 , "1234567.8_90_123_456_789"
143 , "1,2,3,4,5,6,7,89,012.3456789"
144 , "1234567.890_12_3_4_5_6_7_8_9"
145 , "1000000.000_00_0_0_0_0_0_0_0"
156 , testGroup "write_account" $
158 testCase (Text.unpack txt) $
160 (let read (t::Text) =
162 (JCC.read_account <* R.eof)
166 { JCC.write_style_color = False
167 , JCC.write_style_align = True } <$>
168 (JCC.write_account <$> read txt)
170 [TL.fromStrict txt] in
174 , testGroup "write_transaction" $
175 let (==>) (txt::Text) =
176 testCase (Text.unpack txt) .
178 let write (txn, ctx) =
181 { JCC.write_style_color = False
182 , JCC.write_style_align = True } $
183 let jnl = JCC.read_context_journal ctx in
184 let sty = JCC.journal_amount_styles jnl in
185 JCC.write_transaction sty txn in
188 -- (pure . TL.pack . show)
191 (R.and_state (JCC.read_transaction <* R.newline <* R.eof))
192 ( JCC.read_context JCC.charted JCC.journal
193 ::JCC.Read_Context JCC.Transaction [JCC.Transaction] )
196 [ "2000-01-01 some wording"
200 [ "2000-01-01 some wording"
204 [ "2000-01-01 some wording"
208 , " ; second comment"
211 [ "2000-01-01 some wording"
215 , " ; second comment"
216 , " ; third comment" ]]
218 [ "2000-01-01 some wording"
225 { JCC.write_style_color = False
226 , JCC.write_style_align = True }
227 (JCC.write_transaction