File: write-mixed.t

package info (click to toggle)
libxml-struct-perl 0.27-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 220 kB
  • sloc: perl: 600; xml: 17; sh: 6; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 1,507 bytes parent folder | download | duplicates (2)
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
use strict;
use Test::More;
use XML::Struct::Writer;
use Encode;

my ($struct, $xml);

sub write_xml {
    my $str = ""; 
    my $args = ref $_[-1] ? pop : { xmldecl => 0 }; 
    my $writer = XML::Struct::Writer->new( to => \$str, %$args );
    $writer->write(@_);
    $str;
}

$struct = { foo => [0], bar => [], doz => ["Hello","World"], x => undef };
$xml    = "<greet><bar/><doz>Hello</doz><doz>World</doz><foo>0</foo></greet>\n";
is write_xml($struct, "greet"), $xml, "simple format (with root)";

$struct = { foo => { bar => { doz => {} } } };
$xml    = "<root><foo><bar><doz/></bar></foo></root>\n";
is write_xml($struct, undef), $xml, "simple format (no root)";
is write_xml($struct, undef, { attributes => 0, xmldecl => 0 }), $xml, 
    "simple format (no root, no attributes)";

$struct = [ micro => {}, [ { xml => 1 } ] ];
$xml    = "<micro><xml>1</xml></micro>\n"; 
is write_xml($struct, undef), $xml, "mixed format (simple in micro)";

$struct = [ A => [ " ", { B => 1 }, "  ", { B => [] } ] ];
$xml    = "<A> <B>1</B>  <B/></A>\n";
is write_xml($struct, undef), $xml, "mixed format (simple in micro)";

$struct = { a => [ [ b => { a => 1 } ], [ c => { a => 1 }, ['d'] ] ] };
$xml    = "<root><a><b a=\"1\"/></a><a><c a=\"1\">d</c></a></root>\n";
is write_xml($struct, undef), $xml, "mixed format (micro in simple)";

$xml    = "<root><a><b/></a><a><c>d</c></a></root>\n";
is write_xml($struct, undef, { attributes => 0, xmldecl => 0 }), $xml, 
    "mixed format, no attributes";

done_testing;