module Data.TreeDiff.Golden (
ediffGolden,
) where
import Data.TreeDiff
import Prelude ()
import Prelude.Compat
import System.Console.ANSI (SGR (Reset), setSGRCode)
import Text.Parsec (eof, parse)
import Text.Parsec.Text ()
import qualified Data.ByteString as BS
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
ediffGolden
:: (Eq a, ToExpr a)
=> (testName -> IO Expr -> IO Expr -> (Expr -> Expr -> IO (Maybe String)) -> (Expr -> IO ()) -> testTree)
-> testName
-> FilePath
-> IO a
-> testTree
ediffGolden impl testName fp x = impl testName expect actual cmp wrt
where
actual = fmap toExpr x
expect = do
contents <- BS.readFile fp
case parse (exprParser <* eof) fp $ T.decodeUtf8 contents of
Left err -> print err >> fail "parse error"
Right r -> return r
cmp a b
| a == b = return Nothing
| otherwise = return $ Just $
setSGRCode [Reset] ++ show (ansiWlEditExprCompact $ ediff a b)
wrt expr = BS.writeFile fp $ T.encodeUtf8 $ T.pack $ show (prettyExpr expr) ++ "\n"