#!/usr/bin/perl -w
use strict;
use warnings;
use Test::More;
use XML::LibXML;
BEGIN{
if (1000*$] < 5008) {
plan skip_all => "Reader interface only supported in Perl >= 5.8";
exit;
} elsif (!XML::LibXML::HAVE_READER()) {
plan skip_all => "Reader not supported in this libxml2 build";
exit;
} else {
plan tests => 100;
}
use_ok('XML::LibXML::Reader');
};
my $file = "test/textReader/countries.xml";
{
my $reader = XML::LibXML::Reader->new(location => $file, {expand_entities => 1});
isa_ok($reader, "XML::LibXML::Reader");
is($reader->read, 1, "read");
is($reader->byteConsumed, 488, "byteConsumed");
is($reader->attributeCount, 0, "attributeCount");
is($reader->baseURI, $file, "baseURI");
is($reader->encoding, 'UTF-8', "encoding");
is($reader->localName, 'countries', "localName");
is($reader->name, 'countries', "name");
is($reader->prefix, undef, "prefix");
is($reader->value, undef, "value");
is($reader->xmlLang, undef, "xmlLang");
is($reader->xmlVersion, '1.0', "xmlVersion");
$reader->read;
$reader->read;
$reader->read; # skipping to country node
is($reader->name, 'country', "skipping to country");
is($reader->depth, "1", "depth");
is($reader->getAttribute("acronym"), "AL", "getAttribute");
is($reader->getAttributeNo(0), "AL", "getAttributeNo");
is($reader->getAttributeNs("acronym", undef), "AL", "getAttributeNs");
is($reader->lineNumber, "20", "lineNumber");
is($reader->columnNumber, "1", "columnNumber");
ok($reader->hasAttributes, "hasAttributes");
ok(! $reader->hasValue, "hasValue");
ok(! $reader->isDefault, "isDefault");
ok(! $reader->isEmptyElement, "isEmptyElement");
ok(! $reader->isNamespaceDecl, "isNamespaceDecl");
ok(! $reader->isValid, "isValid");
is($reader->localName, "country", "localName");
is($reader->lookupNamespace(undef), undef, "lookupNamespace");
ok($reader->moveToAttribute("acronym"), "moveToAttribute");
ok($reader->moveToAttributeNo(0), "moveToAttributeNo");
ok($reader->moveToAttributeNs("acronym", undef), "moveToAttributeNs");
ok($reader->moveToElement, "moveToElement");
ok($reader->moveToFirstAttribute, "moveToFirstAttribute");
ok($reader->moveToNextAttribute, "moveToNextAttribute");
ok($reader->readAttributeValue, "attributeValue");
$reader->moveToElement;
is($reader->name, "country", "name");
is($reader->namespaceURI, undef, "namespaceURI");
ok($reader->nextSibling, "nextSibling");
is($reader->nodeType, XML_READER_TYPE_SIGNIFICANT_WHITESPACE, "nodeType");
is($reader->prefix, undef, "prefix");
is($reader->readInnerXml, "", "readInnerXml");
is($reader->readOuterXml, "\n", "readOuterXml");
ok($reader->readState, "readState");
is($reader->getParserProp('expand_entities'), 1, "getParserProp");
ok($reader->standalone, "standalone");
is($reader->value, "\n", "value");
is($reader->xmlLang, undef, "xmlLang");
ok($reader->close, "close");
}
# FD interface
for my $how (qw(FD IO)) {
# my $fd;
open my $fd, '<', $file or die "cannot open $file: $!\n";
my $reader = XML::LibXML::Reader->new($how => $fd, URI => $file);
isa_ok($reader, "XML::LibXML::Reader");
$reader->read;
$reader->read;
is($reader->name, "countries","name in fd");
$reader->read;
$reader->read;
$reader->read;
close $fd;
}
# scalar interface
{
open my $fd, '<', $file or die "cannot open $file: $!\n";
my $doc;
{
local $/;
$doc = <$fd>;
}
close $fd;
my $reader = XML::LibXML::Reader->new(string => $doc, URI => $file);
isa_ok($reader, "XML::LibXML::Reader");
$reader->read;
$reader->read;
is($reader->name, "countries","name in string");
}
# DOM
{
my $DOM = XML::LibXML->new->parse_file($file);
my $reader = XML::LibXML::Reader->new(DOM => $DOM);
isa_ok($reader, "XML::LibXML::Reader");
$reader->read;
$reader->read;
is($reader->name, "countries","name in string");
ok($reader->document,"document");
ok($reader->document->isSameNode($DOM),"document is DOM");
}
# Expand
{
my ($node1,$node2, $node3);
my $xml = <<'EOF';