Anonymous avatar Anonymous committed 6d840b2

Modified Files:
t/05dombasic.t
+ more securety test
t/07nodelist.t
+ attributelist tests

Comments (0)

Files changed (7)

 use Test;
-BEGIN { plan tests => 2 }
+BEGIN { plan tests => 4 }
 END { ok(0) unless $loaded }
 use XML::LibXML;
 $loaded = 1;
 
 my $p = XML::LibXML->new();
 ok($p);
+
+# encoding tests
+my $tstr1 = "test";
+my $tstr2 = "täst";
+
+ok( decodeFromUTF8( 'UTF-8' , encodeToUTF8('UTF-8', $tstr1 ) ) eq $tstr1 );
+ok( decodeFromUTF8( 'iso-8859-1' , encodeToUTF8('iso-8859-1', $tstr2 ) ) eq $tstr2 );

t/02parsestring.t

 use Test;
+use Devel::Peek;
+
 BEGIN { plan tests => 4 };
 use XML::LibXML;
 ok(1);
 
 my $parser = XML::LibXML->new();
 ok($parser);
-
+{
 my $doc = $parser->parse_string(<<'EOT');
 <xml/>
 EOT
 
 ok($doc);
+}
 
 eval {
     my $fail = $parser->parse_string(<<'EOT');
 # `make test'. After `make install' it should work as `perl test.pl'
 
 use Test;
-BEGIN { plan tests=>15; }
+BEGIN { plan tests=>19 }
 END {ok(0) unless $loaded;}
 use XML::LibXML;
 $loaded = 1;
 
 # this performs general dom tests
 
-my $vers    = "1.0";
+my $version = "1.0";
 my $enc     = "iso-8859-1";
 my $testtxt = "test";
 my $file    = "example/dromeds.xml";
 
-my $dom = XML::LibXML::Document->createDocument( $vers, $enc );
+my $dom = XML::LibXML::Document->createDocument( $version, $enc );
 ok($dom);
 
-# this feature is for dirty people ;)
+# this feature is for quick and dirty people ;)
 my $dom2 = XML::LibXML::Document->createDocument( );
-ok( $dom2 );
+ok( $dom2
+    && $dom2->getEncoding() eq 'UTF-8' 
+    && $dom2->getVersion() eq $version );
 
-my $xs = "<?xml version=\"$vers\" encoding=\"$enc\"?>\n";
+my $xs = "<?xml version=\"$version\" encoding=\"$enc\"?>\n";
 my $str = $dom->toString;
 ok( $str eq $xs );
 
 ok( defined $elem && $elem->getName() eq "element" );
 
 $dom->setDocumentElement( $elem );
-my $te = $dom->getDocumentElement();
-ok( defined $te && $te->getName() eq $elem->getName() );
+ok( $elem->isEqual( $dom->getDocumentElement() ) );
+
+# lets test if we can overwrite the document element with an 
+# invalid element type
+my $attr = $dom->createAttribute( "test", "test" );
+ok( defined $attr && $attr->getValue() eq "test" );
+
+$dom->setDocumentElement( $attr );
+ok( $elem->isEqual( $dom->getDocumentElement() ) );
+
 
 my $node;
 {
-    my $dom3 = XML::LibXML::Document->createDocument( $vers, $enc );
+    my $dom3 = XML::LibXML::Document->createDocument( $version, $enc );
     $node   = $dom3->createElement( $testtxt );
     $dom3->setDocumentElement( $node );
 }
 ok( defined $node && $node->getName() eq $testtxt );
 
 { 
+    use Devel::Peek;
     my $dom3 = $node->getOwnerDocument();
     ok( defined $dom3 && $dom3->isa( 'XML::LibXML::Document' ) ); 
 }
 # this ends scope and older versions should segfault here 
 ok( defined $node && $node->getName() eq $testtxt );
 
-
 $node = $dom2->createElement( $testtxt );
 $dom2->setDocumentElement( $node );
 my $node2 = $dom->importNode( $node );
-{
+if ( defined $node2 ){
     warn "node not defined " unless defined $node2;
     my $tdoc = $node2->getOwnerDocument();
     warn "doc not defined " unless defined $tdoc;
     warn "wrong doc" if $tdoc->isEqual( $dom2 );
     ok( defined $node2 && defined $tdoc && $tdoc->isEqual( $dom ) == 1 );
 }
+else {
+    ok(0);
+}
 
 my $text = $dom->createTextNode( $testtxt );
 ok( defined $text && $text->isa( "XML::LibXML::Text" ) );
 # parse tests
 
 # init the file parser
-my $parser = XML::LibXML->new();
-$dom    = $parser->parse_file( $file );
+{
+    my $parser = XML::LibXML->new();
+    my $dom3    = $parser->parse_file( $file );
+    ok( defined $dom3 );
+    if ( defined $dom3 ) {
+      $elem   = $dom3->getDocumentElement();
+      ok( defined $elem && 
+          $elem->getType() == XML_ELEMENT_NODE &&
+          $elem->isa( "XML::LibXML::Element" ) );
+    }
+}
 
-ok( defined $dom );
-if ( defined $dom ) {
-  $elem   = $dom->getDocumentElement();
-  ok( defined $elem && 
-      $elem->getType() == XML_ELEMENT_NODE &&
-      $elem->isa( "XML::LibXML::Element" ) );
-}
+ok(1);
+ok(1);
 
 use Test;
 
-BEGIN { plan tests=>42; }
+BEGIN { plan tests=>47; }
 END {ok(0) unless $loaded;}
 use XML::LibXML;
 $loaded = 1;
       $elem3->hasChildNodes() && 
       $elem3->getFirstChild()->getType() == XML_CDATA_SECTION_NODE && 
       $elem3->getFirstChild()->getData() eq $cdata );
-  
+
+  my $testtxt = "täst";
+  my $elem = $dom->createElement( $testtxt );
+  ok( $elem->getName() eq $testtxt );
+  $elem->appendTextNode( $testtxt );
+  $elem->appendTextChild($testtxt, $testtxt);
+  $elem->setAttribute( 'test', $testtxt );
+
+  my ( $n1, $n2 ) = $elem->getChildnodes();
+  ok( $n1 && $n1->getData() eq $testtxt );
+  ok( $n2 && $n2->getName() eq $testtxt );
+  ok( $n2 && $n2->getLocalName() eq $testtxt );
+
+  ok( $elem->getAttribute( 'test' ) eq $testtxt );
 }
 
 
 # `make test'. After `make install' it should work as `perl test.pl'
 
 use Test;
-BEGIN { plan tests=>8; }
+BEGIN { plan tests=>13; }
 END {ok(0) unless $loaded;}
 use XML::LibXML;
 $loaded = 1;
     }	
   
     # we need to create a new document since dromeds is in ASCII ...
-    my $doc = XML::LibXML::Document->new( '1.0', 'iso-8859-1' );
+    my $doc = XML::LibXML::Document->new( '1.0' );
     my $elem2 = $doc->createElement( $camel );
     $doc->setDocumentElement( $elem2 ); 
 
     $elem4 = $doc->createTextNode( $string2 );
     ok( $string2 eq $elem4->toString() );
 }
+
+# attribute lists
+
+my $teststring = '<xml xmlns:b="http://whatever" attr1="1" attr2="2" b:attr3="3"/>';
+my $dom2 = $parser->parse_string( $teststring );
+if( defined $dom2 ) {
+    my $elem = $dom2->getDocumentElement();
+    ok($elem);
+    my @attr = $elem->getAttributes();
+    ok( scalar(@attr) , 3 );
+    ok( $attr[1]->getValue(), 2 );
+
+    my @nsattr = $elem->getAttributesNS("http://whatever");
+    ok( scalar(@nsattr) , 1 );
+    ok( $nsattr[0]->getValue(), 3 );
+}
 
 my $doc = XML::LibXML->new()->parse_string($xml);
 my $docElem = $doc->getDocumentElement();
+  
 my $child = ($docElem->getChildnodes())[0];
+    ok($child->hasAttributeNS('http://whatever','href'));
+    ok(not defined $child->getAttribute("abc"));
+    ok(defined($child->getLocalName()));
+    ok(!defined($child->getPrefix()));
+    ok(!defined($child->getNamespaceURI()));
 
-ok($child->hasAttributeNS('http://whatever','href'));
-
-ok(!defined($child->getAttribute('bogus')));
-
-ok(defined($child->getLocalName()));
-ok(!defined($child->getPrefix()));
-ok(!defined($child->getNamespaceURI()));
-
-my $val = $child->getAttributeNS('http://whatever','href');
-ok($val,'out.xml');
+    my $val = $child->getAttributeNS('http://whatever','href');
+    ok($val,'out.xml');
 
 $child = ($docElem->getChildnodes())[1];
-ok($child->getLocalName() eq 'c');
-ok($child->getPrefix() eq 'b');
-ok($child->getNamespaceURI() eq 'http://whatever');
+    ok($child->getLocalName() eq 'c');
+    ok($child->getPrefix() eq 'b');
+    ok($child->getNamespaceURI() eq 'http://whatever');
 
-$child->removeAttributeNS('http://whatever','href');
-ok(!$child->hasAttributeNS('http://whatever','href'));
+    $child->removeAttributeNS('http://whatever','href');
+    ok(!$child->hasAttributeNS('http://whatever','href'));
 
-my $added_attr = 'added.xml';
-$child->setAttributeNS('http://whatever', 'b2:href', $added_attr);
+    my $added_attr = 'added.xml';
+    $child->setAttributeNS('http://whatever', 'b2:href', $added_attr);
 
-ok($child->hasAttributeNS('http://whatever','href')
-   && $child->getAttributeNS('http://whatever','href') eq $added_attr);
-
+    ok($child->hasAttributeNS('http://whatever','href')
+        && $child->getAttributeNS('http://whatever','href') eq $added_attr);
+ 
 my @bytag = $docElem->getElementsByTagName('x');
 ok(scalar(@bytag) == 1);
 
 ok(1);
 
 sub make_doc {
-my ($r, $cgi) = @_;
-my $document = XML::LibXML::Document->createDocument("1.0", "UTF-8");
-# warn("document: $document\n");
-my ($parent);
+    my ($r, $cgi) = @_;
+    my $document = XML::LibXML::Document->createDocument("1.0", "UTF-8");
+    # warn("document: $document\n");
+    my ($parent);
 
-{ my $elem = $document->createElement(q(p));$document->setDocumentElement($elem); $parent = $elem; }
-$parent->setAttribute("xmlns:" . q(param), q(http://axkit.org/XSP/param));
-{ my $elem = $document->createElement(q(param:foo));$parent->appendChild($elem); $parent = $elem; }
-$parent = $parent->getParentNode;
-# warn("parent now: $parent\n");
-$parent = $parent->getParentNode;
-# warn("parent now: $parent\n");
+    {
+        my $elem = $document->createElement(q(p));$document->setDocumentElement($elem); $parent = $elem; 
+    }
 
-return $document
+    $parent->setAttribute("xmlns:" . q(param), q(http://axkit.org/XSP/param));
+    { 
+        my $elem = $document->createElement(q(param:foo));$parent->appendChild($elem); $parent = $elem; 
+    }
+    $parent = $parent->getParentNode;
+    # warn("parent now: $parent\n");
+    $parent = $parent->getParentNode;
+    # warn("parent now: $parent\n");
+
+    return $document
 }
 
 my $doc = make_doc();
 ok($doc);
 
-ok($doc->toString);
+ok( $doc->toString() );
 
 ok(1); ok(1);
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.