Skip to content

Commit

Permalink
test: add tests for pyromaniac component class
Browse files Browse the repository at this point in the history
  • Loading branch information
the-dipsy committed Feb 27, 2024
1 parent 3071006 commit 53ca4a6
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 0 deletions.
Empty file.
15 changes: 15 additions & 0 deletions tests/compiler/component/components/all.pyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""The most complex component.

It contains all possible segments :O
"""

(name: str, age: int, job: str = None, *hobbies: str)

---
intro = " ".join(f"I like {h}." for h in hobbies)
---

name: {{ name | raw }} X
job: {{ job or "making worlds" }}
age: {{ age }}
intro: {{ intro }}
1 change: 1 addition & 0 deletions tests/compiler/component/components/minimal.pyro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 2 additions & 0 deletions tests/compiler/component/components/pure_python.pyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
[{"bar": args[0], "baz": kwargs["foo"]}]
2 changes: 2 additions & 0 deletions tests/compiler/component/components/pure_yaml.pyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- bar: {{ args[0] }}
baz: {{ kwargs["foo"] }}
5 changes: 5 additions & 0 deletions tests/compiler/component/components/python_with_sig.pyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(num: int)

---

num * 2
5 changes: 5 additions & 0 deletions tests/compiler/component/components/yaml_with_sig.pyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(bar: str, baz: int, qux: str = "quux")

- bar: {{ bar }}
baz: {{ baz }}
qux: {{ qux }}
73 changes: 73 additions & 0 deletions tests/compiler/component/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from unittest import TestCase
from pathlib import PosixPath as Path
from pyromaniac import paths
from pyromaniac.compiler.code.errors import InvalidArgumentError
from pyromaniac.compiler.context import Context
from pyromaniac.compiler.component import Component
from pyromaniac.compiler.library import Library


class TestComponent(TestCase):
def setUp(self):
self.comps = Path(__file__).parent.joinpath("components")
lib = Library(self.comps, [Library(paths.stdlib)])
self.ctx = Context(lib, lib.view(), self.comps)

def test_minimal(self):
comp = self.load("minimal")
self.assertIsNone(comp.doc)
self.assertIsNone(comp.python)
self.assertIsNotNone(comp.yaml)
self.assertEqual(comp.execute(self.ctx), {})

def test_pure_yaml(self):
comp = self.load("pure_yaml")
result = comp.execute(self.ctx, "qux", foo="quux")
self.assertEqual(result, [{"bar": "qux", "baz": "quux"}])

def test_pure_python(self):
comp = self.load("pure_python")
self.assertIsNone(comp.doc)
self.assertIsNotNone(comp.python)
self.assertIsNone(comp.yaml)
result = comp.execute(self.ctx, "qux", foo="quux")
self.assertEqual(result, [{"bar": "qux", "baz": "quux"}])

def test_yaml_with_sig(self):
comp = self.load("yaml_with_sig")
result = comp.execute(self.ctx, "fred", 42)
self.assertEqual(result, [{"bar": "fred", "baz": 42, "qux": "quux"}])
result = comp.execute(self.ctx, "fred", baz=69, qux="waldo")
self.assertEqual(result, [{"bar": "fred", "baz": 69, "qux": "waldo"}])

def test_python_with_sig(self):
comp = self.load("python_with_sig")
self.assertEqual(comp.execute(self.ctx, 42), 42 * 2)

def test_all(self):
comp = self.load("all")
self.assertIsNotNone(comp.doc)
self.assertIsNotNone(comp.sig)
self.assertIsNotNone(comp.python)
self.assertIsNotNone(comp.yaml)
args = ("Alice", 42, "programmer", "handstand", "trains")
self.assertEqual(comp.execute(self.ctx, *args), {
"name": "Alice X", "age": 42, "job": "programmer",
"intro": "I like handstand. I like trains.",
})
self.assertEqual(comp.execute(self.ctx, "Bob", 69), {
"name": "Bob X", "age": 69, "job": "making worlds", "intro": "",
})

def test_invalid_argument(self):
comp = self.load("yaml_with_sig")
with self.assertRaises(InvalidArgumentError):
comp.execute(self.ctx)
with self.assertRaises(InvalidArgumentError):
comp.execute(self.ctx, "foo", "bar")
with self.assertRaises(InvalidArgumentError):
comp.execute(self.ctx, "foo", 42, None)

def load(self, path: str, *args, **kwargs) -> Component:
source = self.comps.joinpath(path).with_suffix(".pyro").read_text()
return Component.create(source)

0 comments on commit 53ca4a6

Please sign in to comment.