module Chart.Test where import Test.Tasty import Test.Tasty.HUnit import Control.Monad (join) import Data.Bool import Data.Char (Char) import Data.Either (Either(..)) import Data.Function (($), (.)) import Data.Functor ((<$>)) import Data.NonNull (NonNull) import Data.Proxy import Data.Text (Text) import Data.TreeMap.Strict.Zipper import Data.Tuple (fst) import System.FilePath.Posix (()) import System.IO (IO, FilePath) import qualified Data.NonNull as NonNull import qualified System.Directory as IO import qualified System.IO.Memoize as IO import qualified Text.Megaparsec as P -- import Control.Applicative (Applicative(..)) -- import Control.Monad.IO.Class (MonadIO(..)) -- import Data.Functor.Identity (Identity(..)) -- import Data.Maybe (isJust) -- import Prelude (error) -- import Text.Show (show) -- import qualified Data.List as List -- import qualified Data.Map.Strict as Map -- import qualified Data.Strict as S -- import qualified Data.Text as Text -- import qualified Data.Text.IO as Text.IO -- import qualified Data.TreeMap.Strict as TreeMap -- import qualified Hcompta as H import qualified Hcompta.LCC as LCC import qualified Hcompta.LCC.Sym () import qualified Language.Symantic as Sym import qualified Language.Symantic.Lib () type Ifaces = '[Proxy (->), Proxy LCC.Quantity] chart_from_file :: FilePath -> IO (Either (P.ParseError Char P.Dec) LCC.Chart) chart_from_file file = do cwd <- IO.getCurrentDirectory (fst . fst <$>) $ -- NOTE: drop final contexts. LCC.read_file (cwd file) $ LCC.read @Ifaces @[LCC.Transaction] $ LCC.g_get $ (\_j chart -> chart) <$> -- NOTE: get Chart from final context. LCC.g_journal @(Sym.TyConsts_of_Ifaces Ifaces) @Ifaces @[LCC.Transaction] (:) chart_00 :: (FilePath, IO (Either (P.ParseError Char P.Dec) LCC.Chart)) chart_00 = (file,) $ join $ IO.once $ chart_from_file file where file = "Hcompta/LCC/Chart/chart.00.lcc" chart_01 :: (FilePath, IO (Either (P.ParseError Char P.Dec) LCC.Chart)) chart_01 = (file,) $ join $ IO.once $ chart_from_file file where file = "Hcompta/LCC/Chart/chart.01.lcc" tag_path :: [Text] -> LCC.Tag_Path tag_path p = LCC.Tag_Path $ NonNull.impureNonNull (LCC.Name <$> p) zip_path :: [Text] -> NonNull [LCC.Name] zip_path p = NonNull.impureNonNull (LCC.Name <$> p) tests :: TestTree tests = testGroup "Chart" [ testGroup "chart_axis_child" $ let (==>) (file, input) expected = testCase file $ do lr_chart <- input let path = do chart <- lr_chart let sel = zipper_child let res = sel $ LCC.zipper_chart chart Right $ (LCC.unName <$>) . path_of_zipper <$> res path @?= Right expected in [ chart_00 ==> [ ["1.Capital"] , ["2.Immobilisation"] , ["3.Stock"] , ["4.Tiers"] , ["5.Finance"] , ["6.Charge"] , ["7.Produit"] ] ] , testGroup "chart_axis_descendant" $ let (==>) (file, input) expected = testCase file $ do lr_chart <- input let path = do chart <- lr_chart let sel = zipper_descendant let res = sel $ LCC.zipper_chart chart Right $ (LCC.unName <$>) . path_of_zipper <$> res path @?= Right expected in [ chart_00 ==> [ ["1.Capital"] , ["2.Immobilisation"] , ["3.Stock"] , ["4.Tiers"] , ["4.Tiers", "0.Fournisseur"] , ["4.Tiers", "0.Fournisseur", "9.Débiteur"] , ["4.Tiers", "1.Client"] , ["4.Tiers", "2.Personnel"] , ["4.Tiers", "3.Sécu"] , ["4.Tiers", "4.État"] , ["5.Finance"] , ["5.Finance", "1.Établissement"] , ["5.Finance", "1.Établissement", "2.Banque"] , ["5.Finance", "3.Caisse"] , ["6.Charge"] , ["7.Produit"] ] ] , testGroup "zipper_account_tags" $ let (==>) (file, input) expected = testCase file $ do lr_chart <- input let path = do chart <- lr_chart let sel = -- //*[...[~Bilan][0][~Bilan:Actif]] zipper_descendant `zipper_filter` not . zipper_null (zipper_ancestor_or_self `zipper_filter` not . zipper_null (zipper_child_lookup $ LCC.Name "Bilan") . LCC.zipper_accounts_tags `zipper_at` 0 `zipper_filter` not . zipper_null (zipper_child_lookups $ zip_path ["Bilan", "Actif"]) . LCC.zipper_accounts_tags) let res = sel $ zipper $ LCC.chart_accounts chart Right $ (LCC.unName <$>) . path_of_zipper <$> res path @?= Right expected in [ chart_00 ==> [ ["2.Immobilisation"] , ["4.Tiers","0.Fournisseur","9.Débiteur"] , ["4.Tiers","1.Client"] , ["4.Tiers","3.Sécu"] ] ] ]