aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2022-09-26 10:38:59 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2022-12-05 14:21:53 +0200
commit98d3f41cd8cebf1c1e26c29f01140d0543ee5853 (patch)
tree5924997c3f45d514e624b646baa1f2bb09103c81
parent15b613ac76fa2a9a439f10ac9455c6eb1498b03f (diff)
Improve tests
-rw-r--r--src/Utils.hs3
-rw-r--r--test/Spec.hs29
-rw-r--r--test/TestUtils.hs11
3 files changed, 28 insertions, 15 deletions
diff --git a/src/Utils.hs b/src/Utils.hs
index e2c182b..3cf61bf 100644
--- a/src/Utils.hs
+++ b/src/Utils.hs
@@ -55,6 +55,7 @@ instance (Eq AST) where
ASTBoolean a == ASTBoolean b = a == b
ASTString a == ASTString b = a == b
ASTVector a == ASTVector b = a == b
+ ASTFunctionCall a == ASTFunctionCall b = a == b
ASTHashMap a == ASTHashMap b = a == b
ASTUnit == ASTUnit = True
_ == _ = False
@@ -66,7 +67,9 @@ instance (Ord AST) where
ASTBoolean a <= ASTBoolean b = a <= b
ASTString a <= ASTString b = a <= b
ASTVector a <= ASTVector b = a <= b
+ ASTFunctionCall a <= ASTFunctionCall b = a <= b
ASTHashMap a <= ASTHashMap b = a <= b
+ ASTUnit <= ASTUnit = True
_ <= _ = False
assertIsASTFunction :: AST -> LContext AST
diff --git a/test/Spec.hs b/test/Spec.hs
index 0869b55..db28f39 100644
--- a/test/Spec.hs
+++ b/test/Spec.hs
@@ -1,21 +1,30 @@
{-# OPTIONS_GHC -Wno-missing-signatures #-}
import Test.HUnit
import Control.Monad.Except
+import qualified Data.Map as M
import Tokenizer ( tokenize )
import Parser ( parse )
+import Evaluator ( evaluate )
+import Builtins
import TestUtils
import Utils
-test1 = (runLContext $ tokenize "(+ 1 (- 10 5))")
- >>= assertEqual "tokenize" ["(", "+", "1", "(", "-", "10", "5", ")", ")"]
-
-test2 = (runLContext $ parse ["(", "+", "1", "2", ")"])
- >>= assertEqual "parse" [ASTFunctionCall [ASTSymbol "+", ASTInteger 1, ASTInteger 2]]
-
-tests = TestList $ map TestCase [
- test1,
- test2
+tests = [
+ "tokenize1" ~:
+ do got <- runL $ tokenize "(+ 1 (- 10 5))"
+ let expected = ["(", "+", "1", "(", "-", "10", "5", ")", ")"]
+ assertEqual "" got expected,
+ "parse1" ~:
+ do got <- runL $ parse ["(", "+", "1", "2", ")"]
+ let expected = [ASTFunctionCall [ASTSymbol "+", ASTInteger 1, ASTInteger 2]]
+ assertEqual "" got expected,
+ "evaluate1" ~:
+ do let env = M.fromList [("+", builtinAdd2)] :: Env
+ (gotEnv, gotAST) <- runL $ evaluate env (ASTFunctionCall [ASTSymbol "+", ASTInteger 1, ASTInteger 2])
+ let expectedAST = ASTInteger 3
+ assertEqual "" gotAST expectedAST
+ assertEqual "" (M.keys gotEnv) (M.keys env)
]
main :: IO ()
-main = void $ runTestTT tests
+main = void $ runTestTT $ test tests
diff --git a/test/TestUtils.hs b/test/TestUtils.hs
index 478d923..bcb147c 100644
--- a/test/TestUtils.hs
+++ b/test/TestUtils.hs
@@ -12,8 +12,9 @@ initialConfig = Config {
configShowHelp = False
}
-runLContext :: LContext a -> IO a
-runLContext lc = do res <- runExceptT $ runReaderT lc initialConfig
- case res of
- Left (LException err) -> error err
- Right val -> return val
+runL :: LContext a -> IO a
+runL lc = do res <- runExceptT $ runReaderT lc initialConfig
+ case res of
+ Left (LException err) -> error err
+ Right val -> return val
+