File: TestTree.pm

package info (click to toggle)
libdbix-class-tree-nestedset-perl 0.10-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196 kB
  • sloc: perl: 1,672; makefile: 2
file content (49 lines) | stat: -rwxr-xr-x 1,494 bytes parent folder | download | duplicates (3)
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
package TestTree;

use strict;
use warnings;

use Test::More;

use namespace::clean;

sub new {
    my ($class, $args) = @_;
    use Data::Dumper;
    bless { schema => $args->{schema} }, $class;
}

sub schema { shift->{schema} };

sub structure {
    my ($self, $root, $test_str) = @_;

    is($root->lft, 1, "$test_str - [".$root->id."] Root left is correct");
    my $nodes_count = $root->nodes->count;
    is($root->rgt, $nodes_count * 2, "$test_str - [".$root->id."] Correct number of nodes");
    my $level = 0;
    is($root->level, $level, "$test_str - [".$root->id."] Correct level");
    my $index = 1;
    my $current_node = $root;
    while ($index < $root->rgt) {
        my $next_node = $self->schema->resultset('MultiTree')->search({
            -or => [
                rgt => $index,
                lft => $index,
            ],
        });
        is($next_node->count, 1, "$test_str - [$index] has a node");
        my $node = $next_node->next;
        ok($node->lft < $node->rgt, "$test_str - [$index] left < right");
        ok($node->rgt < $root->rgt || $node->lft == 1, "$test_str - [$index] right < root->right");
        is(($node->rgt - $node->lft) % 2, 1, "$test_str - [$index] left/right diff is odd");

            # we expect (right - 1 - left)/2 descendants
            my @descendants = $node->descendants;
            is(($node->rgt - 1 - $node->lft)/2, scalar @descendants, "$test_str - [$index] correct number of descendants");

        $index++;
    }
}

1;