From 53ca4a630f38039f928dc89298fad3e6aa5c2f5d Mon Sep 17 00:00:00 2001 From: the-dipsy Date: Tue, 27 Feb 2024 23:21:23 +0100 Subject: [PATCH] test: add tests for pyromaniac component class --- tests/compiler/component/__init__.py | 0 tests/compiler/component/components/all.pyro | 15 ++++ .../component/components/minimal.pyro | 1 + .../component/components/pure_python.pyro | 2 + .../component/components/pure_yaml.pyro | 2 + .../component/components/python_with_sig.pyro | 5 ++ .../component/components/yaml_with_sig.pyro | 5 ++ tests/compiler/component/test.py | 73 +++++++++++++++++++ 8 files changed, 103 insertions(+) create mode 100644 tests/compiler/component/__init__.py create mode 100644 tests/compiler/component/components/all.pyro create mode 100644 tests/compiler/component/components/minimal.pyro create mode 100644 tests/compiler/component/components/pure_python.pyro create mode 100644 tests/compiler/component/components/pure_yaml.pyro create mode 100644 tests/compiler/component/components/python_with_sig.pyro create mode 100644 tests/compiler/component/components/yaml_with_sig.pyro create mode 100644 tests/compiler/component/test.py diff --git a/tests/compiler/component/__init__.py b/tests/compiler/component/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/compiler/component/components/all.pyro b/tests/compiler/component/components/all.pyro new file mode 100644 index 0000000..a67f32b --- /dev/null +++ b/tests/compiler/component/components/all.pyro @@ -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 }} \ No newline at end of file diff --git a/tests/compiler/component/components/minimal.pyro b/tests/compiler/component/components/minimal.pyro new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/tests/compiler/component/components/minimal.pyro @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/compiler/component/components/pure_python.pyro b/tests/compiler/component/components/pure_python.pyro new file mode 100644 index 0000000..f976f2c --- /dev/null +++ b/tests/compiler/component/components/pure_python.pyro @@ -0,0 +1,2 @@ +--- +[{"bar": args[0], "baz": kwargs["foo"]}] diff --git a/tests/compiler/component/components/pure_yaml.pyro b/tests/compiler/component/components/pure_yaml.pyro new file mode 100644 index 0000000..8d0665f --- /dev/null +++ b/tests/compiler/component/components/pure_yaml.pyro @@ -0,0 +1,2 @@ +- bar: {{ args[0] }} + baz: {{ kwargs["foo"] }} \ No newline at end of file diff --git a/tests/compiler/component/components/python_with_sig.pyro b/tests/compiler/component/components/python_with_sig.pyro new file mode 100644 index 0000000..dc5e1c4 --- /dev/null +++ b/tests/compiler/component/components/python_with_sig.pyro @@ -0,0 +1,5 @@ +(num: int) + +--- + +num * 2 \ No newline at end of file diff --git a/tests/compiler/component/components/yaml_with_sig.pyro b/tests/compiler/component/components/yaml_with_sig.pyro new file mode 100644 index 0000000..2dd0ce9 --- /dev/null +++ b/tests/compiler/component/components/yaml_with_sig.pyro @@ -0,0 +1,5 @@ +(bar: str, baz: int, qux: str = "quux") + +- bar: {{ bar }} + baz: {{ baz }} + qux: {{ qux }} \ No newline at end of file diff --git a/tests/compiler/component/test.py b/tests/compiler/component/test.py new file mode 100644 index 0000000..e1f4ed6 --- /dev/null +++ b/tests/compiler/component/test.py @@ -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)