aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2022-12-21 22:11:13 +0000
committerJuan J. Martinez <jjm@usebox.net>2022-12-21 22:11:13 +0000
commit87692f1c8c7a4dc48a7bd319a0a0e15070d8e852 (patch)
tree329ae78acc78ef3a0906aa89150bf76837508125 /test
parent1e33e1510bc5891f040137b0c33fa8d49fe3a4c9 (diff)
downloadmicro-lang-hs-87692f1c8c7a4dc48a7bd319a0a0e15070d8e852.tar.gz
micro-lang-hs-87692f1c8c7a4dc48a7bd319a0a0e15070d8e852.zip
Tidy up
Diffstat (limited to 'test')
-rw-r--r--test/Language.hs198
1 files changed, 99 insertions, 99 deletions
diff --git a/test/Language.hs b/test/Language.hs
index 3e6a898..351e244 100644
--- a/test/Language.hs
+++ b/test/Language.hs
@@ -11,31 +11,31 @@ import Text.Parsec.Pos (newPos)
assertCompileAst :: String -> [A.Expr] -> Assertion
assertCompileAst input expected = do
- r <- return $ parseFromString input
+ let r = parseFromString input
case r of
Left e -> assertFailure $ show e
Right ast -> do
- res <- return $ compileToAst ast
+ let res = compileToAst ast
case res of
Left e -> assertFailure $ show e
Right ast -> assertEqual "" expected ast
assertAst :: String -> [A.Expr] -> Assertion
assertAst input expected = do
- r <- return $ parseFromString input
+ let r = parseFromString input
case r of
Left e -> assertFailure $ show e
Right ast -> assertEqual "" expected ast
expectError :: String -> E.ErrorType -> Assertion
expectError input etyp = do
- r <- return $ parseFromString input
+ let r = parseFromString input
case r of
Left e -> assertFailure $ show e
Right ast -> do
- res <- return $ compile ast
+ let res = compile ast
case res of
- Left e -> case (find (\(E.Error t _ _) -> t == etyp) e) of
+ Left e -> case find (\(E.Error t _ _) -> t == etyp) e of
Just _ -> return ()
Nothing -> assertFailure $ "expected " ++ show etyp ++ " didn't happen, got instead:\n" ++ unlines (map (\(E.Error t _ _) -> show t) e)
Right _ -> assertFailure "expected error, didn't happen"
@@ -53,115 +53,115 @@ testCases =
],
TestLabel
"parse a function"
- $ TestCase $
- assertAst
- "module main\n\
- \def fn() { }"
- [ A.Module "main" $ newPos "-" 1 1,
- A.Func "fn" [] Nothing [] False False $ newPos "-" 2 1
- ],
+ $ TestCase
+ $ assertAst
+ "module main\n\
+ \def fn() { }"
+ [ A.Module "main" $ newPos "-" 1 1,
+ A.Func "fn" [] Nothing [] False False $ newPos "-" 2 1
+ ],
TestLabel
"parse a function with parameters"
- $ TestCase $
- assertAst
- "module main\n\
- \def fn(a: u8) { }"
- [ A.Module "main" $ newPos "-" 1 1,
- A.Func "fn" [("a", A.Type "u8", True, newPos "-" 2 8)] Nothing [] False False $ newPos "-" 2 1
- ],
+ $ TestCase
+ $ assertAst
+ "module main\n\
+ \def fn(a: u8) { }"
+ [ A.Module "main" $ newPos "-" 1 1,
+ A.Func "fn" [("a", A.Type "u8", True, newPos "-" 2 8)] Nothing [] False False $ newPos "-" 2 1
+ ],
TestLabel
"parse a function with return value"
- $ TestCase $
- assertAst
- "module main\n\
- \def fn(): u8 {\n\
- \return 1; }"
- [ A.Module "main" $ newPos "-" 1 1,
- A.Func "fn" [] (Just $ A.Type "u8") [A.Return (Just $ A.Num 1 $ newPos "-" 3 8) $ newPos "-" 3 1] False False $ newPos "-" 2 1
- ],
+ $ TestCase
+ $ assertAst
+ "module main\n\
+ \def fn(): u8 {\n\
+ \return 1; }"
+ [ A.Module "main" $ newPos "-" 1 1,
+ A.Func "fn" [] (Just $ A.Type "u8") [A.Return (Just $ A.Num 1 $ newPos "-" 3 8) $ newPos "-" 3 1] False False $ newPos "-" 2 1
+ ],
TestLabel
"parse a function call"
- $ TestCase $
- assertAst
- "module main\n\
- \def fn() { }\n\
- \fn();"
- [ A.Module "main" $ newPos "-" 1 1,
- A.Func "fn" [] Nothing [] False False $ newPos "-" 2 1,
- A.Call (A.Variable "fn" $ newPos "-" 3 1) [] $ newPos "-" 3 1
- ],
+ $ TestCase
+ $ assertAst
+ "module main\n\
+ \def fn() { }\n\
+ \fn();"
+ [ A.Module "main" $ newPos "-" 1 1,
+ A.Func "fn" [] Nothing [] False False $ newPos "-" 2 1,
+ A.Call (A.Variable "fn" $ newPos "-" 3 1) [] $ newPos "-" 3 1
+ ],
TestLabel
"parse a function call with arguments"
- $ TestCase $
- assertAst
- "module main\n\
- \def fn(a: u8) { }\n\
- \fn(10);"
- [ A.Module "main" $ newPos "-" 1 1,
- A.Func "fn" [("a", A.Type "u8", True, newPos "-" 2 8)] Nothing [] False False $ newPos "-" 2 1,
- A.Call (A.Variable "fn" $ newPos "-" 3 1) [A.Num 10 $ newPos "-" 3 4] $ newPos "-" 3 1
- ],
+ $ TestCase
+ $ assertAst
+ "module main\n\
+ \def fn(a: u8) { }\n\
+ \fn(10);"
+ [ A.Module "main" $ newPos "-" 1 1,
+ A.Func "fn" [("a", A.Type "u8", True, newPos "-" 2 8)] Nothing [] False False $ newPos "-" 2 1,
+ A.Call (A.Variable "fn" $ newPos "-" 3 1) [A.Num 10 $ newPos "-" 3 4] $ newPos "-" 3 1
+ ],
TestLabel
"parse empty return on a function"
- $ TestCase $
- assertAst
- "module main\n\
- \def fn() {\n\
- \return; }"
- [ A.Module "main" $ newPos "-" 1 1,
- A.Func "fn" [] Nothing [A.Return Nothing $ newPos "-" 3 1] False False $ newPos "-" 2 1
- ],
+ $ TestCase
+ $ assertAst
+ "module main\n\
+ \def fn() {\n\
+ \return; }"
+ [ A.Module "main" $ newPos "-" 1 1,
+ A.Func "fn" [] Nothing [A.Return Nothing $ newPos "-" 3 1] False False $ newPos "-" 2 1
+ ],
TestLabel
"parse a recursive function"
- $ TestCase $
- assertAst
- "module main\n\
- \def fn() {\n\
- \fn(); }"
- [ A.Module "main" $ newPos "-" 1 1,
- A.Func "fn" [] Nothing [A.Call (A.Variable "fn" $ newPos "-" 3 1) [] $ newPos "-" 3 1] False False $ newPos "-" 2 1
- ],
+ $ TestCase
+ $ assertAst
+ "module main\n\
+ \def fn() {\n\
+ \fn(); }"
+ [ A.Module "main" $ newPos "-" 1 1,
+ A.Func "fn" [] Nothing [A.Call (A.Variable "fn" $ newPos "-" 3 1) [] $ newPos "-" 3 1] False False $ newPos "-" 2 1
+ ],
TestLabel
"parse a function with a function parameter"
- $ TestCase $
- assertAst
- "module main\n\
- \def fn1() { }\n\
- \def fn2(f: ()) {\n\
- \f(); }\n\
- \fn2(fn1);"
- [ A.Module "main" $ newPos "-" 1 1,
- A.Func "fn1" [] Nothing [] False False $ newPos "-" 2 1,
- A.Func
- "fn2"
- [("f", A.FuncType [] Nothing, True, newPos "-" 3 9)]
- Nothing
- [ A.Call (A.Variable "f" $ newPos "-" 4 1) [] $ newPos "-" 4 1
- ]
- False
- False
- $ newPos "-" 3 1,
- A.Call (A.Variable "fn2" $ newPos "-" 5 1) [A.Variable "fn1" $ newPos "-" 5 5] $ newPos "-" 5 1
- ],
+ $ TestCase
+ $ assertAst
+ "module main\n\
+ \def fn1() { }\n\
+ \def fn2(f: ()) {\n\
+ \f(); }\n\
+ \fn2(fn1);"
+ [ A.Module "main" $ newPos "-" 1 1,
+ A.Func "fn1" [] Nothing [] False False $ newPos "-" 2 1,
+ A.Func
+ "fn2"
+ [("f", A.FuncType [] Nothing, True, newPos "-" 3 9)]
+ Nothing
+ [ A.Call (A.Variable "f" $ newPos "-" 4 1) [] $ newPos "-" 4 1
+ ]
+ False
+ False
+ $ newPos "-" 3 1,
+ A.Call (A.Variable "fn2" $ newPos "-" 5 1) [A.Variable "fn1" $ newPos "-" 5 5] $ newPos "-" 5 1
+ ],
TestLabel
"parse a function with a function parameter (lambda)"
- $ TestCase $
- assertAst
- "module main\n\
- \def fn(f: ()) {\n\
- \f(); }\n\
- \fn(() { });"
- [ A.Module "main" $ newPos "-" 1 1,
- A.Func
- "fn"
- [("f", A.FuncType [] Nothing, True, newPos "-" 2 8)]
- Nothing
- [A.Call (A.Variable "f" $ newPos "-" 3 1) [] $ newPos "-" 3 1]
- False
- False
- $ newPos "-" 2 1,
- A.Call (A.Variable "fn" $ newPos "-" 4 1) [A.Func "lambda@4,4" [] Nothing [] True True $ newPos "-" 4 4] $ newPos "-" 4 1
- ],
+ $ TestCase
+ $ assertAst
+ "module main\n\
+ \def fn(f: ()) {\n\
+ \f(); }\n\
+ \fn(() { });"
+ [ A.Module "main" $ newPos "-" 1 1,
+ A.Func
+ "fn"
+ [("f", A.FuncType [] Nothing, True, newPos "-" 2 8)]
+ Nothing
+ [A.Call (A.Variable "f" $ newPos "-" 3 1) [] $ newPos "-" 3 1]
+ False
+ False
+ $ newPos "-" 2 1,
+ A.Call (A.Variable "fn" $ newPos "-" 4 1) [A.Func "lambda@4,4" [] Nothing [] True True $ newPos "-" 4 4] $ newPos "-" 4 1
+ ],
TestLabel "parse a call to lambda" $
TestCase $
assertAst