module Write.Test where import Test.Tasty import Test.Tasty.HUnit import Data.Bool import Data.Data () import Data.Decimal (DecimalRaw(..)) import Data.Either (Either(..)) import Data.Function (($), (.)) import Data.Functor ((<$>)) import Data.Monoid (Monoid(..)) import Data.Text (Text) import qualified Data.Text as Text import qualified Data.Text.Lazy as TL import Data.Tuple (uncurry) import qualified Data.Strict as S import qualified Hcompta as H import qualified Hcompta.LCC as LCC import Read.Test (test, read_gram) tests :: TestTree tests = testGroup "Write" [ testGroup "Date" $ let (==>) (inp::Text) exp = test inp $ (LCC.write LCC.style_write { LCC.style_write_color = False , LCC.style_write_align = True } . LCC.write_date <$>) <$> read_gram LCC.g_date inp @?= Right (S.Right exp) in [ test "Epoch" $ LCC.write LCC.style_write { LCC.style_write_color = False , LCC.style_write_align = True } (LCC.write_date H.date_epoch) @?= "1970-01-01" , "2000-01-01" ==> "2000-01-01" , "2000-01-01_12:34:51_CET" ==> "2000-01-01_11:34:51" , "2000-01-01_12:34:51+01:10" ==> "2000-01-01_11:24:51" , "2000-01-01_12:34:51-01:10" ==> "2000-01-01_13:44:51" , "2000-01-01_01:02:03" ==> "2000-01-01_01:02:03" , "2000-01-01_01:02" ==> "2000-01-01_01:02" , "2000-01-01_01:00" ==> "2000-01-01_01:00" ] , testGroup "Amount" $ let (<==) (inp::Text) exp = test inp $ LCC.write LCC.style_write { LCC.style_write_color = False , LCC.style_write_align = True } (LCC.write_amount exp) @?= TL.fromStrict inp in [ "0" <== ( mempty , LCC.amount ) , "0.00" <== ( mempty , LCC.amount { LCC.amount_quantity = Decimal 2 0 } ) , "123" <== ( mempty , LCC.amount { LCC.amount_quantity = Decimal 0 123 } ) , "-123" <== ( mempty , LCC.amount { LCC.amount_quantity = Decimal 0 (- 123) } ) , "12.3" <== ( mempty { LCC.style_amount_fractioning = S.Just '.' } , LCC.amount { LCC.amount_quantity = Decimal 1 123 } ) , "1,234.56" <== ( mempty { LCC.style_amount_fractioning = S.Just '.' , LCC.style_amount_grouping_integral = S.Just $ LCC.Style_Amount_Grouping ',' [3] } , LCC.amount { LCC.amount_quantity = Decimal 2 123456 }) , "123,456,789,01,2.3456789" <== ( mempty { LCC.style_amount_fractioning = S.Just '.' , LCC.style_amount_grouping_integral = S.Just $ LCC.Style_Amount_Grouping ',' [1, 2, 3] } , LCC.amount { LCC.amount_quantity = Decimal 7 1234567890123456789 } ) , "1234567.8_90_123_456_789" <== ( mempty { LCC.style_amount_fractioning = S.Just '.' , LCC.style_amount_grouping_fractional = S.Just $ LCC.Style_Amount_Grouping '_' [1, 2, 3] } , LCC.amount { LCC.amount_quantity = Decimal 12 1234567890123456789 }) , "1,2,3,4,5,6,7,89,012.3456789" <== ( mempty { LCC.style_amount_fractioning = S.Just '.' , LCC.style_amount_grouping_integral = S.Just $ LCC.Style_Amount_Grouping ',' [3, 2, 1] } , LCC.amount { LCC.amount_quantity = Decimal 7 1234567890123456789 }) , "1234567.890_12_3_4_5_6_7_8_9" <== ( mempty { LCC.style_amount_fractioning = S.Just '.' , LCC.style_amount_grouping_fractional = S.Just $ LCC.Style_Amount_Grouping '_' [3, 2, 1] } , LCC.amount { LCC.amount_quantity = Decimal 12 1234567890123456789 }) , testGroup "Width" $ let (==>) (inp::Text) = test inp $ LCC.width_amount <$> read_gram LCC.g_amount inp @?= Right (Text.length inp) in (==>) <$> [ "0.00" , "123" , "-123" , "12.3" , "12.5" , "12.3" , "1,234.56" , "123,456,789,01,2.3456789" , "1234567.8_90_123_456_789" , "1,2,3,4,5,6,7,89,012.3456789" , "1234567.890_12_3_4_5_6_7_8_9" , "1000000.000_00_0_0_0_0_0_0_0" , "999" , "1000" , "10,00€" , "10,00 €" , "€10,00" , "€ 10,00" -- , "EUR 10,00" -- , "10,00 EUR" ] ] , testGroup "Account" $ let (==>) inp = test inp $ LCC.write LCC.style_write { LCC.style_write_color = False , LCC.style_write_align = True } . LCC.write_account <$> read_gram LCC.g_account inp @?= Right (TL.fromStrict inp) in (==>) <$> [ "/A/B/C" ] , testGroup "Transaction" $ let (==>) i e = let inp = Text.intercalate "\n" i in let exp = Text.intercalate "\n" e in test inp $ (LCC.write LCC.style_write { LCC.style_write_color = False , LCC.style_write_align = True } . (uncurry LCC.write_transaction) <$>) <$> read_gram (LCC.g_get $ (\txn context_write_amounts -> (LCC.Context_Write { LCC.context_write_account_ref = True , LCC.context_write_max_posting_width = 0 , LCC.context_write_amounts },) <$> txn ) <$> LCC.g_transaction ) inp @?= Right (S.Right (TL.fromStrict exp)) in [ [ "2000-01-01 some wording" , " /A/B/C $1" , " /a/b/c" ] ==> [ "2000-01-01 some wording" , " /A/B/C $1" , " /a/b/c $-1" ] , [ "2000-01-01 some wording" , " /A/B/C $1" , " /a/b/c" , " ; first comment" , " ; second comment" , " ; third comment" ] ==> [ "2000-01-01 some wording" , " /A/B/C $1" , " /a/b/c $-1" , " ; first comment" , " ; second comment" , " ; third comment" ] , test "empty" $ LCC.write LCC.style_write { LCC.style_write_color = False , LCC.style_write_align = True } (LCC.write_transaction LCC.context_write LCC.transaction) @?= "1970-01-01\n" ] ]