summaryrefslogtreecommitdiff
path: root/lambda-calcul/haskell/test/Minilang/IOSpec.hs
diff options
context:
space:
mode:
authorArnaud Bailly <arnaud@pankzsoft.com>2025-10-17 09:52:06 +0200
committerArnaud Bailly <arnaud@pankzsoft.com>2025-10-17 10:14:33 +0200
commitc3a175e808b279eb415bcb5dbcb9db5f34035f98 (patch)
treeab35579f3e69494147117b110bcfa8b08eb911c4 /lambda-calcul/haskell/test/Minilang/IOSpec.hs
parentc6eaf6f117e0fe6c92a81dab542845d4ae8f64f7 (diff)
downloadlambda-nantes-c3a175e808b279eb415bcb5dbcb9db5f34035f98.tar.gz
feat: add basic application to evaluate "programs"
The main simply reads from its stdin, evaluates its input, and dump the result of the evaluation.
Diffstat (limited to 'lambda-calcul/haskell/test/Minilang/IOSpec.hs')
-rw-r--r--lambda-calcul/haskell/test/Minilang/IOSpec.hs37
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")