1 {-# LANGUAGE FlexibleInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
5 import Control.Arrow (left)
6 import Control.Monad (Monad(..), sequence)
8 import Data.Either (Either(..))
9 import Data.Function (($), (.))
10 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.Text.Lazy as TL
19 import qualified Data.Text.Lazy.Encoding as TL
20 import qualified Text.Megaparsec as P
21 import qualified Data.TreeSeq.Strict as TS
24 import Test.Tasty.Golden
26 import Language.Symantic.XML (XMLs)
27 import qualified Language.Symantic.XML as XML
29 -- * Golden testing utilities
30 testGolden :: TestName -> TestName -> IO (Either String BSL.ByteString) -> TestTree
31 testGolden inputFile expectedExt =
32 goldenVsStringDiff inputFile diffGolden (inputFile <> expectedExt)
35 diffGolden :: FilePath -> FilePath -> [String]
36 diffGolden ref new = ["diff", "-u", ref, new]
38 unLeft :: Either String BSL.ByteString -> IO BSL.ByteString
40 Left err -> return $ TL.encodeUtf8 $ TL.pack err
43 readXML :: FilePath -> IO (Either String XMLs)
45 XML.readFile inputFile >>= \case
46 Left err -> return $ Left $ show err
48 return $ left P.errorBundlePretty $
49 XML.readXML inputFile input
51 goldensIO :: IO TestTree
53 testGroup "Golden" <$>
59 goldensXML :: IO TestTree
61 inputFiles <- List.sort <$> findByExtension [".xml"] "test/Golden/XML"
62 return $ testGroup "XML"
64 [ testGolden inputFile ".read" $
65 readXML inputFile >>= \ast ->
66 return $ TL.encodeUtf8 . TL.pack . TS.prettyTrees <$> ast
67 | inputFile <- inputFiles
69 , testGroup "Write" $ List.concat
71 [ testGolden inputFile ".write" $
72 readXML inputFile >>= \ast ->
73 return $ TL.encodeUtf8 . XML.writeXML <$> ast
74 , testGolden inputFile ".write.indented" $
75 readXML inputFile >>= \ast ->
76 return $ TL.encodeUtf8 . XML.writeXMLIndented (TL.pack " ") <$> ast
78 | inputFile <- inputFiles
79 , not $ List.isInfixOf "/Error/" inputFile
83 goldensRNC :: IO TestTree
85 return $ testGroup "RNC"