perl-XML-LibXML / t / 07nodelist.t

# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

use Test;
BEGIN { plan tests=>8; }
END {ok(0) unless $loaded;}
use XML::LibXML;
$loaded = 1;
ok($loaded);

######################### End of black magic.

# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):

# this performs general dom tests
my $file    = "example/dromeds.xml";
my $string = q{
<B lang="eng">hump</B>
<!-- thingi in german -->
<B lang="ger">Höcker</B>
};
my $camel = 'A';
my $tstr  = qq{<$camel>$string</$camel>};

my $string2 = "Höcker";


# init the file parser
my $parser = XML::LibXML->new();
$dom    = $parser->parse_file( $file );

if ( defined $dom ) {
    # get the root document
    $elem   = $dom->getDocumentElement(); 
    ok( defined $elem && $elem->getName() eq "dromedaries" );

    if( defined $elem ) {
        my @nodelist = $elem->getElementsByTagName( "species" );
        ok( scalar(@nodelist) == 3 );
        return unless scalar(@nodelist) == 3;
        my $lama = $nodelist[1];
        ok( defined $lama && $lama->getAttribute( "name" ) eq "Llama" );
    }	
  
    # we need to create a new document since dromeds is in ASCII ...
    my $doc = XML::LibXML::Document->new( '1.0', 'iso-8859-1' );
    my $elem2 = $doc->createElement( $camel );
    $doc->setDocumentElement( $elem2 ); 

    $elem2->appendWellBalancedChunk( $string );
    ok(  $elem2->toString() eq $tstr );
 
    my @bs = $elem2->getElementsByTagName('B');
    ok( ( scalar( @bs ) == 2 ) &&
        ( $bs[0]->getAttribute( 'lang' ) eq "eng" ) && 
        ( $bs[1]->getAttribute( 'lang' ) eq "ger" ) );

    my $elem3 = $doc->createElement('C');
    $elem3->setAttribute( "value", $string2 );


    ok( $elem3->toString() eq "<C value=\"$string2\"/>" );
  
    # test if the output of simple text nodes will be correct as well
    $elem4 = $doc->createTextNode( $string2 );
    ok( $string2 eq $elem4->toString() );
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.