Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TTENSHII committed Dec 5, 2023
1 parent 9c88c5a commit b390b67
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 44 deletions.
45 changes: 1 addition & 44 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,5 @@
-- Main
-}

import ComputeAST
import AST
import Defines

-- Foo = 42
test0 :: Tree
test0 = Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))

test1 :: Tree
test1 = Leaf (Symbol "foo")

-- Bar = 21
test2 :: Tree
test2 = Node "define" (Just (Leaf (Symbol "bar"))) (Just (Leaf (Number 21)))

-- 21 + 21 (result = 42)
test3 :: Tree
test3 = Node "+" (Just (Leaf (Number 21))) (Just (Leaf (Number 21)))

-- Foo + 42 (result = 42 + 42 = 84)
test4 :: Tree
test4 = Node "+" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))

-- Foo + Bar (result = 42 + 21 = 63)
test5 :: Tree
test5 = Node "+" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Symbol "bar")))

-- 2 + (5 * 2) (result = 12)
test6 :: Tree
test6 = Node "+" (Just (Leaf (Number 2))) (Just (Node "*" (Just (Leaf (Number 5))) (Just (Leaf (Number 2)))))

-- 2 + (foo / 2) (result = 23)
test7 :: Tree
test7 = Node "+" (Just (Leaf (Number 2))) (Just (Node "/" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 2)))))

-- 2 + 2 + (5 * 2) (result = 14)
test8 :: Tree
test8 = Node "+" (Just (Leaf (Number 2))) (Just (Node "+" (Just (Leaf (Number 2))) (Just (Node "*" (Just (Leaf (Number 5))) (Just (Leaf (Number 2)))))))

-- (2 * 5) + (foo / 2) (result = 10 + 21 = 31)
test9 :: Tree
test9 = Node "+" (Just (Node "*" (Just (Leaf (Number 2))) (Just (Leaf (Number 5))))) (Just (Node "/" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 2)))))

main :: IO ()
main = putStrLn $ show $ computeAllAST (Env []) [test0, test1, test2, test3, test4, test5, test6, test7, test8, test9]
main = putStrLn "Test"
28 changes: 28 additions & 0 deletions test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,32 @@ unitTestASTCompute = testGroup "AST compute Tests"
assertEqual "define foo 42 and tree with leaf foo"
[Number 42]
(computeAllAST (Env []) [(Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))), (Leaf (Symbol "foo"))])
, testCase "test3" $
assertEqual "define foo 42 and do foo + 42"
[Number 84]
(computeAllAST (Env []) [(Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))), (Node "+" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42))))])
, testCase "test3" $
assertEqual "define foo 42 and do 42 + foo"
[Number 84]
(computeAllAST (Env []) [(Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))), (Node "+" (Just (Leaf (Number 42))) (Just (Leaf (Symbol "foo"))))])
, testCase "test5" $
assertEqual "define foo 42 and do foo + foo"
[Number 84]
(computeAllAST (Env []) [(Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))), (Node "+" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Symbol "foo"))))])
, testCase "test6" $
assertEqual "define foo 42 and bar 21 and do foo + bar"
[Number 63]
(computeAllAST (Env []) [(Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))), (Node "define" (Just (Leaf (Symbol "bar"))) (Just (Leaf (Number 21)))), (Node "+" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Symbol "bar"))))])
, testCase "test7" $
assertEqual "2 + (5 * 2) (result = 12)"
[Number 12]
(computeAllAST (Env []) [(Node "+" (Just (Leaf (Number 2))) (Just (Node "*" (Just (Leaf (Number 5))) (Just (Leaf (Number 2))))))])
, testCase "test8" $
assertEqual "(2 * 5) + (foo / 2) (result = 10 + 21 = 31)"
[Number 31]
(computeAllAST (Env []) [(Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))), Node "+" (Just (Node "*" (Just (Leaf (Number 2))) (Just (Leaf (Number 5))))) (Just (Node "/" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 2)))))])
, testCase "test9" $
assertEqual "2 + 2 + (5 * 2) (result = 14)"
[Number 14]
(computeAllAST (Env []) [(Node "+" (Just (Leaf (Number 2))) (Just (Node "+" (Just (Leaf (Number 2))) (Just (Node "*" (Just (Leaf (Number 5))) (Just (Leaf (Number 2))))))))])
]

0 comments on commit b390b67

Please sign in to comment.