summaryrefslogtreecommitdiff
path: root/test/mustache-th/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'test/mustache-th/Main.hs')
-rw-r--r--test/mustache-th/Main.hs101
1 files changed, 101 insertions, 0 deletions
diff --git a/test/mustache-th/Main.hs b/test/mustache-th/Main.hs
new file mode 100644
index 0000000..4a30a64
--- /dev/null
+++ b/test/mustache-th/Main.hs
@@ -0,0 +1,101 @@
+{-|
+Copyright: (c) Tom Smeding 2026
+License: AGPL-3.0-or-later
+-}
+{-# LANGUAGE OverloadedStrings #-}
+module Main where
+
+import Control.Monad.Trans.Class (lift)
+import Control.Monad (forM_)
+import Control.Monad.Trans.Except
+import Data.Bifunctor (first)
+import Data.IORef
+import Data.Map.Strict (Map)
+import Data.Text (Text)
+import Data.Text qualified as T
+import Data.Yaml qualified as Y
+import System.Directory
+import System.Environment (getArgs)
+import System.Exit (exitSuccess, exitFailure)
+import System.FilePath
+
+import Mustache.TH.Parser
+import RenderNonTH
+
+
+-- TODO: This tests only the parser, not the renderer. Parse all the test YAMLs
+-- in TH and generate data types for the data objects??
+
+
+data SpecFile = SpecFile [SpecTest]
+ deriving (Show)
+
+data SpecTest = SpecTest
+ { stName :: Text
+ , stDesc :: Text
+ , stData :: Y.Value
+ , stTemplate :: Text
+ , stExpected :: Text
+ , stPartials :: Map Text Text }
+ deriving (Show)
+
+instance Y.FromJSON SpecFile where
+ parseJSON = Y.withObject "SpecFile" $ \v -> SpecFile
+ <$> v Y..: "tests"
+
+instance Y.FromJSON SpecTest where
+ parseJSON = Y.withObject "SpecTest" $ \v -> SpecTest
+ <$> v Y..: "name"
+ <*> v Y..: "desc"
+ <*> v Y..: "data"
+ <*> v Y..: "template"
+ <*> v Y..: "expected"
+ <*> v Y..:? "partials" Y..!= mempty
+
+runTest :: String -> SpecTest -> IO (Either String ())
+runTest fname test = runExceptT $ do
+ let name = fname ++ "/" ++ T.unpack (stName test)
+ let quote s = "<<\x1B[38:5:249m" ++ s ++ "\x1B[0m>>"
+ formatOK = "\x1B[32mOK\x1B[0m " ++ name
+ formatParseError e = "\x1B[31;1m> " ++ name ++ "\n\x1B[0;31mParse failed:\x1B[0m " ++ e
+ formatWrong tpl out =
+ "\x1B[31;1m> " ++ name ++ "\n\x1B[0;31mTest failed;\x1B[0m template:\n" ++
+ quote (T.unpack (stTemplate test)) ++ "\nparsed:\n" ++
+ quote (show tpl) ++ "\nexpected:\n" ++
+ quote (show (stExpected test)) ++ "\ngot:\n" ++ quote (show out)
+
+ partials <- except $ first formatParseError $ traverse parseTemplate (stPartials test)
+
+ tpl <- except $ first formatParseError $ parseTemplate (stTemplate test)
+ let res = render partials tpl (stData test)
+ if res == stExpected test
+ then lift $ putStrLn formatOK
+ else throwE $ formatWrong tpl res
+
+printUsage :: IO ()
+printUsage = putStrLn
+ "Usage: test-mustache-th <specdir>\n\
+ \The <specdir> should point to a checkout of https://github.com/mustache/spec ;\n\
+ \this test suite reads files <specdir>/specs/*.yml ."
+
+main :: IO ()
+main = do
+ specDirname <- getArgs >>= \case
+ [path] | path /= "-h", path /= "--help" -> return path
+ [_] -> printUsage >> exitSuccess
+ _ -> printUsage >> exitFailure
+
+ let suitable name =
+ takeExtension name == ".yml" &&
+ take 1 name /= "~"
+ fnames <- filter suitable <$> listDirectory (specDirname </> "specs")
+ ok <- newIORef True
+ forM_ fnames $ \fname -> do
+ SpecFile tests <- Y.decodeFileThrow (specDirname </> "specs" </> fname)
+ forM_ tests $ \test -> do
+ runTest fname test >>= \case
+ Left err -> putStrLn err >> writeIORef ok False
+ Right () -> return ()
+ readIORef ok >>= \case
+ True -> return ()
+ False -> exitFailure