From 87692f1c8c7a4dc48a7bd319a0a0e15070d8e852 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Wed, 21 Dec 2022 22:11:13 +0000 Subject: Tidy up --- test/Language.hs | 198 +++++++++++++++++++++++++++---------------------------- 1 file changed, 99 insertions(+), 99 deletions(-) (limited to 'test/Language.hs') 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 -- cgit v1.2.3