Files
2024B1-AP/a2/a2-handout/src/APL/Check_Tests.hs
2024-09-20 16:34:10 +02:00

46 lines
1.5 KiB
Haskell

module APL.Check_Tests (tests) where
import APL.AST (Exp (..))
import APL.Check (checkExp)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (assertFailure, testCase, (@?=))
-- Assert that the provided expression should pass the type checker.
testPos :: Exp -> TestTree
testPos e =
testCase (show e) $
checkExp e @?= Nothing
-- Assert that the provided expression should fail the type checker.
testNeg :: Exp -> TestTree
testNeg e =
testCase (show e) $
case checkExp e of
Nothing -> assertFailure "expected error"
Just _ -> pure ()
tests :: TestTree
tests =
testGroup
"Checking"
[
testPos (CstBool True),
testNeg (Var "x"),
testPos (Let "x" (CstInt 3) (CstInt 5)),
testPos (Let "x" (CstInt 3) (Var "x")),
testNeg (Let "x" (Var "y") (Var "x")),
testNeg (Let "x" (CstInt 3) (Var "y")),
testPos (Add (Sub (CstInt 9) (CstInt 6)) (CstInt 11)),
testPos (Mul (Div (CstInt 9) (CstInt 3)) (CstInt 1)),
testNeg (Add (Add (CstInt 1) (Var "x")) (CstInt 1)),
testPos (If (CstInt 2) (CstInt 2) (CstInt 2)),
testNeg (If (Var "x") (CstInt 2) (CstInt 2)),
testNeg (If (CstInt 2) (Var "x") (CstInt 2)),
testNeg (If (CstInt 2) (CstInt 2) (Var "x")),
testPos (Lambda "x" (CstInt 5)),
testPos (Lambda "x" (Var "x")),
testNeg (Lambda "x" (Var "y")),
testPos (Apply (Lambda "x" (Var "x")) (CstInt 5)),
testNeg (Apply (Lambda "x" (Var "x")) (Var "x"))
]