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.LCC as LCC
 
  39 import qualified Hcompta.LCC.Lib.Parsec as R
 
  41 import Read.Test (test)
 
  44 tests = testGroup "Write"
 
  45  [ testGroup "write_date" $
 
  46         let (==>) (txt::Text) e =
 
  47                 test (Text.unpack txt) $
 
  51                          { LCC.write_style_color = False
 
  52                          , LCC.write_style_align = True } .
 
  54                         rights [R.runParserWithError
 
  55                          (LCC.read_date id Nothing <* R.eof) () "" txt])
 
  60                          { LCC.write_style_color = False
 
  61                          , LCC.write_style_align = True }
 
  62                  (LCC.write_date H.date_epoch)
 
  64          , "2000-01-01"                ==> "2000-01-01"
 
  65          , "2000-01-01_12:34:51_CET"   ==> "2000-01-01_11:34:51"
 
  66          , "2000-01-01_12:34:51+01:10" ==> "2000-01-01_11:24:51"
 
  67          , "2000-01-01_12:34:51-01:10" ==> "2000-01-01_13:44:51"
 
  68          , "2000-01-01_01:02:03"       ==> "2000-01-01_01:02:03"
 
  69          , "2000-01-01_01:02"          ==> "2000-01-01_01:02"
 
  70          , "2000-01-01_01:00"          ==> "2000-01-01_01:00"
 
  72  , testGroup "write_amount" $
 
  73         let (<==) (txt::Text) e =
 
  74                 test (Text.unpack txt) $
 
  78                          { LCC.write_style_color = False
 
  79                          , LCC.write_style_align = True } $
 
  81                  (TL.fromStrict txt) in
 
  87                 , LCC.amount { LCC.amount_quantity = Decimal 2 0 } )
 
  90                 , LCC.amount { LCC.amount_quantity = Decimal 0 123 } )
 
  93                 , LCC.amount { LCC.amount_quantity = Decimal 0 (- 123) } )
 
  95                 ( mempty { LCC.amount_style_fractioning = Just '.' }
 
  96                 , LCC.amount { LCC.amount_quantity = Decimal 1 123 } )
 
  99                          { LCC.amount_style_fractioning       = Just '.'
 
 100                          , LCC.amount_style_grouping_integral = Just $ LCC.Amount_Style_Grouping ',' [3]
 
 102                 , LCC.amount { LCC.amount_quantity = Decimal 2 123456 })
 
 103          , "123,456,789,01,2.3456789" <==
 
 105                          { LCC.amount_style_fractioning       = Just '.'
 
 106                          , LCC.amount_style_grouping_integral = Just $ LCC.Amount_Style_Grouping ',' [1, 2, 3]
 
 108                 , LCC.amount { LCC.amount_quantity = Decimal 7 1234567890123456789 } )
 
 109          , "1234567.8_90_123_456_789" <==
 
 111                          { LCC.amount_style_fractioning         = Just '.'
 
 112                          , LCC.amount_style_grouping_fractional = Just $ LCC.Amount_Style_Grouping '_' [1, 2, 3]
 
 114                 , LCC.amount { LCC.amount_quantity = Decimal 12 1234567890123456789 })
 
 115          , "1,2,3,4,5,6,7,89,012.3456789" <==
 
 117                          { LCC.amount_style_fractioning       = Just '.'
 
 118                          , LCC.amount_style_grouping_integral = Just $ LCC.Amount_Style_Grouping ',' [3, 2, 1]
 
 120                 , LCC.amount { LCC.amount_quantity = Decimal 7 1234567890123456789 })
 
 121          , "1234567.890_12_3_4_5_6_7_8_9" <==
 
 123                          { LCC.amount_style_fractioning         = Just '.'
 
 124                          , LCC.amount_style_grouping_fractional = Just $ LCC.Amount_Style_Grouping '_' [3, 2, 1]
 
 126                 , LCC.amount { LCC.amount_quantity = Decimal 12 1234567890123456789 })
 
 128  , testGroup "write_amount_length" $
 
 129         let (==>) (txt::Text) =
 
 130                 test (Text.unpack txt) $
 
 132                  (LCC.write_amount_length <$>
 
 133                         rights [R.runParser (LCC.read_amount <* R.eof) () "" txt])
 
 143          , "123,456,789,01,2.3456789"
 
 144          , "1234567.8_90_123_456_789"
 
 145          , "1,2,3,4,5,6,7,89,012.3456789"
 
 146          , "1234567.890_12_3_4_5_6_7_8_9"
 
 147          , "1000000.000_00_0_0_0_0_0_0_0"
 
 158  , testGroup "write_account" $
 
 160                 test (Text.unpack txt) $
 
 162                  (let read (t::Text) =
 
 164                                  (LCC.read_account <* R.eof)
 
 168                                  { LCC.write_style_color = False
 
 169                                  , LCC.write_style_align = True } <$>
 
 170                          (LCC.write_account <$> read txt)
 
 172                  [TL.fromStrict txt] in
 
 176  , testGroup "write_transaction" $
 
 177         let (==>) (lines::[Text]) =
 
 178                 let txt = Text.unlines lines in
 
 179                 test (Text.unpack txt) .
 
 181                         let write (txn, ctx) =
 
 184                                          { LCC.write_style_color = False
 
 185                                          , LCC.write_style_align = True } $
 
 186                                         let jnl = LCC.context_read_journal ctx in
 
 187                                         let sty = LCC.journal_amount_styles jnl in
 
 188                                         LCC.write_transaction sty txn in
 
 191                          -- (pure . TL.pack . show)
 
 194                          (R.and_state (LCC.read_transaction <* R.newline <* R.eof))
 
 195                          ( LCC.context_read LCC.charted LCC.journal
 
 196                          ::LCC.Context_Read LCC.Transaction [LCC.Transaction] )
 
 199          [ [ "2000-01-01 some wording"
 
 203                  [ "2000-01-01 some wording"
 
 206          , [ "2000-01-01 some wording"
 
 210                  , "   ; second comment"
 
 213                  [ "2000-01-01 some wording"
 
 217                  , "   ; second comment"
 
 218                  , "   ; third comment" ]]
 
 219          , [ "2000-01-01 some wording"
 
 226                          { LCC.write_style_color = False
 
 227                          , LCC.write_style_align = True }
 
 228                  (LCC.write_transaction