XML-API

 view release on metacpan or  search on metacpan

t/03-XML-API.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use Test::Exception;
use Test::Memory::Cycle;
use File::Slurp;

BEGIN {
    use_ok('XML::API');
}

# test the interface

can_ok(
    'XML::API', qw/
      _encoding
      _debug
      _open
      _add
      _raw
      _close
      _element
      _ast
      _parse
      _parse_chunk
      _current
      _set_id
      _goto
      _attrs
      _set_lang
      _langs
      _css
      _cdata
      _javascript
      _as_string
      _fast_string
      /
);

# giving invalid doctype with new
throws_ok {
    XML::API->new( doctype => 'notexist' );
}
qr/^Could not load module 'XML::API::NOTEXIST'/;

# make a root for our tree
my $x = XML::API->new;
isa_ok( $x, 'XML::API' );

$x->_open('e');
is(
    "$x", '<?xml version="1.0" encoding="UTF-8" ?>
<e />', 'e open'
);

$x->_close('e');
is(
    "$x", '<?xml version="1.0" encoding="UTF-8" ?>
<e />', 'e close'
);

is( $x->_fast_string, '<?xml version="1.0" encoding="UTF-8" ?><e />',
    'e close fast' );

$x = XML::API->new;
$x->_open( 'e', -type => 'mytype', 'mycontent' );
is(
    "$x", '<?xml version="1.0" encoding="UTF-8" ?>
<e type="mytype">mycontent</e>', 'e open content'
);

$x->_add(' more content');
$x->_element( 'f', 'f content' );

$x->_close('e');
is(
    "$x", '<?xml version="1.0" encoding="UTF-8" ?>
<e type="mytype">mycontent more content
  <f>f content</f>
</e>', 'e content'
);

$x = XML::API->new;
$x->e_open();

$x->c_raw('<d>content</d>');
is(
    "$x", '<?xml version="1.0" encoding="UTF-8" ?>
<e>
  <c><d>content</d></c>
</e>', 'e c d content'
);

t/03-XML-API.t  view on Meta::CPAN

$x->e_open();
is(
    "$x", '<?xml version="1.0" encoding="UTF-8" ?>
<e />', 'e content'
);

$x->c('content');
is(
    "$x", '<?xml version="1.0" encoding="UTF-8" ?>
<e>
  <c>content</c>
</e>', 'e c content'
);

my $n = XML::API->new;
$n->n_open( -attr => 1 );
$n->n2_open;
$n->_add('content');
$n->n3;

is(
    "$n", '<?xml version="1.0" encoding="UTF-8" ?>
<n attr="1">
  <n2>content
    <n3 />
  </n2>
</n>', 'n content'
);

$x->_add($n);

is(
    "$x", '<?xml version="1.0" encoding="UTF-8" ?>
<e>
  <c>content</c>
  <n attr="1">
    <n2>content
      <n3 />
    </n2>
  </n>
</e>', 'e c n content'
);

$x->p_open;
$x->_add('<raw />');
$x->_raw('<raw />');

is(
    "$x", '<?xml version="1.0" encoding="UTF-8" ?>
<e>
  <c>content</c>
  <n attr="1">
    <n2>content
      <n3 />
    </n2>
  </n>
  <p>&lt;raw /&gt;<raw /></p>
</e>', 'e c n p escaped and raw content'
);

$n->_cdata('my < CDATA');

#warn $n;
#warn $x;

is(
    "$x", '<?xml version="1.0" encoding="UTF-8" ?>
<e>
  <c>content</c>
  <n attr="1">
    <n2>content
      <n3 />
      <![CDATA[my < CDATA]]>
    </n2>
  </n>
  <p>&lt;raw /&gt;<raw /></p>
</e>', 'e c n p escaped and raw content'
);

$x->_parse('<div class="divclass"><p>text</p></div>');

is(
    "$x", '<?xml version="1.0" encoding="UTF-8" ?>
<e>
  <c>content</c>
  <n attr="1">
    <n2>content
      <n3 />
      <![CDATA[my < CDATA]]>
    </n2>
  </n>
  <p>&lt;raw /&gt;<raw />
    <div class="divclass">
      <p>text</p>
    </div>
  </p>
</e>', 'e c n p escaped and raw content with parsed data'
);

$x->_parse_chunk('<div class="divclass"><p>text</p></div>');

is(
    "$x", '<?xml version="1.0" encoding="UTF-8" ?>
<e>
  <c>content</c>
  <n attr="1">
    <n2>content
      <n3 />
      <![CDATA[my < CDATA]]>
    </n2>
  </n>
  <p>&lt;raw /&gt;<raw />
    <div class="divclass">
      <p>text</p>
    </div>
    <div class="divclass">
      <p>text</p>
    </div>
  </p>
</e>', 'e c n p escaped and raw content with parsed data'
);



( run in 1.199 second using v1.01-cache-2.11-cpan-6aa56a78535 )