Skip to content

Commit

Permalink
test: add tests for component code segmentation
Browse files Browse the repository at this point in the history
Further manual testing and concentrated code staring has been performed
for some edge cases.
  • Loading branch information
the-dipsy committed Feb 18, 2024
1 parent 93632ca commit 9f899fc
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 5 deletions.
Empty file added tests/compiler/code/__init__.py
Empty file.
44 changes: 44 additions & 0 deletions tests/compiler/code/test_segment.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion tests/components/bar/fred.pyro
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{}
(foo: str))
6 changes: 5 additions & 1 deletion tests/components/bar/main.pyro
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
(name: str, age: int

---

{"message": f"Hello {age} year old {name}!"}
18 changes: 17 additions & 1 deletion tests/components/foo/baz.pyro
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 3 additions & 1 deletion tests/components/quux.pyro
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
---
path = "/directory"
{"storage.directories[0].path": path}
2 changes: 1 addition & 1 deletion tests/components/qux.pyro
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{}
storage.files[0].path: /file.txt

0 comments on commit 9f899fc

Please sign in to comment.