File: test.pl

package info (click to toggle)
libhtml-popuptreeselect-perl 1.6-7.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 160 kB
  • sloc: perl: 504; makefile: 2
file content (117 lines) | stat: -rw-r--r-- 3,524 bytes parent folder | download | duplicates (6)
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
use Test::More qw(no_plan);
use_ok('HTML::PopupTreeSelect');

my $data = { label    => "Root",
             value    => 'val0',
             children => [
                          { label    => "Top Category 1",
                            value       => 'val1',
                            children => [
                                         { label => "Sub Category 1",
                                           value    => 'val2'
                                         },
                                         { label => "Sub Category 2",
                                           value    => 'val3'
                                         },
                                        ],
                          },
                          { label  => "Top Category 2",
                              value     => 'val4',
                          },
                         ],
           };

my $select = HTML::PopupTreeSelect->new(name => 'category',
                                        data => $data,
                                        title => 'Select a Category',
                                        button_label => 'Choose');
isa_ok($select, 'HTML::PopupTreeSelect');

my $output = $select->output();
ok($output);

# see if all the labels made it
for ("Root","Top Category 1", "Sub Category 1",
     "Sub Category 2", "Top Category 2") {
    like($output, qr/$_/);
}

# see if all the values made it
for (0 .. 4) {
    like($output, qr/val$_/);
}

# this one should have CSS
like($output, qr/text\/css/);

# add a second layer
$data = [{ label    => "Root 2",
           value    => 'val5',
           children => [
                        { label => "Top Category 3",
                          value => 'val6'
                        }
                       ]
	 }, $data,
        ];

# test modifying template src, and use of parent
$HTML::PopupTreeSelect::TEMPLATE_SRC =~
    s{<tmpl_var label>}
     {<tmpl_var label> (<tmpl_loop parent><tmpl_var label></tmpl_loop>)};

# make one without CSS
my $nocss = HTML::PopupTreeSelect::DummySub->new(name => 'category',
                                       data => $data,
                                       title => 'Select a Category',
                                       button_label => 'Choose',
                                       parent_var => 1,
                                       include_css => 0);
isa_ok($nocss, 'HTML::PopupTreeSelect::DummySub');
my $nocss_output = $nocss->output;
ok($nocss_output);
ok($nocss_output !~ qr/text\/css/);

# let's look for the parent labels, three levels deep
for my $first (@$data) {
    my $label1 = $first->{label};
    like($nocss_output, qr/\Q$label1 ()/);

    for my $second (@{$first->{children}}) {
        my $label2 = $second->{label};
        like($nocss_output, qr/\Q$label2 ($label1)/);

        for my $third (@{$second->{children}}) {
            my $label3 = $third->{label};
            like($nocss_output, qr/\Q$label3 ($label2)/);
        }

    }

}

# see if all the values made it (we increment them in subclass method)
for (1 .. 7) {
    like($nocss_output, qr/val$_/);
}

# test subclassing
package HTML::PopupTreeSelect::DummySub;

use base 'HTML::PopupTreeSelect';

sub _output_node {
    my ($self, %arg) = @_;

    my @nodes;
    if (ref $arg{node} eq 'ARRAY') {
        push @nodes, @{$arg{node}};
    } else {
        @nodes = ($arg{node});
    }

    # increment for the heck of it
    $_->{value}++ for @nodes;

    $self->SUPER::_output_node(%arg);
}