# Pastebin jb5JxydO import Data.ByteString.Lazy (ByteString) import qualified Data.ByteString.Lazy.Char8 as BC import qualified Data.ByteString as BS import Lib import ParseProgram import System.FilePath (takeBaseName, takeDirectory, ()) import Test.Tasty (TestTree, defaultMain, testGroup) import Test.Tasty.Golden (findByExtension, goldenVsStringDiff) import Text.Megaparsec import Text.Megaparsec.Error (errorBundlePretty) import System.Process main :: IO () main = defaultMain =<< sedReplacementTests sedReplacementTests :: IO TestTree sedReplacementTests = testGroup ".sedx golden tests" . fmap aTest <$> findByExtension [".sedx"] "test/examples" where aTest sedxFile = goldenVsStringDiff testName diff sedOutputGolden -- golden file path (runSedxAndReplace sedxFile sedInput) -- action whose result is tested where testName = takeDirectory sedxFile sedOutputGolden = testName "expected" sedInput = testName "input" diff ref new = ["diff", "-u", ref, new] runSedxAndReplace :: String -> String -> IO ByteString runSedxAndReplace sedxFile sedInput = do prog <- either (fail . errorBundlePretty) pure =<< (parse pProgram sedInput <$> readFile sedxFile) input <- readFile sedInput BC.pack <$> readProcess "sed" ["-E", printMatchAndSub prog] input