-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.hs
44 lines (33 loc) · 1.58 KB
/
Test.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{- Module that tests Syntax as well as Runtime by taking strings, parsing them, executing the result and then checking the result of the computation-}
module Test where
import Test.HUnit
import LangDef
import Syntax
import Runtime
import Parser
import RuntimeTest hiding (runall)
testexpr :: String -> String -> Integer -> Test
testexpr msg s ie = TestCase (assertEqual msg ie result)
where expr = case norest parseExpr s of
[(expr, "")] -> expr
[] -> error ("Could not parse "++s)
result = case eval expr newState of
(MyInteger i, state) -> i
_ -> error (s ++ " did not eval to number")
teststmt msg var expctd es = TestCase (assertEqual msg expctd result)
where result = case norest parseStmt es of
[(stmt, "")] -> runRet stmt var
[] -> error ("Could not parse "++es)
x -> error ("Multiple possible interpretations of "++ es ++ " Interpretations are :" ++ show x)
simple = testexpr "simple" "1" 1
simple2 = testexpr "simple2" "2+3 -2" 3
ops = testexpr "calc with operators" "2 * 2 +3 -2" 5
braces = testexpr "braces" "(2 * 2)+3 -2" 5
braces2 = testexpr "braces2" "2 * (2 +3) -2" 8
bracessimple = testexpr "braces simple" "( 3 )" 3
fac = teststmt "fak" "r" 24
"{i=4;r=1;while not (i == 1) do {r = r*i; i=i-1}}"
facrec = teststmt "fakrec" "r" 24
"{let {fak = function(a) if a == 1 then return 1 else return a*(fak (a-1))}; r = fak(4)}"
runall = runTestTT ( "AllTests" ~: test testLst )
where testLst = [simple, simple2, ops, braces, braces2, bracessimple, fac, facrec]