module Golden where import Control.Monad (Monad(..)) import Data.Either (Either(..)) import Data.Function (($)) import Data.Semigroup (Semigroup(..)) import Data.String (String, IsString(..)) import System.IO (IO, FilePath) import Test.Tasty import Test.Tasty.Golden 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 qualified Symantic.Parser as P import Golden.Grammar goldensIO :: IO TestTree goldensIO = return $ testGroup "Golden" [ goldensGrammar ] goldensGrammar :: TestTree goldensGrammar = testGroup "Grammar" [ testGroup "DumpComb" $ let file p = "test/Golden/Grammar/"<>p<>".dump" in let test name repr = goldenVsStringDiff (file name) diffGolden (file name) $ do -- XXX: Resetting 'TH.counter' makes 'makeLetName' deterministic, -- except when profiling is enabled, in this case those tests may fail -- due to a different numbering of the 'def' and 'ref' combinators. IORef.writeIORef TH.counter 0 return $ fromString $ P.showGrammar repr in [ test "unit" P.unit , test "unit-unit" $ P.unit P.*> P.unit , test "boom" boom , test "brainfuck" brainfuck ] ] -- * Golden testing utilities diffGolden :: FilePath -> FilePath -> [String] diffGolden ref new = ["diff", "-u", ref, new] unLeft :: Either String BSL.ByteString -> IO BSL.ByteString unLeft = \case Left err -> return $ TL.encodeUtf8 $ TL.pack err Right a -> return a