Skip to content

Commit

Permalink
added testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishMahendra committed Oct 1, 2024
1 parent 4f01758 commit d7b286e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
14 changes: 12 additions & 2 deletions jac/jaclang/runtimelib/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions jac/jaclang/tests/fixtures/dynamic_architype.jac
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions jac/jaclang/tests/test_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d7b286e

Please sign in to comment.