From ea61bd9fb1b72daca46d818b50b0ff4683661a9c Mon Sep 17 00:00:00 2001 From: ksenmel Date: Mon, 18 Nov 2024 03:04:39 +0300 Subject: [PATCH] feat(parser-tests): add tests for binary and unary operations parsing --- lib/tests/parser/test_parser.ml | 92 +++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 lib/tests/parser/test_parser.ml diff --git a/lib/tests/parser/test_parser.ml b/lib/tests/parser/test_parser.ml new file mode 100644 index 0000000..7bfb4bb --- /dev/null +++ b/lib/tests/parser/test_parser.ml @@ -0,0 +1,92 @@ +(* * Copyright 2024, raf-nr and ksenmel *) + +(** SPDX-License-Identifier: LGPL-3.0-or-later *) + +open Base +open Parser + + +(* Binary and unary operations *) + +let%expect_test _ = + test_parse {| 1 + 2 + 3 * 4 |}; + [%expect {| + [(SExpression + (EApplication ((EIdentifier (Id "( * )")), + [(EApplication ((EConstant (CInt 1)), + [(EApplication ((EIdentifier (Id "( + )")), + [(EConstant (CInt 0)); (EConstant (CInt 2))])); + (EApplication ((EIdentifier (Id "( + )")), + [(EConstant (CInt 0)); (EConstant (CInt 3))])) + ] + )); + (EConstant (CInt 4))] + ))) + ] |}] +;; + +let%expect_test _ = + test_parse {| 1 * (+ 2) / ( - 3) - 4 |}; + [%expect {| + [(SExpression + (EApplication ((EIdentifier (Id "( - )")), + [(EApplication ((EIdentifier (Id "( / )")), + [(EApplication ((EIdentifier (Id "( * )")), + [(EConstant (CInt 1)); + (EApplication ((EIdentifier (Id "( + )")), + [(EConstant (CInt 0)); (EConstant (CInt 2))])) + ] + )); + (EApplication ((EIdentifier (Id "( - )")), + [(EConstant (CInt 0)); (EConstant (CInt 3))])) + ] + )); + (EConstant (CInt 4))] + ))) + ] |}] + + +let%expect_test _ = + test_parse {| -1 |}; + [%expect {| + [(SExpression + (EApplication ((EIdentifier (Id "( - )")), + [(EConstant (CInt 0)); (EConstant (CInt 1))]))) + ] |}] + +let%expect_test _ = + test_parse {| ((((-1)))) |}; + [%expect {| + [(SExpression + (EApplication ((EIdentifier (Id "( - )")), + [(EConstant (CInt 0)); (EConstant (CInt 1))]))) + ] |}] + +;; +let%expect_test _ = + test_parse {| ((((-1)))) |}; + [%expect {| + [(SExpression + (EApplication ((EIdentifier (Id "( - )")), + [(EConstant (CInt 0)); (EConstant (CInt 1))]))) + ] |}] + + +let%expect_test _ = + test_parse {| ((-(+(-1)))) |}; + [%expect {| + [(SExpression + (EApplication ((EIdentifier (Id "( - )")), + [(EConstant (CInt 0)); + (EApplication ((EIdentifier (Id "( + )")), + [(EConstant (CInt 0)); + (EApplication ((EIdentifier (Id "( - )")), + [(EConstant (CInt 0)); (EConstant (CInt 1))])) + ] + )) + ] + ))) + ] |}] + + +