Skip to content

Commit

Permalink
Merge pull request #1 from CodeVisionaries/fix/project_structure
Browse files Browse the repository at this point in the history
Fix project structure
  • Loading branch information
gschnabel authored Oct 2, 2024
2 parents 27bc134 + 4b57dcc commit ead7af8
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 30 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
Expand Up @@ -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,{})
9 changes: 0 additions & 9 deletions larktools/LICENSE.txt

This file was deleted.

21 changes: 0 additions & 21 deletions larktools/README.md

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
45 changes: 45 additions & 0 deletions tests/test_arithmetic.py
Original file line number Diff line number Diff line change
@@ -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"]
12 changes: 12 additions & 0 deletions tests/test_suite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import unittest

from .test_arithmetic import *


def main():
unittest.TextTestRunner(verbosity=5).run(unittest.TestSuite())


if __name__ == "__main__":
main()

0 comments on commit ead7af8

Please sign in to comment.