Skip to content

Commit a825923

Browse files
committed
Improve test code coverage
1 parent 21f2847 commit a825923

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

tests/iterators.phpt

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,30 +220,50 @@ require_once __DIR__ . '/setup.php';
220220
(function () {
221221
$root = Preset::wikiTree();
222222

223+
//
223224
// level-order (?), leaves only (the default)
224-
$str = [];
225-
foreach (new RecursiveIteratorIterator(new Native($root)) as $node) {
225+
//
226+
227+
$str = $keys = [];
228+
foreach (new RecursiveIteratorIterator(new Native($root)) as $key => $node) {
226229
$str[] = $node->data();
230+
$keys[] = $key;
227231
}
228232
Assert::same('A,C,E,H', implode(',', $str));
229-
$str = [];
230-
foreach (new RecursiveIteratorIterator(new Native($root), RecursiveIteratorIterator::LEAVES_ONLY) as $node) {
233+
Assert::same('0,0,1,0', implode(',', $keys));
234+
235+
$str = $keys = [];
236+
foreach (new RecursiveIteratorIterator(new Native($root), RecursiveIteratorIterator::LEAVES_ONLY) as $key => $node) {
231237
$str[] = $node->data();
238+
$keys[] = $key;
232239
}
233240
Assert::same('A,C,E,H', implode(',', $str));
241+
Assert::same('0,0,1,0', implode(',', $keys));
234242

243+
//
235244
// pre-order, all nodes
236-
$str = [];
237-
foreach (new RecursiveIteratorIterator(new Native($root), RecursiveIteratorIterator::SELF_FIRST) as $node) {
245+
//
246+
247+
$str = $keys = [];
248+
foreach (new RecursiveIteratorIterator(new Native($root), RecursiveIteratorIterator::SELF_FIRST) as $key => $node) {
238249
$str[] = $node->data();
250+
$keys[] = $key;
239251
}
252+
// F,B,A,D,C,E,G,I,H is the iteration order of nodes, which corresponds to the following child indexes (iteration keys):
253+
// 0,0,0,1,0,1,1,0,0
240254
Assert::same('F,B,A,D,C,E,G,I,H', implode(',', $str));
255+
Assert::same('0,0,0,1,0,1,1,0,0', implode(',', $keys));
241256

257+
//
242258
// post-order, all nodes
243-
$str = [];
244-
foreach (new RecursiveIteratorIterator(new Native($root), RecursiveIteratorIterator::CHILD_FIRST) as $node) {
259+
//
260+
261+
$str = $keys = [];
262+
foreach (new RecursiveIteratorIterator(new Native($root), RecursiveIteratorIterator::CHILD_FIRST) as $key => $node) {
245263
$str[] = $node->data();
264+
$keys[] = $key;
246265
}
247266
Assert::same('A,C,E,D,B,H,I,G,F', implode(',', $str));
267+
Assert::same('0,0,1,1,0,0,0,1,0', implode(',', $keys));
248268
})();
249269

tests/traversal-private.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Dakujem\Test;
6+
7+
use Dakujem\Oliva\Iterator\Traversal;
8+
use Error;
9+
use Tester\Assert;
10+
11+
require_once __DIR__ . '/setup.php';
12+
13+
(function () {
14+
// This test exists for code coverage purposes only... ¯\_(ツ)_/¯
15+
// We might remove the private constructor constraint as well.
16+
Assert::throws(function () {
17+
/** @noinspection */
18+
new Traversal();
19+
}, Error::class, 'Call to private Dakujem\Oliva\Iterator\Traversal::__construct() from global scope');
20+
})();

tests/tree.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,12 @@ require_once __DIR__ . '/setup.php';
126126
Assert::same(['original' => $node], $parent->children());
127127
})();
128128

129+
(function () {
130+
$parent = new Node(null);
131+
$node = new NotMovable(null);
132+
133+
Assert::throws(function () use ($parent, $node) {
134+
Tree::linkChildren($parent, [$node]);
135+
}, NodeNotMovable::class, 'Encountered a non-movable node while manipulating a tree.');
136+
})();
137+

0 commit comments

Comments
 (0)