Skip to content

Commit

Permalink
node wrapper and node builder test
Browse files Browse the repository at this point in the history
  • Loading branch information
dakujem committed Jan 19, 2024
1 parent bab07ab commit 089ee19
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/iterators.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Environment::setup();
$to->setParent($from);
};

// Tree from here:
// https://en.wikipedia.org/wiki/Tree_traversal
$root = $f;
$edge($f, $b);
$edge($b, $a);
Expand Down
90 changes: 90 additions & 0 deletions tests/nodes.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

use Dakujem\Oliva\Node;
use Dakujem\Oliva\Simple\NodeBuilder;
use Dakujem\Oliva\Simple\TreeWrapper;
use Tester\Assert;
use Tester\Environment;

require_once __DIR__ . '/../vendor/autoload.php';
Environment::setup();

(function () {
$builder = new NodeBuilder(fn(mixed $data) => new Node($data));

// Tree from here:
// https://en.wikipedia.org/wiki/Tree_traversal
$root = $builder->node('F', [
$builder->node('B', [
$builder->node('A'),
$builder->node('D', [
$builder->node('C'),
$builder->node('E'),
]),
]),
$builder->node('G', [
$builder->node('I', [
$builder->node('H'),
]),
]),
]);

// pre-order
$str = [];
foreach ($root as $node) {
$str[] = $node->data();
}
Assert::same('F,B,A,D,C,E,G,I,H', implode(',', $str));


// Tree from here:
// https://en.wikipedia.org/wiki/Tree_traversal
$data = [
'data' => 'F',
'children' => [
[
'data' => 'B',
'children' => [
[
'data' => 'A',
],
[
'data' => 'D',
'children' => [
[
'data' => 'C',
],
[
'data' => 'E',
],
],
],
],
],
[
'data' => 'G',
'children' => [
[
'data' => 'I',
'children' => [
[
'data' => 'H',
],
],
],
],
],
],
];
$wrapper = new TreeWrapper(fn(array $raw) => new Node($raw['data']), fn(array $raw) => $raw['children'] ?? null);
$root = $wrapper->wrap($data);

// pre-order
$str = [];
foreach ($root as $node) {
$str[] = $node->data();
}
Assert::same('F,B,A,D,C,E,G,I,H', implode(',', $str));
})();

0 comments on commit 089ee19

Please sign in to comment.