File: 07tree-iter.t

package info (click to toggle)
libhtml-widgets-navmenu-perl 1.1000-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 636 kB
  • sloc: perl: 8,051; makefile: 9
file content (295 lines) | stat: -rw-r--r-- 6,105 bytes parent folder | download
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/perl -w

package MyIter;

use strict;

use parent 'HTML::Widgets::NavMenu::Tree::Iterator';

__PACKAGE__->mk_accessors(
    qw(
        _results
        _data
    )
);

sub _init
{
    my $self = shift;

    $self->SUPER::_init(@_);

    my $args = shift;

    $self->_data( $args->{'data'} );

    $self->_results( [] );

    return 0;
}

sub append
{
    my $self = shift;
    push @{ $self->_results() }, @_;
    return 0;
}

sub get_initial_node
{
    my $self = shift;
    return $self->_data();
}

sub get_node_subs
{
    my ( $self, $args ) = @_;

    my $node = $args->{'node'};

    return exists( $node->{'childs'} )
        ? [ @{ $node->{'childs'} } ]
        : [];
}

sub get_new_accum_state
{
    my ( $self, $args ) = @_;
    my $parent_item = $args->{'item'};
    my $node        = $args->{'node'};

    if ( !defined($parent_item) )
    {
        return $node->{'accum'};
    }

    my $prev_state = $parent_item->_accum_state();

    return ( $node->{'accum'} || $prev_state );
}

sub node_start
{
    my $self     = shift;
    my $top_item = $self->top;
    my $node     = $self->top->_node();

    $self->append(
        join( "-", "Start", $node->{'id'}, $top_item->_accum_state ) );
}

sub node_end
{
    my $self = shift;
    my $node = $self->top->_node();

    $self->append( join( "-", "End", $node->{'id'} ) );
}

sub node_should_recurse
{
    my $self = shift;
    my $node = $self->top->_node();
    return $node->{'recurse'};
}

1;

package MyIterComplexSubs;

use vars qw(@ISA);

@ISA = qw(MyIter);

sub get_node_from_sub
{
    my $self = shift;
    my $args = shift;

    my $item = $args->{'item'};
    my $sub  = $args->{'sub'};
    my $node = $item->_node();

    return $node->{'subs_db'}->{$sub};
}

1;

package main;

use Test::More tests => 4;

use strict;

sub test_traverse
{
    my ( $data, $expected, $test_name, $class ) = (@_);
    $class ||= "MyIter";
    my $traverser = $class->new(
        {
            'data' => $data
        },
    );

    $traverser->traverse();

    is_deeply( $traverser->_results(), $expected, $test_name );
}

{
    my $data = {
        'id'      => "A",
        'recurse' => 1,
        'accum'   => "one",
        'childs'  => [
            {
                'id'    => "B",
                'accum' => "two",
            },
            {
                'id'      => "C",
                'recurse' => 1,
                'childs'  => [
                    {
                        'id' => "FG",
                    },
                ],
            },
        ],
    };
    my @expected = (
        "Start-A-one",  "Start-B-two", "End-B", "Start-C-one",
        "Start-FG-one", "End-FG",      "End-C", "End-A"
    );

    # TEST
    test_traverse( $data, \@expected,
        "Simple example for testing the Tree traverser." );
}

# This test checks that the should_recurse predicate is honoured.
{
    my $data = {
        'id'      => "A",
        'recurse' => 1,
        'accum'   => "one",
        'childs'  => [
            {
                'id'    => "B",
                'accum' => "two",
            },
            {
                'id'      => "C",
                'recurse' => 0,
                'childs'  => [
                    {
                        'id' => "FG",
                    },
                ],
            },
        ],
    };
    my @expected = (
        "Start-A-one", "Start-B-two", "End-B", "Start-C-one",
        "End-C",       "End-A"
    );

    # TEST
    test_traverse( $data, \@expected, "Example with recurse = 0" );
}

{
    my $data = {
        'id'      => "A",
        'recurse' => 1,
        'accum'   => "one",
        'childs'  => [
            {
                'id'    => "B",
                'accum' => "two",
            },
            {
                'id'      => "C",
                'recurse' => 0,
                'childs'  => [
                    {
                        'id' => "FG",
                    },
                    {
                        'id'      => "E",
                        'recurse' => 0,
                        'childs'  => [
                            {
                                'id' => "Y",
                            },
                            {
                                'id' => "Z",
                            },
                        ],
                    },
                ],
            },
            {
                'id'      => "AGH",
                'recurse' => 1,
                'accum'   => "three",
                'childs'  => [
                    {
                        'id'      => "MON",
                        'recurse' => 0,
                        'accum'   => "four",
                        'childs'  => [
                            {
                                'id'      => "HELLO",
                                'recurse' => 1,
                            },
                        ],
                    },
                    {
                        'id'      => "KOJ",
                        'recurse' => 1,
                    },
                ],
            }
        ],
    };
    my @expected = (
        "Start-A-one",     "Start-B-two",
        "End-B",           "Start-C-one",
        "End-C",           "Start-AGH-three",
        "Start-MON-four",  "End-MON",
        "Start-KOJ-three", "End-KOJ",
        "End-AGH",         "End-A"
    );

    # TEST
    test_traverse( $data, \@expected,
        "Example with lots of weird combinations" );
}

{
    my $data = {
        'id'      => "A",
        'recurse' => 1,
        'accum'   => "one",
        'childs'  => [qw(hello good)],
        'subs_db' => {
            'hello' => {
                'id'    => "BOK",
                'accum' => "two",
            },
            'good' => {
                'id' => "C",
            },
        },
    };

    my @expected = (
        "Start-A-one", "Start-BOK-two", "End-BOK", "Start-C-one",
        "End-C",       "End-A"
    );

    # TEST
    test_traverse( $data, \@expected, "Example with complex sub resolution",
        "MyIterComplexSubs" );
}