packages feed

gemoire-0.1.0: test/Main.hs

{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Control.Monad (when)
import qualified Data.HashMap.Strict as M
import System.Exit (exitFailure)
import Test.HUnit (Test (..), failures, runTestTT, (~=?))

import Gemoire.Gemlog.Feed (lastModified, sortPosts)
import Gemoire.Gemlog.Post (parsePost)
import Gemoire.Template (Component (..), format, template)

-- | Query the test cases.
main :: IO ()
main = do
    count <-
        runTestTT . TestList $
            feeds <> posts <> templates
    when (failures count > 0) exitFailure

feeds :: [Test]
feeds =
    let post =
            [ M.fromList [("post", "1"), ("modified", "2024-08-17T05:00:00+00:00")]
            , M.fromList [("post", "2"), ("modified", "2024-08-17T09:00:00+00:00")]
            , M.fromList [("post", "3"), ("modified", "2024-11-14T13:47:01+00:00")]
            ]
     in [ [ M.fromList [("post", "3"), ("modified", "2024-11-14T13:47:01+00:00")]
          , M.fromList [("post", "2"), ("modified", "2024-08-17T09:00:00+00:00")]
          , M.fromList [("post", "1"), ("modified", "2024-08-17T05:00:00+00:00")]
          ]
            ~=? sortPosts post
        , M.fromList [("modified", "2024-11-14T13:47:01+00:00"), ("modified_date", "2024-11-14")]
            ~=? lastModified post
        ]

posts :: [Test]
posts =
    [ M.fromList [("post", "post")] ~=? parsePost M.empty "post"
    , M.fromList [("title", "heading"), ("post", "# heading")] ~=? parsePost M.empty "# heading"
    , M.fromList [("data", "extras"), ("post", "with extras")]
        ~=? parsePost (M.singleton "data" "extras") "with {$data$}"
    , M.fromList [("data", "variables"), ("post", "with variables")]
        ~=? parsePost M.empty "{= data variables =}\CR\LFwith {$data$}"
    ]

templates :: [Test]
templates =
    [ [Left "text"] ~=? template "text"
    , [Left "also {$", Left "text"] ~=? template "also {$text"
    , [Right $ Placeholder "placeholder" Nothing] ~=? template "{$ placeholder $}"
    , [Right $ Placeholder "with" $ Just "default value"] ~=? template "{$with:default value$}"
    , [Right $ Placeholder "{$weird" Nothing, Left "$}"] ~=? template "{${$weird$}$}"
    , [Right $ Placeholder "pholder" $ Just "multiline \CR\LF value"]
        ~=? template "{$pholder:multiline \CR\LF value$}"
    , [Right $ Placeholder "multiline\CR\LFunfortunately" Nothing]
        ~=? template "{$multiline\CR\LFunfortunately$}"
    , [Left "mixed ", Right $ Placeholder "pholder" Nothing, Left " text"]
        ~=? template "mixed {$pholder$} text"
    , [Right $ Fallback "variable" "fallback" Nothing] ~=? template "{&variable:fallback&}"
    , [Right $ Fallback "same as a variable" "" Nothing] ~=? template "{& same as a variable&}"
    , [Right $ Fallback "var" "fback" (Just "default")] ~=? template "{&var:fback:default&}"
    , [Left "commented ", Left " text"] ~=? template "commented {# meow #} text"
    , [] ~=? template "{# comment with {$flare$} #}"
    , [] ~=? template "{#multiline \CR\LF comment#}"
    , [Left "{# not a comment #}"] ~=? template "{\\{# not a comment #}\\}"
    , [Left "constant"] ~=? template "{\\constant\\}"
    , "formatted text!" ~=? format (template "formatted {$ key $}") (M.singleton "key" "text!")
    , "default value" ~=? format (template "{$key:default$} value") M.empty
    , "falled back!" ~=? format (template "falled {&unexist:fallback&}!") (M.singleton "fallback" "back")
    , "couldn't fall back." ~=? format (template "couldn't {&a:b:fall&} back.") M.empty
    ]