From 9f899fc8cda116a09066b3e1ef2d677b223fba7f Mon Sep 17 00:00:00 2001 From: the-dipsy Date: Sun, 18 Feb 2024 16:51:19 +0100 Subject: [PATCH] test: add tests for component code segmentation Further manual testing and concentrated code staring has been performed for some edge cases. --- tests/compiler/code/__init__.py | 0 tests/compiler/code/test_segment.py | 44 +++++++++++++++++++++++++++++ tests/components/bar/fred.pyro | 2 +- tests/components/bar/main.pyro | 6 +++- tests/components/foo/baz.pyro | 18 +++++++++++- tests/components/quux.pyro | 4 ++- tests/components/qux.pyro | 2 +- 7 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 tests/compiler/code/__init__.py create mode 100644 tests/compiler/code/test_segment.py diff --git a/tests/compiler/code/__init__.py b/tests/compiler/code/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/compiler/code/test_segment.py b/tests/compiler/code/test_segment.py new file mode 100644 index 0000000..68a01c8 --- /dev/null +++ b/tests/compiler/code/test_segment.py @@ -0,0 +1,44 @@ +from unittest import TestCase +from pathlib import Path +from pyromaniac.compiler.code.segment.errors import ( + InvalidSignatureError, + UnexpectedTokenError, +) +from pyromaniac.compiler.code.segment import segment + + +class TestSegment(TestCase): + def setUp(self): + self.comps = Path(__file__).parent.parent.parent.joinpath("components") + + def test_pure_yaml(self): + doc, sig, python, yaml = segment(self.load("qux.pyro")) + self.assertIsNotNone(yaml) + self.assertTupleEqual((doc, sig, python), (None, None, None)) + + def test_pure_python(self): + doc, sig, python, yaml = segment(self.load("quux.pyro")) + self.assertIsNotNone(python) + self.assertTupleEqual((doc, sig, yaml), (None, None, None)) + + def test_all(self): + doc, sig, python, yaml = segment(self.load("foo", "baz.pyro")) + self.assertIsNotNone(doc) + self.assertIsNotNone(sig) + self.assertIsNotNone(python) + self.assertIsNotNone(yaml) + self.assertTrue(python.splitlines()[8].startswith("message =")) + self.assertTrue(yaml.splitlines()[13].startswith("storage.files")) + + def test_invalid_signature(self): + with self.assertRaises(InvalidSignatureError): + segment(self.load("bar", "main.pyro")) + + def test_unexpected_token(self): + with self.assertRaises(UnexpectedTokenError) as ctx: + segment(self.load("bar", "fred.pyro")) + self.assertEqual(ctx.exception.line, 1) + self.assertEqual(ctx.exception.token.string, ')') + + def load(self, *path: str) -> str: + return self.comps.joinpath(*path).read_text() diff --git a/tests/components/bar/fred.pyro b/tests/components/bar/fred.pyro index 0967ef4..5e2a061 100644 --- a/tests/components/bar/fred.pyro +++ b/tests/components/bar/fred.pyro @@ -1 +1 @@ -{} +(foo: str)) \ No newline at end of file diff --git a/tests/components/bar/main.pyro b/tests/components/bar/main.pyro index 0967ef4..312dda6 100644 --- a/tests/components/bar/main.pyro +++ b/tests/components/bar/main.pyro @@ -1 +1,5 @@ -{} +(name: str, age: int + +--- + +{"message": f"Hello {age} year old {name}!"} diff --git a/tests/components/foo/baz.pyro b/tests/components/foo/baz.pyro index 0967ef4..f6980aa 100644 --- a/tests/components/foo/baz.pyro +++ b/tests/components/foo/baz.pyro @@ -1 +1,17 @@ -{} +"""doc string""" # this is the doc string + +# Now comes the signature +(name: str, age: int, *args: str, **kwargs: float) # this is the signature + +--- + +# here we use string interpolation to construct a message +message = f"Hello {age} year old {name}!" + +--- + +# here comes the yaml code +storage.files[0]: + path: /greeting.txt + contents.inline: {{ message }} # here we insert the message with jinja syntax + "hello" \ No newline at end of file diff --git a/tests/components/quux.pyro b/tests/components/quux.pyro index 0967ef4..2c2021a 100644 --- a/tests/components/quux.pyro +++ b/tests/components/quux.pyro @@ -1 +1,3 @@ -{} +--- +path = "/directory" +{"storage.directories[0].path": path} \ No newline at end of file diff --git a/tests/components/qux.pyro b/tests/components/qux.pyro index 0967ef4..ac8a471 100644 --- a/tests/components/qux.pyro +++ b/tests/components/qux.pyro @@ -1 +1 @@ -{} +storage.files[0].path: /file.txt \ No newline at end of file