Skip to content

Commit

Permalink
fixing unknown node
Browse files Browse the repository at this point in the history
  • Loading branch information
Grummfy committed Jan 20, 2015
1 parent c18c778 commit 6ddbc0c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 30 deletions.
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

0 comments on commit 6ddbc0c

Please sign in to comment.