File: internal.t

package info (click to toggle)
libur-perl 0.470%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,192 kB
  • sloc: perl: 61,814; javascript: 255; xml: 108; sh: 13; makefile: 9
file content (186 lines) | stat: -rw-r--r-- 7,166 bytes parent folder | download | duplicates (5)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env perl

use Test::More;
use File::Basename;
use lib File::Basename::dirname(__FILE__)."/../../lib";
use lib File::Basename::dirname(__FILE__)."/..";
use lib File::Basename::dirname(__FILE__)."/test_namespace/";
use UR;
use strict;
use warnings;

use Cwd;

plan tests => 38;

our $NAMESPACE = 'Testing';
my $base_dir = Cwd::abs_path(File::Basename::dirname(__FILE__));
our %expected_class_data = (
                Testing => {
                    name  => 'Testing',
                    is    => ['UR::Namespace'],
                    relpath  => 'Testing.pm',
                    file  => 'Testing.pm',
                },
                'Testing::Something' => {
                    name  => 'Testing::Something',
                    is    => ['UR::Object'],
                    relpath  => 'Testing/Something.pm',
                    file  => 'Something.pm',
                },
                'Testing::Something::SubClass1' => {
                    name  => 'Testing::Something::SubClass1',
                    is    => ['Testing::Something'],
                    relpath  => 'Testing/Something/SubClass1.pm',
                    file  => 'SubClass1.pm',
                },
                'Testing::Something::SubClass2' => {
                    name  => 'Testing::Something::SubClass2',
                    is    => ['Testing::Something'],
                    relpath  => 'Testing/Something/SubClass2.pm',
                    file  => 'SubClass2.pm',
                },
                'Testing::Color' => {
                    name  => 'Testing::Color',
                    is    => ['UR::Object'],
                    relpath  => 'Testing/Color.pm',
                    file  => 'Color.pm',
                },
            );

my $cmd = UR::Namespace::Command::Sys::ClassBrowser->create(
                namespace_name => $NAMESPACE,
            );
ok($cmd, 'Created ClassBrowser command');

test_name_cache($cmd);
test_directory_tree_cache($cmd);
test_name_tree_cache($cmd);
test_inheritance_tree_cache($cmd);

sub test_name_cache {
    my $cmd = shift;

    my $by_class_name = $cmd->_generate_class_name_cache('Testing');
    strip_ids(values %$by_class_name); # IDs are random UUIDs

    is_deeply($by_class_name, \%expected_class_data, '_generate__class_name_cache');
}

sub test_directory_tree_cache {
    my $cmd = shift;

    ok( $cmd->_load_class_info_from_modules_on_filesystem($NAMESPACE) ,'_load_class_info_from_modules_on_filesystem');

    my $tree = $cmd->directory_tree_cache($NAMESPACE);
    ok($tree, 'Get directory tree cache');
    ok($tree->has_children, 'Tree root has children');
    is($tree->name, $NAMESPACE, 'Tree root name');
    my $data = strip_ids($tree->data);
    is_deeply($tree->data, $expected_class_data{$NAMESPACE}, 'Tree root data');

    my $children = $tree->children();
    is(scalar(@$children), 3, 'Root has 3 children');

    foreach ( { class => 'Testing::Color', file => 'Color.pm'},
              { class => 'Testing::Something', file => 'Something.pm'} ) {
        is_deeply($tree->get_child($_->{file})->data, $expected_class_data{$_->{class}}, "get_child $_->{file}");
        ok(! $tree->get_child($_->{file})->has_children, "$_->{file} has no children");
    }

    ok(! $tree->get_child('not there'), 'Getting non-existent child returns nothing');

    $tree = $tree->get_child('Something');
    ok($tree, 'Get child directory "Something"');
    ok($tree->has_children, 'directory "Something" has children');
    foreach ( { class => 'Testing::Something::SubClass1', file => 'SubClass1.pm' },
              { class => 'Testing::Something::SubClass2', file => 'SubClass2.pm' } ) {
        is_deeply($tree->get_child($_->{file})->data, $expected_class_data{$_->{class}}, "get_child $_->{file}");
        ok(! $tree->get_child($_->{file})->has_children, "$_->{file} has no children");
    }
}

sub test_name_tree_cache {
    my $cmd = shift;

    ok( $cmd->_load_class_info_from_modules_on_filesystem($NAMESPACE) ,'_load_class_info_from_modules_on_filesystem');
    my $tree = $cmd->name_tree_cache($NAMESPACE);
    ok($tree, 'Get name tree cache');
    ok($tree->has_children, 'Tree root has children');
    is($tree->name, $NAMESPACE, 'Tree root name');
    my $data = strip_ids($tree->data);
    is_deeply($tree->data, $expected_class_data{$NAMESPACE}, 'Tree root data');

    my $children = $tree->children();
    is(scalar(@$children), 2, 'Root has 2 children');

    is_deeply($tree->get_child('Color')->data, $expected_class_data{'Testing::Color'}, 'get child Color');
    ok(! $tree->get_child('Color')->has_children, 'Color has no children');

    $tree = $tree->get_child('Something');
    is_deeply($tree->data, $expected_class_data{'Testing::Something'}, 'Get child "Something"');
    $children = $tree->children;
    is(scalar(@$children), 2, '"Something" has 2 children');
    foreach my $name ( qw( SubClass1 SubClass2 )) {
        my $class = 'Testing::Something::'.$name;
        is_deeply($tree->get_child($name)->data, $expected_class_data{$class}, "Get child $name");
        ok(! $tree->get_child($name)->has_children, "$name has no children");
    }
    
}

sub test_inheritance_tree_cache {
    my $cmd = shift;

    ok( $cmd->_load_class_info_from_modules_on_filesystem($NAMESPACE) ,'_load_class_info_from_modules_on_filesystem');
    my $tree = $cmd->inheritance_tree_cache($NAMESPACE);
    ok($tree, 'Get inheritance tree cache');
    ok($tree->has_children, 'Tree root has children');
    is($tree->name, 'UR::Object', 'UR::Object is the tree root');

    my $visit;
    $visit = sub {
        my $tree = shift;
        return { name => $tree->name,
                 children => [  map { $visit->($_) }
                                sort { $a->name cmp $b->name }
                                @{$tree->children} ],
                };
    };
    my $got_inheritance = $visit->($tree);
            
    my $expected_inheritance = {
            name => 'UR::Object',
            children => [
                    {   name => 'Testing::Color',
                        children => [],
                    },
                    {   name => 'Testing::Something',
                        children => [
                            {   name => 'Testing::Something::SubClass1',
                                children => [],
                            },
                            {   name => 'Testing::Something::SubClass2',
                                children => [],
                            }
                        ],
                    },
                    {   name => 'UR::Singleton',
                        children => [
                            {   name => 'UR::Namespace',
                                children => [
                                    {   name => 'Testing',
                                        children => [],
                                    },
                                ],
                            },
                        ],
                    }
            ]
        };
    is_deeply($got_inheritance, $expected_inheritance, 'Inheritance tree');
}

sub strip_ids {
    delete $_->{id} foreach (@_);
}