— How to use: runghc testFoo.hs
import System.Environment (getArgs)
import System.Exit (exitFailure)
import Test.HUnit
import Text.Read (readMaybe)
import ParserLib
import TrycatDef
import TrycatParser (trycat, stmt)
tests =
[ “assign” ~:
runParser trycat “xy0 := 3 / y2” ~?= Just (Assign “xy0” (Div (Lit 3) (Var “y2”)))
, “try-catch” ~:
runParser trycat “try x:=x/0;y:=0/x; catch x:=1+0;y:=0+2; endtry”
~?= Just (Try [Assign “x” (Div (Var “x”) (Lit 0)),
Assign “y” (Div (Lit 0) (Var “x”))]
[Assign “x” (Add (Lit 1) (Lit 0)),
Assign “y” (Add (Lit 0) (Lit 2))])
, “compound” ~:
runParser trycat “begin x:=x/0; y:=0/x; end”
~?= Just (Compound [Assign “x” (Div (Var “x”) (Lit 0)),
Assign “y” (Div (Lit 0) (Var “x”))])
, “reserved word is not var” ~:
runParser trycat “x := try + catch” ~?= Nothing
]
main = do
args <- getArgs
case args of
a:_ | Just n <- readMaybe a, 0 <= n, n < length tests ->
do c@Counts{errors=e, failures=f} <- runTestTT (tests !! n)
if e == 0 && f == 0
then return c
else exitFailure
| otherwise -> error “No such test number.”
_ -> runTestTT (TestList tests)