1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 17;
use Test::Exception;
BEGIN {
use_ok('Forest::Tree');
}
my $root = Forest::Tree->new(
node => 'root',
children => [
Forest::Tree->new(
node => '1.0',
children => [
Forest::Tree->new(node => '1.1'),
Forest::Tree->new(node => '1.2'),
]
),
Forest::Tree->new(
node => '2.0',
children => [
Forest::Tree->new(
node => '2.1',
children => [
Forest::Tree->new(node => '2.1.1'),
Forest::Tree->new(node => '2.1.2'),
]
),
]
)
]
);
isa_ok($root, 'Forest::Tree');
my @output;
$root->traverse(sub {
my $t = shift;
isa_ok($t, 'Forest::Tree');
ok($t->has_parent, '... got a parent node');
push @output => [ $t->depth, $t->node, $t->parent->node ]
});
is_deeply(
\@output,
[
[0,'1.0','root'],
[1,'1.1','1.0'],
[1,'1.2','1.0'],
[0,'2.0','root'],
[1,'2.1','2.0'],
[2,'2.1.1','2.1'],
[2,'2.1.2','2.1'],
],
'... the tree was properly initialized'
);
|