From 0306eae9d3016cf524a64ea14ae655ea03366b70 Mon Sep 17 00:00:00 2001 From: thomasms Date: Mon, 30 Sep 2024 15:50:14 +0200 Subject: [PATCH 1/2] Adding some simple tests --- README.md | 11 ++++++++ larktools/tests/test_arithmetic.py | 45 ++++++++++++++++++++++++++++++ larktools/tests/test_suite.py | 12 ++++++++ 3 files changed, 68 insertions(+) create mode 100644 larktools/tests/test_arithmetic.py create mode 100644 larktools/tests/test_suite.py diff --git a/README.md b/README.md index c806e95..7d87f48 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,18 @@ # larktools + ## Development + This package is using [hatch](https://hatch.pypa.io/1.12/) as project managment tool. Install hatch via `pip`: `pip install hatch`. For running tests using hatch, see the [hatch run tests documentation](https://hatch.pypa.io/1.9/community/contributing/#run-the-tests). + + +### Tests + +To run the test suite, `pytest` is recommended, and can be done via: + +```bash +pytest -v --maxfail=1 larktools/tests/test_suite.py +``` diff --git a/larktools/tests/test_arithmetic.py b/larktools/tests/test_arithmetic.py new file mode 100644 index 0000000..2c271a5 --- /dev/null +++ b/larktools/tests/test_arithmetic.py @@ -0,0 +1,45 @@ +import unittest +from typing import Optional, Union + +from lark import Lark + +from larktools.ebnf_grammar import grammar +from larktools.evaluation import eval_arith_expr + + +class ArithParser: + def __init__(self): + self.parser = Lark(grammar, parser="lalr", start="arith_expr") + self.parse = self.parser.parse + + def parse_and_eval(self, expression: str, env: Optional[Union[None, dict]] = None) -> Union[int, float]: + tree = self.parse(expression) + res = eval_arith_expr(tree, {} if env is None else env) + return res + + +class ArithemticTests(unittest.TestCase): + + def setUp(self): + self.parser = ArithParser() + + def _parse_and_assert(self, expression: str, expected: Union[int, float]) -> None: + res = self.parser.parse_and_eval(expression) + self.assertEqual(expected, res) + + def test_integer_addition(self): + self._parse_and_assert("3 + 5", 8) + self._parse_and_assert("5 + 3", 8) + self._parse_and_assert("9999999999999999 + 555555555555555", 10555555555555554) + + def test_integer_addition_neg(self): + self._parse_and_assert("-5 + 3", -2) + self._parse_and_assert("3 + (-5)", -2) + self._parse_and_assert("9999999999999999 + (-9999999999999999)", 0) + + def test_float_addition(self): + self._parse_and_assert("3.00000001 + 5.2", 8.20000001) + self._parse_and_assert("5e3 + 1.23E-2", 5000.00123) + + +__all__ = ["ArithemticTests"] diff --git a/larktools/tests/test_suite.py b/larktools/tests/test_suite.py new file mode 100644 index 0000000..a0400c9 --- /dev/null +++ b/larktools/tests/test_suite.py @@ -0,0 +1,12 @@ +import unittest + +from .test_arithmetic import * + + +def main(): + unittest.TextTestRunner(verbosity=5).run(unittest.TestSuite()) + + +if __name__ == "__main__": + main() + From 4b57dcc05522c1e6c2fe6c9282648d5c21f6e76d Mon Sep 17 00:00:00 2001 From: "SPRENGER Julia, NEA/SCI/DB" Date: Wed, 2 Oct 2024 14:41:05 +0200 Subject: [PATCH 2/2] Deflate project structure --- .../examples => examples}/first_example.py | 4 ++++ larktools/LICENSE.txt | 9 -------- larktools/README.md | 21 ------------------- larktools/pyproject.toml => pyproject.toml | 0 {larktools/src => src}/larktools/__about__.py | 0 {larktools/src => src}/larktools/__init__.py | 0 .../src => src}/larktools/ebnf_grammar.py | 0 .../src => src}/larktools/evaluation.py | 0 .../src => src}/larktools/tree_utils.py | 0 {larktools/tests => tests}/__init__.py | 0 {larktools/tests => tests}/test_arithmetic.py | 0 {larktools/tests => tests}/test_suite.py | 0 12 files changed, 4 insertions(+), 30 deletions(-) rename {larktools/examples => examples}/first_example.py (89%) delete mode 100644 larktools/LICENSE.txt delete mode 100644 larktools/README.md rename larktools/pyproject.toml => pyproject.toml (100%) rename {larktools/src => src}/larktools/__about__.py (100%) rename {larktools/src => src}/larktools/__init__.py (100%) rename {larktools/src => src}/larktools/ebnf_grammar.py (100%) rename {larktools/src => src}/larktools/evaluation.py (100%) rename {larktools/src => src}/larktools/tree_utils.py (100%) rename {larktools/tests => tests}/__init__.py (100%) rename {larktools/tests => tests}/test_arithmetic.py (100%) rename {larktools/tests => tests}/test_suite.py (100%) diff --git a/larktools/examples/first_example.py b/examples/first_example.py similarity index 89% rename from larktools/examples/first_example.py rename to examples/first_example.py index d0402d6..799a631 100644 --- a/larktools/examples/first_example.py +++ b/examples/first_example.py @@ -21,3 +21,7 @@ res2 = eval_arith_expr(tree2, env) var_assign_str = ", ".join(f"{var}={val}" for var, val in env.items()) print(f"{arith_expr2} evaluates to {res2} when using {var_assign_str}") + +arith_expr3 = "5 / 5 - 3 * 6" +tree3 = parsefun(arith_expr3) +eval_arith_expr(tree3,{}) diff --git a/larktools/LICENSE.txt b/larktools/LICENSE.txt deleted file mode 100644 index 3420f14..0000000 --- a/larktools/LICENSE.txt +++ /dev/null @@ -1,9 +0,0 @@ -MIT License - -Copyright (c) 2024-present CodeVisionaries - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/larktools/README.md b/larktools/README.md deleted file mode 100644 index fbb63d7..0000000 --- a/larktools/README.md +++ /dev/null @@ -1,21 +0,0 @@ -# larktools - -[![PyPI - Version](https://img.shields.io/pypi/v/larktools.svg)](https://pypi.org/project/larktools) -[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/larktools.svg)](https://pypi.org/project/larktools) - ------ - -## Table of Contents - -- [Installation](#installation) -- [License](#license) - -## Installation - -```console -pip install larktools -``` - -## License - -`larktools` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license. diff --git a/larktools/pyproject.toml b/pyproject.toml similarity index 100% rename from larktools/pyproject.toml rename to pyproject.toml diff --git a/larktools/src/larktools/__about__.py b/src/larktools/__about__.py similarity index 100% rename from larktools/src/larktools/__about__.py rename to src/larktools/__about__.py diff --git a/larktools/src/larktools/__init__.py b/src/larktools/__init__.py similarity index 100% rename from larktools/src/larktools/__init__.py rename to src/larktools/__init__.py diff --git a/larktools/src/larktools/ebnf_grammar.py b/src/larktools/ebnf_grammar.py similarity index 100% rename from larktools/src/larktools/ebnf_grammar.py rename to src/larktools/ebnf_grammar.py diff --git a/larktools/src/larktools/evaluation.py b/src/larktools/evaluation.py similarity index 100% rename from larktools/src/larktools/evaluation.py rename to src/larktools/evaluation.py diff --git a/larktools/src/larktools/tree_utils.py b/src/larktools/tree_utils.py similarity index 100% rename from larktools/src/larktools/tree_utils.py rename to src/larktools/tree_utils.py diff --git a/larktools/tests/__init__.py b/tests/__init__.py similarity index 100% rename from larktools/tests/__init__.py rename to tests/__init__.py diff --git a/larktools/tests/test_arithmetic.py b/tests/test_arithmetic.py similarity index 100% rename from larktools/tests/test_arithmetic.py rename to tests/test_arithmetic.py diff --git a/larktools/tests/test_suite.py b/tests/test_suite.py similarity index 100% rename from larktools/tests/test_suite.py rename to tests/test_suite.py