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
);
for
(
"Root"
,
"Top Category 1"
,
"Sub Category 1"
,
"Sub Category 2"
,
"Top Category 2"
) {
like(
$output
,
qr/$_/
);
}
for
(0 .. 4) {
like(
$output
,
qr/val$_/
);
}
like(
$output
,
qr/text\/
css/);
$data
= [{
label
=>
"Root 2"
,
value
=>
'val5'
,
children
=> [
{
label
=>
"Top Category 3"
,
value
=>
'val6'
}
]
},
$data
,
];
$HTML::PopupTreeSelect::TEMPLATE_SRC
=~
s{<tmpl_var label>}
{<tmpl_var label> (<tmpl_loop parent><tmpl_var label></tmpl_loop>)};
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/);
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)/
);
}
}
}
for
(1 .. 7) {
like(
$nocss_output
,
qr/val$_/
);
}
sub
_output_node {
my
(
$self
,
%arg
) =
@_
;
my
@nodes
;
if
(
ref
$arg
{node} eq
'ARRAY'
) {
push
@nodes
, @{
$arg
{node}};
}
else
{
@nodes
= (
$arg
{node});
}
$_
->{value}++
for
@nodes
;
$self
->SUPER::_output_node(
%arg
);
}