use strict;
use Test::More;
use XML::Struct qw(readXML writeXML simpleXML removeXMLAttr textValues);

is_deeply simpleXML(["root"]), { }, 'simple empty root';
is_deeply simpleXML(["root",{},["text"]]), { }, 'simple empty root with text';
is_deeply simpleXML(["root",{ x => 1, y => 2 },["text"]]), 
    { x => 1, y => 2 }, 
    'simple empty root with text and attributes';

my $xml = readXML(<<'XML');
<root x="1">
  text
  <x>2</x>
  <y a='b'>
    <yy>3</yy>
  </y>
  <x>4</x>
  <z>5</z>
</root>
XML

foreach my $depth (qw(* -1 2)) {
    is_deeply simpleXML($xml, depth => $depth), { 
        x => [ 1, 2, 4 ],
        y => { a => 'b', yy => 3 },
        z => 5,
    }, "simple attributes/children (depth $depth)";
}

my $data = simpleXML($xml, depth => 1);
is_deeply $data->{y}, {
     a => 'b',
     yy => [ [ 'yy', { }, [ '3' ] ] ]
   }, "depth = 1";

$data = simpleXML($xml, depth => 2, root => 'R');
is_deeply $data->{R}->{x}, [
    1, [x=>{},[2]], [x=>{},[4]]
], "depth = 1 with root";

$data = simpleXML($xml, depth => 2, root => 1);
is_deeply $data->{root}->{z}, [ ['z',{},[5]] ], 'depth = 2 with root';

# experimental
$data = simpleXML($xml, depth => 0);
is_deeply $data->{z}, [ ['z',{},[5]] ], 'depth = 0';

#$data = simpleXML($xml, depth => 1, root => 1);



is_deeply simpleXML([ a => { x => 1 } ], root => 1),
    { a => { x => 1 } }, 
    'simple with KeepRoot (root=1)';    

is_deeply simpleXML([ a => { x => 1 } ], root => 'doc'),
    { doc => { x => 1 } }, 
    'simple with custom root';

is_deeply simpleXML([ a => { x => 1 }, [[ x => {}, ['2'] ]]], root => 'doc'),
    { doc => { x => [1,2] } }, 
    'simple with custom root and attributes/values';


is textValues([
        root => {}, [
            "some ",
            [foo => {}, [
                    [ bar => {}, ["text"]]
                ]
            ]
        ]
    ]), "some text", 'textValues (EXPERIMENTAL)';

$xml = <<XML;
<root>
  <foo>text</foo>
  <bar key="value">
    text
    <doz/>
  </bar>
</root>
XML

is_deeply readXML($xml, simple => 1), {
    foo => "text",
    bar => {
        key => "value",
        doz => {}
    }
}, 'hashified with readXML';

is_deeply simpleXML( removeXMLAttr(readXML($xml)), attributes => 0 ), {
    foo => "text",
    bar => {
        doz => {}
    }

}, 'removeXMLAttr';

done_testing;
