Skip to content

Commit

Permalink
test base node behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
dakujem committed Jan 27, 2024
1 parent 8840a77 commit a5d4190
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
129 changes: 129 additions & 0 deletions tests/nodes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace Dakujem\Test;
use Dakujem\Oliva\Node;
use Dakujem\Oliva\Simple\NodeBuilder;
use Dakujem\Oliva\Simple\TreeWrapper;
use Dakujem\Oliva\Tree;
use Dakujem\Oliva\TreeNodeContract;
use Exception;
use Tester\Assert;

require_once __DIR__ . '/setup.php';
Expand Down Expand Up @@ -81,3 +84,129 @@ require_once __DIR__ . '/setup.php';
// pre-order
Assert::same('FBADCEGIH', TreeTesterTool::flatten($root));
})();


// Test linking/unlinking of nodes via Tree utility.
(function () {
$a = new Node('A');
$b = new Node('B');
$c = new Node('C');
$d = new Node('D');
$e = new Node('E');

Tree::linkChildren($a, [$b, $c]);
Tree::linkChildren($b, [$d, $e]);

Assert::same('ABDEC', TreeTesterTool::flatten($a));
assert::same($a, $b->root());
assert::same($a, $c->root());
assert::same($a, $d->root());
assert::same($a, $e->root());

Assert::true($a->hasChild(0));
Assert::true($a->hasChild(1));
Assert::true($a->hasChild($b));
Assert::true($a->hasChild($c));
Assert::false($a->hasChild($d));
Assert::false($a->hasChild(2));

$hasRun = false;
Assert::same($b, $e->parent());
Assert::same([$d, $e], $b->children());
Assert::same([], $d->children());
Tree::linkChildren($d, $e, function (TreeNodeContract $originalParent) use (&$hasRun, $b) {
$hasRun = true;
// B is the original parent of E
Assert::same($b, $originalParent);
});
Assert::true($hasRun);
Assert::same('ABDEC', TreeTesterTool::flatten($a));
Assert::same($d, $e->parent());
Assert::same([$d], $b->children());
Assert::same([$e], $d->children());
assert::same($a, $e->root());

Tree::unlinkChildren($a);
Assert::same('A', TreeTesterTool::flatten($a));
Assert::true($a->isRoot());
Assert::true($a->isLeaf());
Assert::same([], $a->children());
Assert::true($b->isRoot());
Assert::true($c->isRoot());

// When unlinking node that is already a root, null is returned.
// Otherwise, the original parent is returned.
Assert::same(null, Tree::unlink($b));
Tree::link($b, $a, 'my-key');
Assert::false($a->isLeaf());
Assert::false($b->isRoot());
Assert::true($a->hasChild($b));
Assert::true($a->hasChild('my-key'));
Assert::false($a->hasChild(0));
Assert::same($a, Tree::unlink($b));
Assert::true($b->isRoot());
})();


// A data node can be filled with any data.
(function () {
$a = new Node('A');
Assert::same('A', $a->data());
$a->fill('foo');
Assert::same('foo', $a->data());
$a->fill(null);
Assert::same(null, $a->data());
$a->fill($object = new Exception());
Assert::same($object, $a->data());
})();



(function () {
$tree = Preset::wikiTree();
$raw = json_decode(json_encode($tree), true);
Assert::same([
'data' => 'F',
'children' => [
[
'data' => 'B',
'children' => [
[
'data' => 'A',
'children' => [],
],
[
'data' => 'D',
'children' => [
[
'data' => 'C',
'children' => [],
],
[
'data' => 'E',
'children' => [],
],
],
],
],
],
[
'data' => 'G',
'children' => [
[
'data' => 'I',
'children' => [
[
'data' => 'H',
'children' => [],
],
],
],
],
],
],
], $raw);
})();



8 changes: 8 additions & 0 deletions tests/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ final class Preset
/**
* Returns manually built tree from Wikipedia:
* @link https://en.wikipedia.org/wiki/Tree_traversal
*
* F
* |
* B ------+------ G
* | |
* A --+-- D I
* | |
* C --+-- E H
*/
public static function wikiTree(): Node
{
Expand Down

0 comments on commit a5d4190

Please sign in to comment.