-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for component code segmentation
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
Showing
7 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
Empty file.
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,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() |
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 +1 @@ | ||
{} | ||
(foo: str)) |
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 +1,5 @@ | ||
{} | ||
(name: str, age: int | ||
|
||
--- | ||
|
||
{"message": f"Hello {age} year old {name}!"} |
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 +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" |
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 +1,3 @@ | ||
{} | ||
--- | ||
path = "/directory" | ||
{"storage.directories[0].path": path} |
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 +1 @@ | ||
{} | ||
storage.files[0].path: /file.txt |