From 55692e10192968ae7f040ceb9a8469b6ce2412c4 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 16 Aug 2022 22:34:52 +0100 Subject: Added more tests --- test/Language.hs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/test/Language.hs b/test/Language.hs index 9590b8d..c804035 100644 --- a/test/Language.hs +++ b/test/Language.hs @@ -13,7 +13,7 @@ assertAst tcase input expected = do r <- return $ runParser (scan parse) () "test" input case r of Left e -> assertFailure $ show e - Right ast -> assertEqual tcase ast expected + Right ast -> assertEqual tcase expected ast testCase1 = TestCase $ assertAst "parse module" "module main" [A.Module "main" $ newPos "test" 1 1] @@ -83,4 +83,68 @@ testCase7 = A.Func "fn" [] Nothing [A.Return Nothing $ newPos "test" 3 1] False False $ newPos "test" 2 1 ] -language = [testCase1, testCase2, testCase3, testCase4, testCase5, testCase6, testCase7] +testCase8 = + TestCase $ + assertAst + "parse a recursive function" + "module main\n\ + \def fn() {\n\ + \fn(); }" + [ A.Module "main" $ newPos "test" 1 1, + A.Func "fn" [] Nothing [A.Call (A.Var "fn" $ newPos "test" 3 1) [] $ newPos "test" 3 1] False False $ newPos "test" 2 1 + ] + +testCase9 = + TestCase $ + assertAst + "parse a function with a function parameter" + "module main\n\ + \def fn1() { }\n\ + \def fn2(f: ()) {\n\ + \f(); }\n\ + \fn2(fn1);" + [ A.Module "main" $ newPos "test" 1 1, + A.Func "fn1" [] Nothing [] False False $ newPos "test" 2 1, + A.Func + "fn2" + [("f", A.FuncType [] Nothing, newPos "test" 3 9)] + Nothing + [ A.Call (A.Var "f" $ newPos "test" 4 1) [] $ newPos "test" 4 1 + ] + False + False + $ newPos "test" 3 1, + A.Call (A.Var "fn2" $ newPos "test" 5 1) [A.Var "fn1" $ newPos "test" 5 5] $ newPos "test" 5 1 + ] + +testCase10 = + TestCase $ + assertAst + "parse a function with a function parameter (lambda)" + "module main\n\ + \def fn(f: ()) {\n\ + \f(); }\n\ + \fn(() { });" + [ A.Module "main" $ newPos "test" 1 1, + A.Func + "fn" + [("f", A.FuncType [] Nothing, newPos "test" 2 8)] + Nothing + [A.Call (A.Var "f" $ newPos "test" 3 1) [] $ newPos "test" 3 1] + False + False + $ newPos "test" 2 1, + A.Call (A.Var "fn" $ newPos "test" 4 1) [A.Func "lambda@4,4" [] Nothing [] True True $ newPos "test" 4 4] $ newPos "test" 4 1 + ] + +testCase11 = + TestCase $ + assertAst + "parse a call to lambda" + "module main\n\ + \() { }();" + [ A.Module "main" $ newPos "test" 1 1, + A.Call (A.Func "lambda@2,1" [] Nothing [] True True $ newPos "test" 2 1) [] $ newPos "test" 2 1 + ] + +language = [testCase1, testCase2, testCase3, testCase4, testCase5, testCase6, testCase7, testCase8, testCase9, testCase10, testCase11] -- cgit v1.2.3