packages feed

symantic-parser-0.2.1.20210803: tests/Golden/Utils.hs

module Golden.Utils where

import Control.Monad (Monad(..))
import Data.Either (Either(..))
import Data.Function (($))
import Data.Semigroup (Semigroup(..))
import Data.String (String)
import System.IO (IO, FilePath)
import System.IO.Unsafe (unsafePerformIO)
import qualified Data.ByteString.Lazy as BSL
import qualified Data.IORef as IORef

import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
import qualified Language.Haskell.TH.Syntax as TH
import Paths_symantic_parser

getGoldenDir :: FilePath -> FilePath
getGoldenDir p = unsafePerformIO $ getDataFileName $ "tests/Golden/" <> p

goldenDiff :: FilePath -> FilePath -> [String]
goldenDiff ref new = ["diff", "-u", "-w", "-B", ref, new]

-- | Resetting 'TH.counter' makes 'makeLetName' deterministic,
-- except when GHC or executable flags change, like profiling
-- or even --accept unfortunately,
-- in those case the golden tests may fail
-- due to a different numbering of the 'ref' combinators.
-- Hence 'ShowLetName' is used with 'False' there.
resetTHNameCounter :: IO ()
resetTHNameCounter = IORef.writeIORef TH.counter 0

unLeft :: Either String BSL.ByteString -> IO BSL.ByteString
unLeft lr = case lr of
  Left err -> return $ TL.encodeUtf8 $ TL.pack err
  Right a  -> return a