BEGIN { use_ok(
'XML::PP'
) }
my
$xml_pp
= new_ok(
'XML::PP'
);
my
$input1
= {
name
=>
'note'
,
children
=> [
{
name
=>
'to'
,
children
=> [ {
text
=>
'Tove'
} ] },
{
name
=>
'from'
,
children
=> [ {
text
=>
'Jani'
} ] },
{
name
=>
'heading'
,
children
=> [ {
text
=>
'Reminder'
} ] },
{
name
=>
'body'
,
children
=> [ {
text
=>
'Don\'t forget me this weekend!'
} ] },
],
};
my
$expected1
= {
note
=> {
to
=>
'Tove'
,
from
=>
'Jani'
,
heading
=>
'Reminder'
,
body
=>
'Don\'t forget me this weekend!'
,
}
};
is_deeply(
$xml_pp
->collapse_structure(
$input1
),
$expected1
,
'Basic $xml_pp->collapse test'
);
my
$input2
= {
name
=>
'note'
,
children
=> [
{
name
=>
'body'
,
children
=> [
{
name
=>
'p'
,
children
=> [ {
text
=>
'Paragraph 1'
} ] },
{
name
=>
'footer'
,
children
=> [ {
text
=>
'Goodbye'
} ] },
]
},
]
};
my
$expected2
= {
note
=> {
body
=> {
p
=>
'Paragraph 1'
,
footer
=>
'Goodbye'
,
}
}
};
diag(Data::Dumper->new([
$xml_pp
->collapse_structure(
$input2
)])->Dump())
if
(
$ENV
{
'TEST_VERBOSE'
});
is_deeply(
$xml_pp
->collapse_structure(
$input2
),
$expected2
,
'Nested $xml_pp->collapse test'
);
my
$input3
= {
name
=>
'data'
,
children
=> [
{
name
=>
'item'
,
children
=> [ {
text
=>
'One'
} ] },
{
name
=>
'item'
,
children
=> [ {
text
=>
'Two'
} ] },
{
name
=>
'item'
,
children
=> [ {
text
=>
'Three'
} ] },
]
};
my
$expected3
= {
data
=> {
item
=> [
'One'
,
'Two'
,
'Three'
]
}
};
is_deeply(
$xml_pp
->collapse_structure(
$input3
),
$expected3
,
'Multiple same-name tags as array'
);
my
$input4
= {
name
=>
'note'
,
children
=> [
{
name
=>
'to'
,
children
=> [ {
text
=>
''
} ] },
{
name
=>
'from'
,
children
=> [] },
{
name
=>
'body'
,
children
=> [ {
text
=>
'Content'
} ] },
]
};
my
$expected4
= {
note
=> {
body
=>
'Content'
,
}
};
is_deeply(
$xml_pp
->collapse_structure(
$input4
),
$expected4
,
'Skipping empty nodes'
);
done_testing();