Last active
March 20, 2020 19:23
-
-
Save caseploeg/5e7bc3103a2c2706f1c1f412fe50f027 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
How to use: | |
runghc test.hs | |
-} | |
import System.Environment (getArgs) | |
import System.Exit (exitFailure) | |
import Test.HUnit | |
import Text.Read (readMaybe) | |
import ExprParser (expr) | |
import ExprDef | |
import ParserLib | |
tests = [ | |
"example0" ~: | |
runParser expr "x + y" | |
~?= Just (Prim2 Add (Var "x") (Var "y")) | |
, | |
"example" ~: | |
runParser expr "!(x + y)" | |
~?= Just (Prim1 Not (Prim2 Add (Var "x") (Var "y"))) | |
, | |
"example2" ~: | |
-- WHY? addition and subtraction have the same precedence and are collectively left | |
-- associated | |
runParser expr "(x + y - z + a - b)" | |
~?= Just (Prim2 Sub (Prim2 Add (Prim2 Sub (Prim2 Add (Var "x") (Var "y")) (Var "z")) (Var "a")) (Var "b")) | |
, | |
"example2" ~: | |
runParser expr "(x - y - z)" | |
~?= Just (Prim2 Sub (Prim2 Sub (Var "x") (Var "y")) (Var "z")) | |
, | |
"example3" ~: | |
runParser expr "! - ! 5" | |
~?= Just (Prim1 Not (Prim1 Neg (Prim1 Not (LitNat 5)))) | |
, | |
"example3.1" ~: | |
runParser expr "! ! ! 5" | |
~?= Just (Prim1 Not (Prim1 Not (Prim1 Not (LitNat 5)))) | |
, | |
"example3.2" ~: | |
runParser expr "- - -5" | |
~?= Just (Prim1 Neg (Prim1 Neg (Prim1 Neg (LitNat 5)))) | |
, | |
-- WHY? because the operator function in ParseLib reads as many '!' chars in a row as it can (it's greedy munching), so "!!" gets read at once, which is unexpected, thus crashing to Nothing, unary operators need to have spaces inbetween like the above example | |
"example3.5" ~: | |
runParser expr "!!5" | |
~?= Nothing | |
, | |
"example4" ~: | |
runParser expr "(x || !(5 + 3))" | |
~?= Just (Prim2 Or (Var "x") (Prim1 Not (Prim2 Add (LitNat 5) (LitNat 3)))) | |
, | |
"example5" ~: | |
runParser expr "(5 == 6)" | |
~?= Just (Prim2 EqNat (LitNat 5) (LitNat 6)) | |
, | |
"example6" ~: | |
runParser expr "(5 == (y - z))" | |
~?= Just (Prim2 EqNat (LitNat 5) (Prim2 Sub (Var "y") (Var "z"))) | |
, | |
"example7" ~: | |
runParser expr "(4 == (5 == (y - z)))" | |
~?= Just (Prim2 EqNat (LitNat 4) (Prim2 EqNat (LitNat 5) (Prim2 Sub (Var "y") (Var "z")))) | |
, | |
-- WHY? the eq operator has no assocation, thus 2 in a row without parenthesis inbetween in unexpected (undefined) behaviour | |
"example7.5" ~: | |
runParser expr "(4 == 5 == (y - z))" | |
~?= Nothing | |
, | |
-- WHY? or is a reserved word and can't be a variable name | |
"example8" ~: | |
runParser expr "(or + 6)" | |
~?= Nothing | |
] | |
-- More tests when marking. | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment