1 {-# LANGUAGE NoMonomorphismRestriction #-}
2 {-# LANGUAGE Rank2Types #-}
5 import Control.Monad (Monad(..))
6 import Data.Either (Either(..))
7 import Data.Function (($))
8 import Data.Semigroup (Semigroup(..))
9 import Data.String (String, IsString(..))
10 import System.IO (IO, FilePath)
11 import Text.Show (Show(..))
13 import Test.Tasty.Golden
14 import qualified Data.ByteString.Lazy as BSL
15 import qualified Data.IORef as IORef
16 import qualified Data.Text.Lazy as TL
17 import qualified Data.Text.Lazy.Encoding as TL
18 import qualified Language.Haskell.TH.Syntax as TH
20 import qualified Symantic.Parser as P
21 import qualified Symantic.Parser.Staging as Hask
24 goldensIO :: IO TestTree
25 goldensIO = return $ testGroup "Golden"
30 goldensGrammar :: TestTree
31 goldensGrammar = testGroup "Grammar"
32 [ testGroup "DumpComb" $
34 let file = "test/Golden/Grammar/"<>name<>".dump" in
35 goldenVsStringDiff file diffGolden file $ do
36 -- XXX: Resetting 'TH.counter' makes 'makeLetName' deterministic,
37 -- except when profiling is enabled, in this case those tests may fail
38 -- due to a different numbering of the 'def' and 'ref' combinators.
39 IORef.writeIORef TH.counter 0
40 return $ fromString $ show $ P.dumpComb $ P.observeSharing repr
41 , testGroup "OptimizeComb" $
43 let file = "test/Golden/Grammar/"<>name<>".opt.dump" in
44 goldenVsStringDiff file diffGolden file $ do
45 IORef.writeIORef TH.counter 0
46 return $ fromString $ show $ P.dumpComb $ P.optimizeComb $ P.observeSharing repr
49 tests :: P.Grammar repr => (forall a. String -> repr a -> TestTree) -> [TestTree]
51 [ test "unit" $ P.unit
52 , test "unit-unit" $ P.unit P.*> P.unit
53 , test "app" $ P.pure (Hask.Haskell Hask.id) P.<*> P.unit
55 , test "brainfuck" $ brainfuck
58 -- * Golden testing utilities
60 diffGolden :: FilePath -> FilePath -> [String]
61 diffGolden ref new = ["diff", "-u", ref, new]
63 unLeft :: Either String BSL.ByteString -> IO BSL.ByteString
65 Left err -> return $ TL.encodeUtf8 $ TL.pack err