Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on unknown child node with jump to #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/mg/PAGI/Node/NodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ class NodeController
*/
public function jumpTo($name)
{
if (!isset($this->nodes[$name])) {
throw new NodeException("Unknown node: $name");
}
// Cant make this recursive because php does not support tail
// recursion optimization.
while($name !== false) {

if (!isset($this->nodes[$name])) {
throw new NodeException("Unknown node: '$name''");
}

$node = $this->nodes[$name];
$this->logDebug("Running $name");
$node->run();
Expand Down
92 changes: 65 additions & 27 deletions test/node/Test_NodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,53 @@ function (Node $node) use ($object) {
$this->assertFalse($object->flag);
}

/**
* @test
* @expectedException PAGI\Node\Exception\NodeException
*/
public function cannot_jump_to_unknown_node()
{
$controller = $this->createNodeController(__METHOD__);
$controller->jumpTo(__METHOD__);
}
/**
* @test
* @expectedException PAGI\Node\Exception\NodeException
*/
public function cannot_jump_to_unknown_node()
{
$controller = $this->createNodeController(__METHOD__);
$controller->jumpTo(__METHOD__);
}

/**
* @test
* @expectedException PAGI\Node\Exception\NodeException
*/
public function cannot_jump_to_unknown_childnode()
{
$controller = $this->createNodeController(__METHOD__);

$node = $controller->register(__METHOD__ . 'node');

$controller->registerResult($node->getName())
->jumpAfterEval(function (Node $node)
{
return 'def';
});

$controller->jumpTo($node->getName());
}

/**
* @test
* @expectedException PAGI\Node\Exception\NodeException
*/
public function cannot_jump_to_empty_childnode()
{
$controller = $this->createNodeController(__METHOD__);

$node = $controller->register(__METHOD__ . 'node');

$controller->registerResult($node->getName())
->jumpAfterEval(function (Node $node)
{
// do nothing, happends when we don't read the docs!
});

$controller->jumpTo($node->getName());
}

/**
* @test
Expand Down Expand Up @@ -150,26 +188,26 @@ function (Node $node) use ($object) {
*/
public function can_jump_after_evaluation()
{
$object = new stdClass;
$object->flag = false;
$this->client->onCreateNode(__METHOD__);
$this->client->onCreateNode('can_jump_after_evaluation2');
$controller = $this->createNodeController(__METHOD__);
$controller->registerResult(__METHOD__)
$object = new stdClass;
$object->flag = false;

$this->client->onCreateNode(__METHOD__);
$this->client->onCreateNode('can_jump_after_evaluation2');

$controller = $this->createNodeController(__METHOD__);
$controller->registerResult(__METHOD__)
->onComplete()->jumpAfterEval(function (Node $node) {
return 'can_jump_after_evaluation2';
});
$controller->registerResult('can_jump_after_evaluation2')->onComplete()->execute(
function (Node $node) use ($object) {
$object->flag = true;
}
);
$controller->register(__METHOD__)->saySound('test');
$controller->register('can_jump_after_evaluation2')->saySound('test2');
$controller->jumpTo(__METHOD__);
$this->assertTrue($object->flag);
});
$controller->registerResult('can_jump_after_evaluation2')->onComplete()->execute(
function (Node $node) use ($object) {
$object->flag = true;
}
);
$controller->register(__METHOD__)->saySound('test');
$controller->register('can_jump_after_evaluation2')->saySound('test2');
$controller->jumpTo(__METHOD__);
$this->assertTrue($object->flag);
}

/**
Expand Down