1 {-# LANGUAGE DataKinds #-}
2 {-# LANGUAGE TypeApplications #-}
5 import Control.Arrow (left)
6 import Control.Monad (Monad(..))
7 import Data.Either (Either(..))
8 import Data.Function (($), (.))
9 import Data.Functor ((<$>))
11 import Data.Semigroup (Semigroup(..))
12 import Data.String (String)
13 import System.FilePath (FilePath)
15 import Text.Show (Show(..))
16 import qualified Data.ByteString.Lazy as BSL
17 import qualified Data.List as List
18 import qualified Data.Map.Strict as Map
19 import qualified Data.Text as Text
20 import qualified Data.Text.Lazy as TL
21 import qualified Data.Text.Lazy.Encoding as TL
22 import qualified Data.TreeSeq.Strict as TreeSeq
23 import qualified Text.Blaze.Html.Renderer.Utf8 as Blaze
24 import qualified Text.Megaparsec as P
27 import Test.Tasty.Golden
30 import qualified Language.TCT as TCT
31 import qualified Language.TCT.Debug as TCT
32 import qualified Language.TCT.Write.HTML5 as TCT.Write.HTML5
33 import qualified Language.TCT.Write.Plain as TCT.Write.Plain
34 import qualified Language.TCT.Write.XML as TCT.Write.XML
37 import qualified Language.DTC.Document as DTC
38 import qualified Language.DTC.Write.HTML5 as DTC.Write.HTML5
39 import qualified Language.DTC.Write.XML as DTC.Write.XML
40 import qualified Language.DTC.Read.TCT as DTC
41 import qualified Language.DTC.Sym as DTC
42 import qualified Language.RNC.Write as RNC
43 import qualified Text.Blaze.DTC as Blaze.DTC
44 import qualified Text.Blaze.Utils as Blaze
46 -- * Golden testing utilities
47 testGolden :: TestName -> TestName -> IO (Either String BSL.ByteString) -> TestTree
48 testGolden inputFile expectedExt =
49 goldenVsStringDiff inputFile diffGolden (inputFile <> expectedExt)
52 diffGolden :: FilePath -> FilePath -> [String]
53 diffGolden ref new = ["diff", "-u", ref, new]
55 unLeft :: Either String BSL.ByteString -> IO BSL.ByteString
57 Left err -> return $ TL.encodeUtf8 $ TL.pack err
61 goldensIO :: IO TestTree
63 inputFiles <- List.sort <$> findByExtension [".tct"] "test/Golden"
66 [ goldensTCT inputFiles
67 , goldensDTC inputFiles
71 readTCT :: FilePath -> IO (Either String TCT.Roots)
72 readTCT inputFile = do
73 txt <- BSL.readFile inputFile
76 TCT.readTCT inputFile $
79 goldensTCT :: [FilePath] -> TestTree
80 goldensTCT inputFiles =
83 [ testGolden inputFile ".ast" $
84 readTCT inputFile >>= \ast ->
90 | inputFile <- inputFiles
93 [ testGolden inputFile "" $
94 readTCT inputFile >>= \ast ->
97 . (<> TL.singleton '\n')
98 . TCT.Write.Plain.document
100 | inputFile <- inputFiles
103 [ testGolden inputFile ".html5" $
104 readTCT inputFile >>= \ast ->
107 . TCT.Write.HTML5.document
109 | inputFile <- inputFiles
112 [ testGolden inputFile ".xml" $
113 readTCT inputFile >>= \ast ->
117 . TreeSeq.prettyTrees
118 . TCT.Write.XML.document
120 | inputFile <- inputFiles
125 type Langs = '[FR, EN]
126 readDTC :: FilePath -> IO (Either String DTC.Document)
127 readDTC inputFile = do
128 readTCT inputFile >>= \case
129 Left err -> return $ Left err
131 let xml = TCT.Write.XML.document tct in
132 case DTC.readDTC xml of
133 Left err -> return $ Left $ P.parseErrorPretty err
134 Right dtc -> return $ Right dtc
136 goldensDTC :: [FilePath] -> TestTree
137 goldensDTC inputFiles =
138 let locale = LocaleIn @Langs en_US in
139 let lang = Text.unpack $ textLocales Map.! locale in
142 [ testGolden "schema/dtc.rnc" "" $ do
152 [ testGolden inputFile (".dtc"<>"."<>lang<>".xml") $
153 readDTC inputFile >>= \dtc ->
155 Blaze.prettyMarkup Blaze.DTC.isInlinedElement
156 . DTC.Write.XML.document locale
158 | inputFile <- inputFiles
161 [ testGolden inputFile (".dtc"<>"."<>lang<>".html5") $
162 readDTC inputFile >>= \dtc ->
164 Blaze.prettyMarkup Blaze.DTC.isInlinedElement
165 . DTC.Write.HTML5.document locale
167 | inputFile <- inputFiles