add evaluation tests

This commit is contained in:
Patrick Aldis 2026-01-15 16:01:15 +00:00
parent b603d9edf9
commit e462724482

View File

@ -18,6 +18,11 @@ checkParse :: String -> Expr -> Expectation
checkParse text expectedExpr = checkParse text expectedExpr =
parse parseExpr "" text `shouldBe` Right expectedExpr parse parseExpr "" text `shouldBe` Right expectedExpr
checkEval :: String -> Int -> Expectation
checkEval text expectedVal =
fmap eval (parse parseExpr "" text) `shouldBe` Right expectedVal
spec :: Spec spec :: Spec
spec = do spec = do
describe "evaluate expressions" $ do describe "evaluate expressions" $ do
@ -39,5 +44,11 @@ spec = do
checkParse "2+3*5" (BinaryExpr Add (Literal 2) (BinaryExpr Multiply (Literal 3) (Literal 5))) checkParse "2+3*5" (BinaryExpr Add (Literal 2) (BinaryExpr Multiply (Literal 3) (Literal 5)))
checkParse "(2+3)*5" (BinaryExpr Multiply (BinaryExpr Add (Literal 2) (Literal 3)) (Literal 5) ) checkParse "(2+3)*5" (BinaryExpr Multiply (BinaryExpr Add (Literal 2) (Literal 3)) (Literal 5) )
checkParse "((2+3))*5" (BinaryExpr Multiply (BinaryExpr Add (Literal 2) (Literal 3)) (Literal 5) ) checkParse "((2+3))*5" (BinaryExpr Multiply (BinaryExpr Add (Literal 2) (Literal 3)) (Literal 5) )
it "can evaluate expressions correctly" $ do
checkEval "2" 2
checkEval "3*4/2" 6
checkEval "2+3*6" 20
checkEval "1-2+3" (-4)