diff options
Diffstat (limited to 'lambda-calcul/haskell/test')
| -rw-r--r-- | lambda-calcul/haskell/test/Minilang/IOSpec.hs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/lambda-calcul/haskell/test/Minilang/IOSpec.hs b/lambda-calcul/haskell/test/Minilang/IOSpec.hs new file mode 100644 index 0000000..038877d --- /dev/null +++ b/lambda-calcul/haskell/test/Minilang/IOSpec.hs @@ -0,0 +1,37 @@ +module Minilang.IOSpec where + +import Control.Exception (bracket) +import Minilang.IO (runEval) +import System.Directory (getTemporaryDirectory, removeFile) +import System.FilePath ((<.>), (</>)) +import System.IO (IOMode (..), hClose, readFile, withFile, writeFile) +import System.Posix (mkstemp) +import Test.Hspec (Spec, around, describe, it, parallel, shouldBe) +import Prelude hiding (lines, readFile, writeFile) + +spec :: Spec +spec = parallel $ + describe "MiniLang I/O Evaluator" $ do + around withTempFile $ + it "evaluates a simple MiniLang 'program' and dumps result" $ \fileName -> do + let outputFileName = fileName <> ".out" + program = + [ "(App (Lam \"x\" (Var \"x\")) (Var \"y\"))" + ] + + writeFile fileName (unlines program) + + _ <- withFile fileName ReadMode $ \hin -> + withFile outputFileName AppendMode $ \hout -> + runEval hin hout + + out <- readFile outputFileName + + out `shouldBe` "Var \"y\"\n" + +withTempFile :: (String -> IO ()) -> IO () +withTempFile = + bracket mkTempFile rmTempFile + where + mkTempFile = getTemporaryDirectory >>= \dir -> mkstemp (dir </> "test-repl") >>= \(fp, h) -> hClose h >> pure fp + rmTempFile fn = removeFile fn >> removeFile (fn <.> "out") |
