3 import Control.Arrow (left)
4 import Control.Monad (Monad(..), sequence)
6 import Data.Either (Either(..))
7 import Data.Function (($), (.))
8 import Data.Functor ((<$>))
9 import Data.Semigroup (Semigroup(..))
10 import Data.String (String)
11 import System.FilePath (FilePath)
13 import Text.Show (Show(..))
14 import qualified Data.ByteString.Lazy as BSL
15 import qualified Data.List as List
16 import qualified Data.Text.Lazy as TL
17 import qualified Data.Text.Lazy.Encoding as TL
18 import qualified Text.Megaparsec as P
19 import qualified Data.TreeSeq.Strict as TS
22 import Test.Tasty.Golden
24 import Language.Symantic.XML (XMLs)
25 import qualified Language.Symantic.XML as XML
27 -- * Golden testing utilities
28 testGolden :: TestName -> TestName -> IO (Either String BSL.ByteString) -> TestTree
29 testGolden inputFile expectedExt =
30 goldenVsStringDiff inputFile diffGolden (inputFile <> expectedExt)
33 diffGolden :: FilePath -> FilePath -> [String]
34 diffGolden ref new = ["diff", "-u", ref, new]
36 unLeft :: Either String BSL.ByteString -> IO BSL.ByteString
38 Left err -> return $ TL.encodeUtf8 $ TL.pack err
41 readXML :: FilePath -> IO (Either String XMLs)
43 XML.readFile inputFile >>= \case
44 Left err -> return $ Left $ show err
46 return $ left P.parseErrorPretty $
47 XML.readXML inputFile input
49 goldensIO :: IO TestTree
51 testGroup "Golden" <$>
57 goldensXML :: IO TestTree
59 inputFiles <- List.sort <$> findByExtension [".xml"] "test/Golden/XML"
60 return $ testGroup "XML"
62 [ testGolden inputFile ".read" $
63 readXML inputFile >>= \ast ->
64 return $ TL.encodeUtf8 . TL.pack . TS.prettyTrees <$> ast
65 | inputFile <- inputFiles
67 , testGroup "Write" $ List.concat
69 [ testGolden inputFile ".write" $
70 readXML inputFile >>= \ast ->
71 return $ TL.encodeUtf8 . XML.writeXML <$> ast
72 , testGolden inputFile ".write.indented" $
73 readXML inputFile >>= \ast ->
74 return $ TL.encodeUtf8 . XML.writeXMLIndented (TL.pack " ") <$> ast
76 | inputFile <- inputFiles
77 , not $ List.isInfixOf "/Error/" inputFile
81 goldensRNC :: IO TestTree
83 return $ testGroup "RNC"