blob: 4a30a6493e74a0d53d0952d520e3d0e9f9110099 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
|