-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from CodeVisionaries/fix/project_structure
Fix project structure
- Loading branch information
Showing
13 changed files
with
72 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|