From d7b286e0341740dc041e300c013bb5e73ace1eb2 Mon Sep 17 00:00:00 2001 From: Ashish Mahendra Date: Tue, 1 Oct 2024 15:35:16 +0000 Subject: [PATCH] added testcase --- jac/jaclang/runtimelib/machine.py | 14 ++++++++-- .../tests/fixtures/dynamic_architype.jac | 26 +++++++++++++++++++ jac/jaclang/tests/test_language.py | 12 +++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 jac/jaclang/tests/fixtures/dynamic_architype.jac diff --git a/jac/jaclang/runtimelib/machine.py b/jac/jaclang/runtimelib/machine.py index 4a8fc3fea..135196e18 100644 --- a/jac/jaclang/runtimelib/machine.py +++ b/jac/jaclang/runtimelib/machine.py @@ -151,7 +151,11 @@ def update_walker( logger.warning(f"Module {module_name} not found in loaded modules.") return () - def spawn_node(self, node_name: str, attributes: dict) -> NodeArchitype: + def spawn_node( + self, + node_name: str, + attributes: Union[dict, None] = None, + ) -> NodeArchitype: """Spawn a node instance of the given node_name with attributes.""" node_class = self.get_architype("__main__", node_name) if isinstance(node_class, type) and issubclass(node_class, NodeArchitype): @@ -160,10 +164,16 @@ def spawn_node(self, node_name: str, attributes: dict) -> NodeArchitype: else: raise ValueError(f"Node {node_name} not found.") - def spawn_walker(self, walker_name: str, attributes: dict) -> WalkerArchitype: + def spawn_walker( + self, + walker_name: str, + attributes: Union[dict, None] = None, + ) -> WalkerArchitype: """Spawn a walker instance of the given walker_name.""" walker_class = self.get_architype("__main__", walker_name) if isinstance(walker_class, type) and issubclass(walker_class, WalkerArchitype): + if attributes is None: + attributes = {} walker_instance = walker_class(**attributes) return walker_instance else: diff --git a/jac/jaclang/tests/fixtures/dynamic_architype.jac b/jac/jaclang/tests/fixtures/dynamic_architype.jac new file mode 100644 index 000000000..142179a87 --- /dev/null +++ b/jac/jaclang/tests/fixtures/dynamic_architype.jac @@ -0,0 +1,26 @@ +import:py from jaclang.runtimelib.machine {JacMachine} + +walker test_walker { + can visit_nodes with `root entry { + visit [-->](`?test_node); + } +} + +walker child_walker :test_walker: {} + +node test_node { + has value:int; + + can print_value with test_walker entry { + print("Value:", f'{self.value}'); + } +} + +with entry { + for value in range(1, 4) { + root ++> test_node(value=value); + } + node_obj = JacMachine.get().spawn_node('test_node', {'value': 0}); + walker_obj = JacMachine.get().spawn_walker('child_walker'); + root spawn walker_obj; +} diff --git a/jac/jaclang/tests/test_language.py b/jac/jaclang/tests/test_language.py index 6754fd912..559ffa792 100644 --- a/jac/jaclang/tests/test_language.py +++ b/jac/jaclang/tests/test_language.py @@ -1010,6 +1010,18 @@ def test_walker_dynamic_update(self) -> None: bar_file.write(original_content) + def test_dynamic_spawn_architype(self): + """Test that the walker and node can be spawned and behaves as expected.""" + captured_output = io.StringIO() + sys.stdout = captured_output + cli.run(self.fixture_abs_path("dynamic_architype.jac")) + output = captured_output.getvalue().strip() + + expected_outputs = ["Value: 1", "Value: 3", "Value: 2"] + + for expected in expected_outputs: + self.assertIn(expected, output.split("\n")) + def test_object_ref_interface(self) -> None: """Test class method output.""" captured_output = io.StringIO()