Commits

Anonymous committed 7ace41c

Modified Files:
Tag: mm_fix
10ns.t 11memory.t
tiny fixes

Added Files:
Tag: mm_fix
02parse.t 03doc.t 04node.t 05text.t 06elements.t
i redesigned the testsuite. the special interfaces are now tested
by special scripts. this supports development of the package.

Removed Files:
Tag: mm_fix
02parsestring.t 03parsefile.t 04parsefh.t 05dombasic.t
06nodetypes.t
these files are not required anymore

Comments (0)

Files changed (12)

+# $Id$
+
+##
+# this test checks the parsing capabilities of XML::LibXML
+# it relies on the success of t/01basic.t
+
+use Test;
+use Devel::Peek;
+
+BEGIN { plan tests => 28 };
+use XML::LibXML;
+
+##
+# test values
+my $goodWFString = "<foobar/>";
+my $badWFString1 = "<foo>&</foo>";
+my $badWFString2 = "<foo>";
+
+my $goodWBString = "foo<bar/>foo";
+my $badWBString1 = "<foo>bar";
+my $badWBString2 = "foo</bar>";
+
+my $goodfile = "example/dromeds.xml";
+my $badfile1 = "example/bad.xml";
+my $badfile2 = "does_not_exist.xml";
+
+my $parser = XML::LibXML->new();
+
+print "# 1. Well Formed String Parsing\n";
+
+{
+    my $doc = $parser->parse_string($goodWFString);
+    ok(defined $doc);
+    my $str = $doc->toString();
+    $str =~ s/\<\?xml[^\?]*\?\>//;
+    $str =~ s/\n//g;
+    ok($str, $goodWFString );    
+}
+
+eval { my $fail = $parser->parse_string($badWFString1); };
+ok($@);
+
+eval { my $fail = $parser->parse_string($badWFString2); };
+ok($@);
+
+eval { my $fail = $parser->parse_string(""); };
+ok($@);
+
+eval { my $fail = $parser->parse_string(undef); };
+ok($@);
+
+print "# 2. Well Ballanced String Parsing\n";
+
+{
+    my $fragment;
+    eval { $fragment = $parser->parse_xml_chunk( $goodWBString ); };
+    ok( $fragment );
+    ok( $fragment->toString(), $goodWBString );
+}
+
+eval { my $fail = $parser->parse_xml_chunk($badWBString1); };
+ok($@);
+
+eval { my $fail = $parser->parse_xml_chunk($badWBString2); };
+ok($@);
+
+eval { my $fail = $parser->parse_xml_chunk(""); };
+ok($@);
+
+eval { my $fail = $parser->parse_xml_chunk(undef); };
+ok($@);
+
+print "# 3. Parse A File\n";
+
+{
+    my $doc = $parser->parse_file($goodfile);
+    ok($doc);
+}
+ 
+eval {my $fail = $parser->parse_file($badfile1);};
+ok($@);
+
+eval { $parser->parse_file($badfile2); };
+ok($@);
+
+print "# 4. Parse A Handle\n";
+
+my $fh = IO::File->new($goodfile);
+ok($fh);
+
+my $doc = $parser->parse_fh($fh);
+ok($doc);
+
+$fh = IO::File->new($badfile1);
+ok($fh);
+
+eval { my $doc = $parser->parse_fh($fh); };
+ok($@);
+
+$fh = IO::File->new($badfile2);
+
+eval { my $doc = $parser->parse_fh($fh); };
+ok($@);
+
+print "# 5. x-include processing\n";
+
+my $goodXInclude = q{
+<x>
+<xinclude:include 
+ xmlns:xinclude="http://www.w3.org/2001/XInclude"
+ href="test2.xml"/>
+</x>
+};
+
+
+my $badXInclude = q{
+<x xmlns:xinclude="http://www.w3.org/2001/XInclude">
+<xinclude:include href="bad.xml"/>
+</x>
+};
+
+{
+    $parser->base_uri( "example/" );
+
+    my $doc = $parser->parse_string( $goodXInclude );
+    ok($doc);
+    my $i;
+    eval { $i = $parser->processXIncludes($doc); };
+    ok( $i );
+
+    $doc = $parser->parse_string( $badXInclude );
+    $i= undef;
+    eval { $i = $parser->processXIncludes($doc); };
+    ok($@);
+    
+    # auto expand
+    $parser->expand_xinclude(1);
+    $doc = $parser->parse_string( $goodXInclude );
+    ok($doc);
+
+    $doc = undef;
+    eval { $doc = $parser->parse_string( $badXInclude ); };
+    ok($@);
+    ok(!$doc);
+
+    # some bad stuff 
+    eval{ $parser->processXIncludes(undef); };
+    ok($@);
+    eval{ $parser->processXIncludes("blahblah"); };
+    ok($@);
+}

t/02parsestring.t

-use Test;
-use Devel::Peek;
-
-BEGIN { plan tests => 12 };
-use XML::LibXML;
-ok(1);
-
-my $parser = XML::LibXML->new();
-ok($parser);
-
-{
-    my $doc = $parser->parse_string(<<'EOT');
-<test/>
-EOT
-
-    ok($doc);
-}
-
-eval {
-    my $fail = $parser->parse_string(<<'EOT');
-<foo>&</foo>
-EOT
-};
-ok($@);
-
-# warn "doc is: ", $doc2->toString, "\n";
-
-eval { my $fail = $parser->parse_string(""); };
-# warn "# $@\n";
-ok($@);
-
-## 
-# phish: parse_xml_chunk tests
-
-my $chunk = "foo<a>bar<b/>foobar</a>" ;
-my $fragment;
-
-eval {
-    $fragment = $parser->parse_xml_chunk( $chunk );
-};
-ok( $fragment );
-
-my $doc = $parser->parse_string(<<'EOT');
-<test/>
-EOT
-
-ok($doc);
-
-my $r = $doc->getDocumentElement();
-ok($r);
-
-$r->appendChild( $fragment );
-my $str = $r->toString();
-
-# bad fragment tests
-my $badchunk = "foo<bar>";
-eval {
-    $fragment = $parser->parse_xml_chunk( $badchunk );
-};
-ok( !$fragment );
-
-$badchunk = "foo</bar>foobar";
-eval {
-    $fragment = $parser->parse_xml_chunk( $badchunk );
-};
-ok( !$fragment );
-
-$badchunk = "";
-eval {
-    $fragment = $parser->parse_xml_chunk( $badchunk );
-};
-ok( !$fragment );
-
-$badchunk = undef;
-eval {
-    local $^W; # turn off uninitialised value warnings
-    $fragment = $parser->parse_xml_chunk( $badchunk );
-};
-ok( !$fragment );
+# $Id$
+
+##
+# this test checks the DOM Document interface of XML::LibXML
+# it relies on the success of t/01basic.t and t/02parse.t
+
+# it will ONLY test the DOM capabilities as specified in DOM Level3
+# XPath tests should be done in another test file
+
+# since all tests are run on a preparsed 
+
+use Test;
+use Devel::Peek;
+
+use strict;
+use warnings;
+
+BEGIN { plan tests => 62 };
+use XML::LibXML;
+
+{
+    print "# 1. Document Attributes\n";
+
+    my $doc = XML::LibXML::Document->createDocument();
+    ok($doc);
+    ok( $doc->encoding,undef); 
+    ok( $doc->version,  "1.0" );
+    ok( $doc->standalone, -1 ); # is the value we get for undefined,
+                                   # actually the same as 0 but just not set.
+    ok( $doc->URI, undef);       # should be set by default.
+    ok( $doc->compression, -1 ); # -1 indicates NO compression at all!
+                                 # while 0 indicates just no zip compression 
+                                 # (big difference huh?)
+
+    $doc->setEncoding( "iso-8859-1" );
+    ok( $doc->encoding, "iso-8859-1" );
+
+    $doc->setVersion(12.5);
+    ok( $doc->version, "12.5" );
+
+    $doc->setStandalone(1);
+    ok( $doc->standalone, 1 );
+
+    $doc->setBaseURI( "localhost/here.xml" );
+    ok( $doc->URI, "localhost/here.xml" );
+
+    my $doc2 = XML::LibXML::Document->createDocument("1.1", "iso-8859-2");
+    ok( $doc2->encoding, "iso-8859-2" );
+    ok( $doc2->version,  "1.1" );
+}
+
+{
+    print "# 2. Creating Elements\n";
+    my $doc = XML::LibXML::Document->new();
+    {
+        my $node = $doc->createDocumentFragment();
+        ok($node);
+        ok($node->nodeType, XML_DOCUMENT_FRAG_NODE);
+    }
+
+    {
+        my $node = $doc->createElement( "foo" );
+        ok($node);
+        ok($node->nodeType, XML_ELEMENT_NODE );
+        ok($node->nodeName, "foo" );
+    }
+
+    {
+        # namespaced element test
+        my $node = $doc->createElementNS( "http://kungfoo", "foo:bar" );
+        ok($node);
+        ok($node->nodeType, XML_ELEMENT_NODE);
+        ok($node->nodeName, "foo:bar");
+        ok($node->prefix, "foo");
+        ok($node->localname, "bar");
+        ok($node->namespaceURI, "http://kungfoo");
+    }
+
+    {
+        my $node = $doc->createTextNode( "foo" );
+        ok($node);
+        ok($node->nodeType, XML_TEXT_NODE );
+        ok($node->nodeValue, "foo" );
+    }
+
+    {
+        my $node = $doc->createComment( "foo" );
+        ok($node);
+        ok($node->nodeType, XML_COMMENT_NODE );
+        ok($node->nodeValue, "foo" );
+        ok($node->toString, "<!--foo-->");
+    }
+
+    {
+        my $node = $doc->createCDATASection( "foo" );
+        ok($node);
+        ok($node->nodeType, XML_CDATA_SECTION_NODE );
+        ok($node->nodeValue, "foo" );
+        ok($node->toString, "<![CDATA[foo]]>");
+    }
+
+    {
+        my $attr = $doc->createAttribute("foo", "bar");
+        ok($attr);
+        ok($attr->nodeType, XML_ATTRIBUTE_NODE );
+        ok($attr->name, "foo");
+        ok($attr->value, "bar" );
+        ok($attr->hasChildNodes, 0);
+        my $content = $attr->firstChild;
+        ok( $content );
+    }
+
+    {
+        eval {
+            my $attr = $doc->createAttributeNS("http://kungfoo", "kung:foo","bar");
+        };
+        ok($@);
+
+        my $root = $doc->createElement( "foo" );
+        $doc->setDocumentElement( $root );
+
+        my $attr;
+        eval {
+           $attr = $doc->createAttributeNS("http://kungfoo", "kung:foo","bar");
+        };
+        ok($attr);
+        ok($attr->nodeName, "kung:foo");
+        ok($attr->name,"foo" );
+        ok($attr->value, "bar" );
+        
+    }
+
+    {
+        my $pi = $doc->createProcessingInstruction( "foo", "bar" );
+        ok($pi);
+        ok($pi->nodeType, XML_PI_NODE);
+        ok($pi->nodeName, "foo");
+        ok($pi->textContent, "bar");
+    }
+
+}
+
+{
+    print "# 3.  Document Manipulation\n";
+    print "# 3.1 Document Elements\n"; 
+
+    my $doc = XML::LibXML::Document->new();
+    my $node = $doc->createElement( "foo" );
+    $doc->setDocumentElement( $node );
+    my $tn = $doc->documentElement;
+    ok($tn);
+    ok($node->isSameNode($tn));
+
+    my $node2 = $doc->createElement( "bar" );
+    
+    $doc->appendChild($node2);
+    my @cn = $doc->childNodes;
+    ok( scalar(@cn) , 1);
+    ok($cn[0]->isSameNode($node));
+
+    $doc->insertBefore($node2, $node);
+    @cn = $doc->childNodes;
+    ok( scalar(@cn) , 1);
+    ok($cn[0]->isSameNode($node));
+
+    $doc->removeChild($node);
+    @cn = $doc->childNodes;
+    ok( scalar(@cn) , 0);
+
+    for ( 1..2 ) {
+        my $nodeA = $doc->createElement( "x" );
+        $doc->setDocumentElement( $nodeA );
+    }
+    ok(1); # must not segfault here :)
+
+    $doc->setDocumentElement( $node2 );
+    @cn = $doc->childNodes;
+    ok( scalar(@cn) , 1);
+    ok($cn[0]->isSameNode($node2));
+
+    print "# 3.2 Processing Instructions\n"; 
+    {
+        my $pi = $doc->createProcessingInstruction( "foo", "bar" );
+        $doc->appendChild( $pi );
+        @cn = $doc->childNodes;
+        ok( $pi->isSameNode($cn[-1]) );
+        $pi->setData( 'bar="foo"' );
+        ok( $pi->textContent, 'bar="foo"');
+        $pi->setData( foo=>"foo" );
+        ok( $pi->textContent, 'foo="foo"');
+        
+    }
+
+    print "# 3.3 Comment Nodes\n"; 
+
+    print "# 3.4 DTDs\n";
+}
+
+{
+    print "# 4. Document Storing\n";
+}

t/03parsefile.t

-use Test;
-BEGIN { plan tests => 5 };
-use XML::LibXML;
-ok(1);
-
-my $parser = XML::LibXML->new();
-ok($parser);
-
-my $doc = $parser->parse_file("example/dromeds.xml");
-
-ok($doc);
-
-eval {
-    $parser->parse_file("example/bad.xml");
-};
-ok($@);
-
-eval {
-    $parser->parse_file("does_not_exist.xml");
-};
-ok($@);
-
-# warn "doc is: ", $doc->toString, "\n";
+# $Id$
+
+##
+# this test checks the DOM Node interface of XML::LibXML
+# it relies on the success of t/01basic.t and t/02parse.t
+
+# it will ONLY test the DOM capabilities as specified in DOM Level3
+# XPath tests should be done in another test file
+
+# since all tests are run on a preparsed 
+
+use Test;
+use Devel::Peek;
+
+use strict;
+use warnings;
+
+BEGIN { plan tests => 101 };
+use XML::LibXML;
+
+my $xmlstring = q{<foo>bar<foobar/><bar foo="foobar"/><!--foo--><![CDATA[&foo bar]]></foo>};
+
+my $parser = XML::LibXML->new();
+my $doc    = $parser->parse_string( $xmlstring );
+
+print "# 1   Standalone Without NameSpaces\n\n"; 
+print "# 1.1 Node Attributes\n";
+
+{
+    my $node = $doc->documentElement;
+    my $rnode;
+
+    ok($node);
+    ok($node->nodeType, XML_ELEMENT_NODE);
+    ok($node->nodeName, "foo");
+    ok($node->nodeValue, undef);
+    ok($node->hasChildNodes);
+    ok($node->textContent, "bar&foo bar");
+
+    {
+        my @children = $node->childNodes;
+        ok( scalar @children, 5 );
+        ok( $children[0]->nodeType, XML_TEXT_NODE );
+        ok( $children[0]->nodeValue, "bar" );
+        ok( $children[4]->nodeType, XML_CDATA_SECTION_NODE );
+        ok( $children[4]->nodeValue, "&foo bar" );
+
+        my $fc = $node->firstChild;
+        ok( $fc );
+        ok( $fc->isSameNode($children[0]));
+        ok( $fc->baseURI =~ /unknown-/ );
+
+        my $od = $fc->ownerDocument;
+        ok( $od );
+        ok( $od->isSameNode($doc));
+
+        my $xc = $fc->nextSibling;
+        ok( $xc );
+        ok( $xc->isSameNode($children[1]) );
+
+        $fc = $node->lastChild;
+        ok( $fc );
+        ok( $fc->isSameNode($children[4]));
+
+        $xc = $fc->previousSibling;
+        ok( $xc );
+        ok( $xc->isSameNode($children[3]) );
+        $rnode = $xc;
+
+        $xc = $fc->parentNode;
+        ok( $xc );
+        ok( $xc->isSameNode($node) );
+
+        $xc = $children[2];   
+        {
+            print "# 1.2 Attribute Node\n";
+            ok( $xc->hasAttributes );
+            my $attributes = $xc->attributes;
+            ok( $attributes );
+            ok( ref($attributes), "XML::LibXML::NamedNodeMap" );
+            ok( $attributes->length, 1 );
+            my $attr = $attributes->getNamedItem("foo");
+
+            ok( $attr );
+            ok( $attr->nodeType, XML_ATTRIBUTE_NODE );
+            ok( $attr->nodeName, "foo" );
+            ok( $attr->nodeValue, "foobar" );
+            ok( $attr->hasChildNodes, 0);
+        }
+
+        {
+            my @attributes = $xc->attributes;
+            ok( scalar( @attributes ), 1 );
+        }
+
+        print "# 1.2 Node Cloning\n";
+        {
+            my $cnode  = $doc->createElement("foo");
+            my $c1node = $doc->createElement("bar");
+            $cnode->appendChild( $c1node );
+            
+            my $xnode = $cnode->cloneNode(0);
+            ok( $xnode );
+            ok( $xnode->nodeName, "foo" );
+            ok( not $xnode->hasChildNodes );
+            
+            $xnode = $cnode->cloneNode(1);
+            ok( $xnode );
+            ok( $xnode->nodeName, "foo" );
+            ok( $xnode->hasChildNodes );
+            my @cn = $xnode->childNodes;
+            ok( @cn );
+            ok( scalar(@cn), 1);
+            ok( $cn[0]->nodeName, "bar" );
+            ok( !$cn[0]->isSameNode( $c1node ) );
+        }
+    }
+
+    {
+        my $children = $node->childNodes;
+        ok( defined $children );
+        ok( ref($children), "XML::LibXML::NodeList" );
+    }
+
+    print "# 2. (Child) Node Manipulation\n";
+
+    print "# 2.1 Valid Operations\n";
+
+    {
+        print "# 2.1.1 Single Node\n";
+
+        my $inode = $doc->createElement("kungfoo"); # already tested
+        my $xn = $node->insertBefore($inode, $rnode);
+        ok( $xn );
+        ok( $xn->isSameNode($inode) );
+
+        my @cn = $node->childNodes;
+        ok(scalar(@cn), 6);
+        ok( $cn[3]->isSameNode($inode) );
+
+        $xn = $node->removeChild($inode);
+        ok($xn);
+        ok($xn->isSameNode($inode));
+    
+        @cn = $node->childNodes;
+        ok(scalar(@cn), 5);
+        ok( $cn[3]->isSameNode($rnode) );
+    
+        $xn = $node->appendChild($inode);    
+        ok($xn);
+        ok($xn->isSameNode($inode));
+        ok($xn->isSameNode($node->lastChild));
+
+        $xn = $node->removeChild($inode);
+        ok($xn);
+        ok($xn->isSameNode($inode));
+        ok($cn[-1]->isSameNode($node->lastChild));
+
+        $xn = $node->replaceChild( $inode, $rnode );
+        ok($xn);
+        ok($xn->isSameNode($rnode));
+
+        my @cn2 = $node->childNodes;
+        ok(scalar(@cn), 5);
+        ok( $cn2[3]->isSameNode($inode) );
+    }
+    {
+        print "# 2.1.2 Document Fragment\n";
+
+        my @cn   = $doc->documentElement->childNodes;
+        my $rnode= $doc->documentElement;
+
+        my $frag = $doc->createDocumentFragment;
+        my $node1= $doc->createElement("kung");
+        my $node2= $doc->createElement("foo");
+
+        $frag->appendChild($node1);
+        $frag->appendChild($node2);
+
+        my $xn = $node->appendChild( $frag );
+        ok($xn);
+        my @cn2 = $node->childNodes;
+        ok(scalar(@cn2), 7);
+        ok($cn2[-1]->isSameNode($node2));
+        ok($cn2[-2]->isSameNode($node1));
+
+        $frag->appendChild( $node1 );
+        $frag->appendChild( $node2 );
+
+        @cn2 = $node->childNodes;
+        ok(scalar(@cn2), 5);
+
+        $xn = $node->replaceChild( $frag, $cn[3] );
+        ok($xn);
+        ok($xn->isSameNode($cn[3]));
+        @cn2 = $node->childNodes;
+        ok(scalar(@cn2), 6);
+
+        $frag->appendChild( $node1 );
+        $frag->appendChild( $node2 );
+
+        $xn = $node->insertBefore( $frag, $cn[0] );           
+        ok($xn);
+        ok($node1->isSameNode($node->firstChild));
+        @cn2 = $node->childNodes;
+        ok(scalar(@cn2), 6);
+
+    }
+
+    print "# 2.2 Invalid Operations\n";    
+}
+
+print "# 3   Standalone With NameSpaces\n\n"; 
+
+{
+    my $doc = XML::LibXML::Document->new();
+    my $URI = "http://kungfoo";
+    my $pre = "foo";
+    my $name= "bar";
+
+    my $elem = $doc->createElementNS($URI, $pre.":".$name);
+
+    ok($elem);
+    ok($elem->nodeName, $pre.":".$name);
+    ok($elem->namespaceURI, $URI);
+    ok($elem->prefix, $pre);
+    ok($elem->localname, $name );
+
+    ok( $elem->lookupNamespacePrefix( $URI ), $pre);
+    ok( $elem->lookupNamespaceURI( $pre ), $URI);
+}
+
+print "# 4.   Document swtiching\n";
+
+{
+    print "# 4.1 simple document\n";
+    my $docA = XML::LibXML::Document->new;
+    {
+        my $docB = XML::LibXML::Document->new;
+        my $e1   = $docB->createElement( "A" );
+        my $e2   = $docB->createElement( "B" );
+        my $e3   = $docB->createElementNS( "C:D","http://kungfoo" );
+        $e1->appendChild( $e2 );
+        $e1->appendChild( $e3 );
+
+        $docA->setDocumentElement( $e1 );
+    }
+    my $elem = $docA->documentElement;
+    my @c = $elem->childNodes;
+    my $xroot = $c[0]->ownerDocument;
+    ok( $xroot->isSameNode($docA) );
+
+    print "# 4.2 move NS node where NS is defiend elsewhere\n";
+
+}
+
+print "# 5.   libxml2 specials\n";
+
+{
+    my $docA = XML::LibXML::Document->new;
+    my $e1   = $docA->createElement( "A" );
+    my $e2   = $docA->createElement( "B" );
+    my $e3   = $docA->createElement( "C" ); 
+
+    $e1->appendChild( $e2 );
+    my $x = $e2->replaceNode( $e3 );
+    my @cn = $e1->childNodes;
+    ok(@cn);
+    ok( scalar(@cn), 1 );   
+    ok($cn[0]->isSameNode($e3));
+    ok($x->isSameNode($e2));
+
+    $e3->addSibling( $e2 );
+    @cn = $e1->childNodes;  
+    ok( scalar(@cn), 2 );   
+    ok($cn[0]->isSameNode($e3));
+    ok($cn[1]->isSameNode($e2));     
+}
+
+print "# 6.   implicit attribute manipulation\n";
+
+{
+    my $parser = XML::LibXML->new();
+    my $doc = $parser->parse_string( '<foo bar="foo"/>' );
+    my $root = $doc->documentElement;
+    my $attributes = $root->attributes;
+    ok($attributes);
+    
+    my $newAttr = $doc->createAttribute( "kung", "foo" );
+    $attributes->setNamedItem( $newAttr );
+
+    my @att = $root->attributes;
+    ok(@att);
+    ok(scalar(@att), 2);
+    $newAttr = $doc->createAttributeNS( "http://kungfoo", "x:kung", "foo" );
+
+    $attributes->setNamedItem($newAttr);
+    @att = $root->attributes;
+    ok(@att);
+    ok(scalar(@att), 4); # because of the namespace ...
+    
+    $newAttr = $doc->createAttributeNS( "http://kungfoo", "x:kung", "bar" );
+    $attributes->setNamedItem($newAttr);
+    @att = $root->attributes;
+    ok(@att);
+    ok(scalar(@att), 4);
+    ok($att[2]->isSameNode($newAttr));
+
+    $attributes->removeNamedItem("x:kung");
+
+    @att = $root->attributes;
+    ok(@att);
+    ok(scalar(@att), 3);
+    ok($attributes->length, 3);
+}

t/04parsefh.t

-use Test;
-BEGIN { plan tests => 6 };
-use XML::LibXML;
-use IO::File;
-ok(1);
-
-my $parser = XML::LibXML->new();
-ok($parser);
-
-my $fh = IO::File->new("example/dromeds.xml");
-
-ok($fh);
-
-my $doc = $parser->parse_fh($fh);
-
-ok($doc);
-
-$fh = IO::File->new("example/bad.xml");
-
-ok($fh);
-
-eval {
-    my $doc = $parser->parse_fh($fh);
-};
-ok($@);
-

t/05dombasic.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=>23 }
-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 $version = "1.0";
-my $enc     = "iso-8859-1";
-my $testtxt = "test";
-my $file    = "example/dromeds.xml";
-
-my $dom = XML::LibXML::Document->createDocument( $version, $enc );
-ok($dom);
-
-# this feature is for quick and dirty people ;)
-my $dom2 = XML::LibXML::Document->createDocument( );
-ok( $dom2
-    && $dom2->getEncoding() eq 'UTF-8' 
-    && $dom2->getVersion() eq $version );
-
-my $xs = "<?xml version=\"$version\" encoding=\"$enc\"?>\n";
-my $str = $dom->toString;
-ok( $str eq $xs );
-
-my $elem = $dom->createElement( "element" );
-ok( defined $elem && $elem->getName() eq "element" );
-
-$dom->setDocumentElement( $elem );
-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( $version, $enc );
-    $node   = $dom3->createElement( $testtxt );
-    $dom3->setDocumentElement( $node );
-}
-
-# this ends scope and older versions should segfault here 
-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" ) );
-
-$text = $dom->createComment( $testtxt );
-ok( defined $text && $text->isa( "XML::LibXML::Comment" ) );
-
-$text = $dom->createCDATASection( $testtxt );
-ok( defined $text && $text->isa( "XML::LibXML::CDATASection" ) );
-
-# PI tests
-my $pi = $dom->createPI( "test", "test" );
-ok( $pi );
-
-
-$dom->appendChild( $pi );
-my @clds = $dom->childNodes();
-my $cnt_dn = scalar( @clds );
-ok( $cnt_dn > 1 );
-
-$node = $dom2->createElement( $testtxt );
-$dom->appendChild( $node );
-@clds = $dom->childNodes();
-ok( scalar( @clds ), $cnt_dn );
-
-# parse tests
-
-# init the file parser
-{
-    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( $dom3->URI, $file );
-      my $oldURI = $dom3->URI("foo.xml");
-      ok( $dom3->URI, "foo.xml" );
-      ok( $oldURI, $file );
-    }
-}
-
+# $Id$
+
+##
+# this test checks the DOM Characterdata interface of XML::LibXML
+
+use Test;
+use Devel::Peek;
+
+use strict;
+use warnings;
+
+BEGIN { plan tests => 24 };
+use XML::LibXML;
+
+my $doc = XML::LibXML::Document->new();
+
+{
+    print "# 1. creation\n";
+    my $foo = "foobar";
+    my $textnode = $doc->createTextNode($foo);
+    ok( $textnode );
+    ok( $textnode->nodeValue(), $foo );
+
+    print "# 2. substring\n";
+    my $tnstr = $textnode->substringData( 1,2 );
+    ok( $tnstr , "oo" );
+    ok( $textnode->nodeValue(), $foo );
+
+    print "# 3. Expansion\n";
+    $textnode->appendData( $foo );
+    ok( $textnode->nodeValue(), $foo . $foo );
+
+    $textnode->insertData( 6, "FOO" );
+    ok( $textnode->nodeValue(), $foo."FOO".$foo );
+
+    $textnode->setData( $foo );
+    $textnode->insertData( 6, "FOO" );
+    ok( $textnode->nodeValue(), $foo."FOO" );
+    $textnode->setData( $foo );
+    $textnode->insertData( 3, "" );
+    ok( $textnode->nodeValue(), $foo );
+
+    print "# 4. Removement\n";
+    $textnode->deleteData( 1,2 );
+    ok( $textnode->nodeValue(), "fbar" );
+    $textnode->setData( $foo );
+    $textnode->deleteData( 1,10 );
+    ok( $textnode->nodeValue(), "f" );
+    $textnode->setData( $foo );
+    $textnode->deleteData( 10,1 );
+    ok( $textnode->nodeValue(), $foo );
+    $textnode->deleteData( 1,0 );
+    ok( $textnode->nodeValue(), $foo );
+    $textnode->deleteData( 0,0 );
+    ok( $textnode->nodeValue(), $foo );
+    $textnode->deleteData( 0,2 );
+    ok( $textnode->nodeValue(), "obar" );
+
+    print "# 5. Replacement\n";
+    $textnode->setData( "test" );
+    $textnode->replaceData( 1,2, "phish" );
+    ok( $textnode->nodeValue(), "tphisht" );
+    $textnode->setData( "test" );
+    $textnode->replaceData( 1,4, "phish" );
+    ok( $textnode->nodeValue(), "tphish" );
+    $textnode->setData( "test" );
+    $textnode->replaceData( 1,0, "phish" );
+    ok( $textnode->nodeValue(), "tphishest" );
+
+
+    print "# 6. XML::LibXML features\n";
+    $textnode->setData( "test" );
+
+    $textnode->replaceDataString( "es", "new" );   
+    ok( $textnode->nodeValue(), "tnewt" );
+
+    $textnode->replaceDataRegEx( 'n(.)w', '$1s' );
+    ok( $textnode->nodeValue(), "test" );
+
+    $textnode->setData( "blue phish, white phish, no phish" );
+    $textnode->replaceDataRegEx( 'phish', 'test' );
+    ok( $textnode->nodeValue(), "blue test, white phish, no phish" );
+
+    # replace them all!
+    $textnode->replaceDataRegEx( 'phish', 'test', 'g' );
+    ok( $textnode->nodeValue(), "blue test, white test, no test" );
+
+    # check if special chars are encoded properly 
+    $textnode->setData( "te?st" );
+    $textnode->replaceDataString( "e?s", 'ne\w' );   
+    ok( $textnode->nodeValue(), 'tne\wt' );
+}
+
+{
+    print "# standalone test\n";
+    my $node = XML::LibXML::Text->new("foo");
+    ok($node);
+    ok($node->nodeValue, "foo" );
+}
+# $Id$
+
+##
+# this test checks the DOM element and attribute interface of XML::LibXML
+
+use Test;
+use Devel::Peek;
+
+use strict;
+use warnings;
+
+BEGIN { plan tests => 41 };
+use XML::LibXML;
+
+my $foo       = "foo";
+my $bar       = "bar";
+my $nsURI     = "http://foo";
+my $prefix    = "x";
+my $attname1  = "A";
+my $attvalue1 = "a";
+my $attname2  = "B";
+my $attvalue2 = "b";
+
+print "# 1. bound node\n";
+{
+    my $doc = XML::LibXML::Document->new();
+    my $elem = $doc->createElement( $foo );
+    ok($elem);
+    ok($elem->tagName, $foo);
+    
+    $elem->setAttribute( $attname1, $attvalue1 );
+    ok( $elem->hasAttribute($attname1) );
+    ok( $elem->getAttribute($attname1), $attvalue1);
+
+    my $attr = $elem->getAttributeNode($attname1);
+    ok($attr);
+    ok($attr->name, $attname1);
+    ok($attr->value, $attvalue1);
+
+    $elem->setAttribute( $attname1, $attvalue2 );
+    ok($elem->getAttribute($attname1), $attvalue2);
+    ok($attr->value, $attvalue2);
+
+    my $attr2 = $doc->createAttribute($attname2, $attvalue1);
+    ok($attr2);
+
+    $elem->setAttributeNode($attr2);
+    ok($elem->hasAttribute($attname2) );
+    ok($elem->getAttribute($attname2),$attvalue1);
+
+    my $tattr = $elem->getAttributeNode($attname2);
+    ok($tattr->isSameNode($attr2));
+
+    print "# 1.1 Namespaced Attributes\n";
+
+    $elem->setAttributeNS( $nsURI, $prefix . ":". $foo, $attvalue2 );
+    ok( $elem->hasAttributeNS( $nsURI, $foo ) );
+    # warn $elem->toString() , "\n";
+    $tattr = $elem->getAttributeNodeNS( $nsURI, $foo );
+    ok($tattr);
+    ok($tattr->name, $foo);
+    ok($tattr->nodeName, $prefix .":".$foo);
+    ok($tattr->value, $attvalue2 );
+
+    $elem->removeAttributeNode( $tattr );
+    ok( !$elem->hasAttributeNS($nsURI, $foo) );
+
+    # node based functions
+    my $e2 = $doc->createElement($foo);
+    $doc->setDocumentElement($e2);
+    my $nsAttr = $doc->createAttributeNS( $nsURI.".x", $prefix . ":". $foo, $bar);
+    ok( $nsAttr );
+    $elem->setAttributeNodeNS($nsAttr);
+    ok( $elem->hasAttributeNS($nsURI.".x", $foo) );    
+    $elem->removeAttributeNS( $nsURI.".x", $foo);
+    ok( !$elem->hasAttributeNS($nsURI.".x", $foo) );
+
+    $elem->setAttributeNS( $nsURI, $prefix . ":". $attname1, $attvalue2 );
+
+    $elem->removeAttributeNS("",$attname1);
+    ok( $elem->hasAttribute($attname1) );
+    ok( $elem->hasAttributeNS($nsURI,$attname1) );
+} 
+
+print "# 2. unbound node\n";
+{
+    my $elem = XML::LibXML::Element->new($foo);
+    ok($elem);
+    ok($elem->tagName, $foo);
+
+    $elem->setAttribute( $attname1, $attvalue1 );
+    ok( $elem->hasAttribute($attname1) );
+    ok( $elem->getAttribute($attname1), $attvalue1);
+
+    my $attr = $elem->getAttributeNode($attname1);
+    ok($attr);
+    ok($attr->name, $attname1);
+    ok($attr->value, $attvalue1);
+
+    $elem->setAttributeNS( $nsURI, $prefix . ":". $foo, $attvalue2 );
+    ok( $elem->hasAttributeNS( $nsURI, $foo ) );
+    # warn $elem->toString() , "\n";
+    my $tattr = $elem->getAttributeNodeNS( $nsURI, $foo );
+    ok($tattr);
+    ok($tattr->name, $foo);
+    ok($tattr->nodeName, $prefix .":".$foo);
+    ok($tattr->value, $attvalue2 );
+
+    $elem->removeAttributeNode( $tattr );
+    ok( !$elem->hasAttributeNS($nsURI, $foo) );
+    # warn $elem->toString() , "\n";
+}
+
+print "# 3. Namespace switching\n";
+{
+    my $elem = XML::LibXML::Element->new($foo);
+    ok($elem);
+
+    my $doc = XML::LibXML::Document->new();
+    my $e2 = $doc->createElement($foo);
+    $doc->setDocumentElement($e2);
+    my $nsAttr = $doc->createAttributeNS( $nsURI, $prefix . ":". $foo, $bar);
+    ok( $nsAttr );
+
+    $elem->setAttributeNodeNS($nsAttr);
+    ok( $elem->hasAttributeNS($nsURI, $foo) );    
+
+    ok( $nsAttr->ownerDocument, undef);
+    # warn $elem->toString() , "\n";
+} 

t/06nodetypes.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;
-use Devel::Peek;
-BEGIN { plan tests=>87; }
-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 script performs element node tests
-
-my $version = "1.0";
-my $enc1    = "iso-8859-1";
-my $enc2    = "iso-8859-2";
-
-my $aname  = "test";
-my $avalue = "the value";
-my $bvalue = "other value";
-my $testtxt= "text";
-my $comment= "comment";
-my $cdata  = "unparsed";
-
-print "# document tests\n";
-my $dom = XML::LibXML::Document->createDocument( $version, $enc1 );
-ok($dom);
-
-ok( $dom->getVersion, $version );
-ok( $dom->getEncoding, $enc1 );
-
-$dom->setEncoding($enc2);
-ok( $dom->getEncoding, $enc2 );
-$dom->setVersion( "2.0" );
-ok( $dom->getVersion, "2.0" );
-
-print "# node creation 1 (bound element)\n";
-$dom->setEncoding("UTF8");
-
-my $elem1 = $dom->createElement( "A" );
-ok( $elem1 );
-ok( $elem1->getType(), XML_ELEMENT_NODE );
-ok( $elem1->getName(), "A" );
-
-$elem1->setAttribute( $aname, $avalue );
-ok( $elem1->getAttribute( $aname ) eq $avalue );
-ok( $elem1->hasAttribute( $aname ) );
- 
-# toString test
-my $estr = $elem1->toString();
-my $tstr = "<A $aname=\"$avalue\"/>";
-ok( $estr, $tstr );  
-
-my $owner = $elem1->getOwnerDocument();
-ok( $owner->getType, XML_DOCUMENT_NODE );
-
-$dom->setDocumentElement( $elem1 );
-my $te = $dom->getDocumentElement();
-  
-# in the first version it cause a method not found ... 
-ok( $estr, $te->toString() );
-ok( $elem1->isSameNode( $te ) );
-
-#####################################################
-print "# attribute tests\n";
-
-$elem1->setAttribute( $aname, $bvalue );
-ok( $elem1->hasAttribute( $aname ) );
-ok( $elem1->getAttribute( $aname ), $bvalue );
-$elem1->removeAttribute( $aname );
-ok( not $elem1->hasAttribute( $aname ) );
-
-my $attr = XML::LibXML::Attr->new( 'test', 'value' );
-ok( defined $attr && $attr->name() eq 'test' && $attr->getValue() eq 'value' );
-
-$attr->setValue( 'other' );
-ok( $attr->getValue(), 'other' );
-
-###################################################
-print "# text node tests\n";
-
-my $textnode = $dom->createTextNode("test");
-ok( $textnode );
-ok( $textnode->nodeValue(), "test" );
-
-# DOM spec stuff
-
-my $tnstr = $textnode->substringData( 1,2 );
-ok( $tnstr , "es" );
-ok( $textnode->nodeValue(), "test" );
-
-$textnode->appendData( "test" );
-ok( $textnode->nodeValue(), "testtest" );
-
-# this should always work
-$textnode->insertData( 4, "TesT" );
-ok( $textnode->nodeValue(), "testTesTtest" );
-# this should append
-$textnode->setData( "test" );
-$textnode->insertData( 6, "Test" );
-ok( $textnode->nodeValue(), "testTest" );
-$textnode->setData( "test" );
-$textnode->insertData( 3, "" );
-ok( $textnode->nodeValue(), "test" );
-
-# delete data
-$textnode->deleteData( 1,2 );
-ok( $textnode->nodeValue(), "tt" );
-$textnode->setData( "test" );
-$textnode->deleteData( 1,10 );
-ok( $textnode->nodeValue(), "t" );
-$textnode->setData( "test" );
-$textnode->deleteData( 10,1 );
-ok( $textnode->nodeValue(), "test" );
-$textnode->deleteData( 1,0 );
-ok( $textnode->nodeValue(), "test" );
-$textnode->deleteData( 0,0 );
-ok( $textnode->nodeValue(), "test" );
-$textnode->deleteData( 0,2 );
-ok( $textnode->nodeValue(), "st" );
-
-$textnode->setData( "test" );
-$textnode->replaceData( 1,2, "phish" );
-ok( $textnode->nodeValue(), "tphisht" );
-$textnode->setData( "test" );
-$textnode->replaceData( 1,4, "phish" );
-ok( $textnode->nodeValue(), "tphish" );
-$textnode->setData( "test" );
-$textnode->replaceData( 1,0, "phish" );
-ok( $textnode->nodeValue(), "tphishest" );
-
-# now the cool functions they can't do in java :)
-$textnode->setData( "test" );
-
-$textnode->replaceDataString( "es", "new" );   
-ok( $textnode->nodeValue(), "tnewt" );
-
-$textnode->replaceDataRegEx( 'n(.)w', '$1s' );
-ok( $textnode->nodeValue(), "test" );
-
-$textnode->setData( "blue phish, white phish, no phish" );
-$textnode->replaceDataRegEx( 'phish', 'test' );
-ok( $textnode->nodeValue(), "blue test, white phish, no phish" );
-
-# replace them all!
-$textnode->replaceDataRegEx( 'phish', 'test', 'g' );
-ok( $textnode->nodeValue(), "blue test, white test, no test" );
-
-
-###################################################
-print "# child node functions:\n";
-
-my $text = $dom->createTextNode( $testtxt );
-ok( $text );
-ok( $text->getType, XML_TEXT_NODE );
-ok( $text->getData(), $testtxt );
-
-$elem1->appendChild( $text );
-ok( $elem1->hasChildNodes() );
-
-my $tt = $elem1->getFirstChild();
-ok( $tt );
-ok( $text->isSameNode($tt) );
-
-$tt = $elem1->getLastChild();
-ok( $tt );
-ok( $tt->isSameNode($text) ) ;
-
-my @children = $elem1->getChildnodes();
-ok( scalar( @children ) == 1 ); 
-
-# test bugs in classification
-ok( $tt->isa("XML::LibXML::Text") );
-  
-$text = $dom->createComment( $comment ); 
-ok( $text->isa("XML::LibXML::Comment") );
-$elem1->appendChild( $text );
-
-
-$text = $dom->createCDATASection( $cdata ); 
-ok( $text->isa("XML::LibXML::CDATASection") );
-$elem1->appendChild( $text );
- 
-my $str = "";
-
-print "# traversing tests\n";
-
-my $c = $elem1->getFirstChild();
-while ( $c ) {
-    if( $c->getType() == XML_TEXT_NODE ){
-    	ok( $c->isa( "XML::LibXML::Text" ) );
-	    $str .='t';
-    }
-    elsif( $c->getType() == XML_COMMENT_NODE ){
-    	ok( $c->isa( "XML::LibXML::Comment" ) );
-	    $str .='c';
-    }
-    elsif( $c->getType() == XML_CDATA_SECTION_NODE ){
-    	ok( $c->isa( "XML::LibXML::CDATASection" ) );
-	    $str .='d';
-    }
-    else{
-    	$str .= '?';
-    }
-    $c = $c->getNextSibling();
-}
-
-ok( $str, 'tcd' ); 
-
-# reverse traversing
-$str = "";
-my $rem = undef;
-$c = $elem1->getLastChild();
-while ( $c ) {
-    if( $c->getType() == XML_TEXT_NODE ){
-	    ok( $c->isa( "XML::LibXML::Text" ) );
-      	$str .='t';
-    }
-    elsif( $c->getType() == XML_COMMENT_NODE ){
-	    ok( $c->isa( "XML::LibXML::Comment" ) );
-	    $rem = $c;
-	    $str .='c';
-    }
-    elsif( $c->getType() == XML_CDATA_SECTION_NODE ){
-      	ok( $c->isa( "XML::LibXML::CDATASection" ) );
-      	$str .='d';
-    }
-    else{
-	    $str .= '?';
-    }
-    $c = $c->getPreviousSibling();
-}
-
-ok( $str , 'dct' ); 
-
-
-print "# replace test\n";
-
-my $elem3 = $dom->createElement( "C" );
-my $tn = $elem1->replaceChild( $elem3, $rem );
-ok( $tn->isSameNode( $rem ) );
-
-$str = "";
-$c = $elem1->getLastChild();
-
-while ( $c ) {
-    if( $c->getType() == XML_TEXT_NODE )            {$str .='t';}
-    elsif( $c->getType() == XML_COMMENT_NODE )      {$str .='c';}
-    elsif( $c->getType() == XML_CDATA_SECTION_NODE ){$str .='d';}
-    elsif( $c->getType() == XML_ELEMENT_NODE )      {$str .='e';}
-    else{$str .= '?';}
-    $c = $c->getPreviousSibling();
-}
-ok( $str, 'det' );    
-ok( not defined $rem->getParentNode() && 
-    not defined $rem->getNextSibling() &&
-    not defined $rem->getPreviousSibling() );
-
-
-# remove test
-print "# remove test\n";
-
-$tt = $elem1->removeChild( $elem3 );
-ok( $tt->isSameNode( $elem3 ) );
-
-$str = "";
-$c = $elem1->getLastChild();
-while ( $c ) {
-    if( $c->getType() == XML_TEXT_NODE )            {$str .='t';}
-    elsif( $c->getType() == XML_COMMENT_NODE )      {$str .='c';}
-    elsif( $c->getType() == XML_CDATA_SECTION_NODE ){$str .='d';}
-    elsif( $c->getType() == XML_ELEMENT_NODE )      {$str .='e';}
-    else{$str .= '?';}
-    $c = $c->getPreviousSibling();
-}
-ok( $str, 'dt' );    
-
-ok( not defined $elem3->getParentNode() && 
-    not defined $elem3->getNextSibling() &&
-    not defined $elem3->getPreviousSibling() ); 
-
-# node moving in the tree ...
-
-$elem1->appendChild( $elem3 );
-$elem3->appendChild( $text );
-$str = "";
-$c = $elem1->getLastChild();
-
-while ( $c ) {
-    if( $c->getType() == XML_TEXT_NODE )            {$str .='t';}
-    elsif( $c->getType() == XML_COMMENT_NODE )      {$str .='c';}
-    elsif( $c->getType() == XML_CDATA_SECTION_NODE ){$str .='d';}
-    elsif( $c->getType() == XML_ELEMENT_NODE )      {$str .='e';}
-    else{$str .= '?';}
-    $c = $c->getPreviousSibling();
-}
-ok( $str, 'et' );
-
-ok( $elem3->hasChildNodes() && 
-    $elem3->getFirstChild()->getType() == XML_CDATA_SECTION_NODE && 
-    $elem3->getFirstChild()->getData() eq $cdata );
-
-#################################################
-# explicit document fragment test
-print "# fragment tests \n";
-
-$elem4 = $dom->createElement("D");
-
-$frag = $dom->createDocumentFragment();
-#   $frag = XML::LibXML::DocumentFragment->new();
-ok( $frag );
-$frag->appendChild( $elem4 );
-
-ok( $frag->hasChildNodes() );
-ok( ($frag->childNodes)[0]->nodeName, "D" );
-
-$domroot = $dom->documentElement;
-
-# @ta = $domroot->childNodes;
-# warn "root has ",scalar( @ta ) ," elements\n"; 
-$domroot->appendChild( $frag );
-@ta =$frag->childNodes;
-ok( scalar(@ta), 0 );
-
-@ta =$domroot->childNodes;
-ok( scalar(@ta), 3 );
-# ok( ($domroot->childNodes)[2]->nodeName, $elem4->nodeName );
-
-$frag->appendChild( ($domroot->childNodes)[1] );
-$frag->appendChild( ($domroot->childNodes)[1] );
-  
-$cnode = ($domroot->childNodes)[0];
-
-ok( $cnode );
-ok( $cnode->nodeValue, $testtxt);
-
-ok( scalar($domroot->childNodes), 1 );
-$domroot->replaceChild( $frag, $cnode );
-ok( scalar($frag->childNodes), 0 );
-ok( scalar($domroot->childNodes), 2 ); 
-
-# warn $domroot->toString();
-
-print "# node creation 2 (unbound element)\n";
-
-# NOTE!
-#
-# this should only be a virtual thing! you should never everdo such a
-# thing. create nodes allways through a document, otherwise the node
-# might not be in UTF-8 which confuses XSLT, toString etc.
-#
-# these tests are ment to test logical correctness!
-
-my $elem2 = XML::LibXML::Element->new( "B" );
-
-ok( $elem2 );
-ok( defined $elem2 && $elem2->getType() == XML_ELEMENT_NODE );
-ok( defined $elem2 && $elem2->getName() eq "B" );
-
-# much easier to test if no owner document is set ...
-ok( not defined $elem2->getOwnerDocument() );
-
-$elem2->setAttribute( $aname, $avalue );
-ok( $elem2->getAttribute( $aname ), $avalue );
-$elem2->setAttribute( $aname, $bvalue );
-ok( $elem2->getAttribute( $aname ), $bvalue );
-$elem2->removeAttribute( $aname );
-ok( not $elem2->hasAttribute( $aname ) );
-
-
-print "# document switching!\n";
-
-$elem3 = $dom->createElement( "C" );
-$elem2->appendChild( $elem3 );
-ok( not defined $elem3->getOwnerDocument() );
-
-print "# end tests \n";
 use Test;
-BEGIN { plan tests=>21; }
-END {ok(0) unless $loaded;}
+BEGIN { plan tests=>16; }
 use XML::LibXML;
-$loaded = 1;
-ok($loaded);
 
-my $xml = <<EOX;
+my $parser = XML::LibXML->new();
+
+my $xml1 = <<EOX;
 <a xmlns:b="http://whatever"
 ><x b:href="out.xml"
 /><b:c/></a>
 EOX
 
-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()));
+my $xml2 = <<EOX;
+<a xmlns:b="http://whatever" xmlns:c="http://kungfoo"
+><x b:href="out.xml"
+/><b:c/><c:b/></a>
+EOX
 
-    my $val = $child->getAttributeNS('http://whatever','href');
-    ok($val,'out.xml');
+my $xml3 = <<EOX;
+<a xmlns:b="http://whatever">
+    <x b:href="out.xml"/>
+    <x>
+    <c:b xmlns:c="http://kungfoo">
+        <c:d/>
+    </c:b>
+    </x>
+    <x>
+    <c:b xmlns:c="http://foobar">
+        <c:d/>
+    </c:b>
+    </x>
+</a>
+EOX
 
-$child = ($docElem->getChildnodes())[1];
-    ok($child->getLocalName() eq 'c');
-    ok($child->getPrefix() eq 'b');
-    ok($child->getNamespaceURI() eq 'http://whatever');
-    
+print "# 1.   single namespace \n";
 
-    $child->removeAttributeNS('http://whatever','href');
-    ok(!$child->hasAttributeNS('http://whatever','href'));
+{
+    my $doc1 = $parser->parse_string( $xml1 );
+    my $elem = $doc1->documentElement;
+    ok($elem->lookupNamespaceURI( "b" ), "http://whatever" );
+    my @cn = $elem->childNodes;
+    ok($cn[0]->lookupNamespaceURI( "b" ), "http://whatever" );
+    ok($cn[1]->namespaceURI, "http://whatever" );
+}
 
-    my $added_attr = 'added.xml';
-    $child->setAttributeNS('http://whatever', 'b2:href', $added_attr);
+print "# 2.    multiple namespaces \n";
 
-    ok($child->hasAttributeNS('http://whatever','href')
-        && $child->getAttributeNS('http://whatever','href') eq $added_attr);
- 
-my @bytag = $docElem->getChildrenByTagName('x');
-ok(scalar(@bytag) == 1);
+{
+    my $doc2 = $parser->parse_string( $xml2 );
 
-@bytag = $docElem->getChildrenByTagNameNS('http://whatever','c');
-ok(scalar(@bytag) == 1);
+    my $elem = $doc2->documentElement;
+    ok($elem->lookupNamespaceURI( "b" ), "http://whatever");
+    ok($elem->lookupNamespaceURI( "c" ), "http://kungfoo");
+    my @cn = $elem->childNodes;
 
-my $tag = pop @bytag;
-ok($tag->getLocalName() eq 'c');
-ok($tag->getPrefix() eq 'b');
-ok($tag->getNamespaceURI() eq 'http://whatever');
+    ok($cn[0]->lookupNamespaceURI( "b" ), "http://whatever" );
+    ok($cn[0]->lookupNamespaceURI( "c" ), "http://kungfoo");
 
-my $newElem = $doc->createElementNS('http://whatever','d:newElem');
-ok(defined($newElem));
-ok($newElem->getLocalName, 'newElem');
-ok($newElem->getPrefix, 'd');
-ok($newElem->getNamespaceURI, 'http://whatever');
+    ok($cn[1]->namespaceURI, "http://whatever" );
+    ok($cn[2]->namespaceURI, "http://kungfoo" );
+}
+
+print "# 3.   nested names \n";
+
+{
+    my $doc3 = $parser->parse_string( $xml3 );    
+    my $elem = $doc3->documentElement;
+    my @cn = $elem->childNodes;
+    my @xs = grep { $_->nodeType == XML_ELEMENT_NODE } @cn;
+
+    my @x1 = $xs[1]->childNodes; my @x2 = $xs[2]->childNodes;
+
+    ok( $x1[1]->namespaceURI , "http://kungfoo" );    
+    ok( $x2[1]->namespaceURI , "http://foobar" );    
+
+    # namespace scopeing
+    ok( $elem->lookupNamespacePrefix( "http://kungfoo" ), undef );
+    ok( $elem->lookupNamespacePrefix( "http://foobar" ), undef );
+}
+
+print "# 4. post creation namespace setting\n";
+{
+    my $e1 = XML::LibXML::Element->new("foo");
+    my $e2 = XML::LibXML::Element->new("bar:foo");
+    my $e3 = XML::LibXML::Element->new("foo");
+    $e3->setAttribute( "kung", "foo" );
+    my $a = $e3->getAttributeNode("kung");
+
+    $e1->appendChild($e2);
+    $e2->appendChild($e3);
+    ok( $e2->setNamespace("http://kungfoo", "bar") );
+    ok( $a->setNamespace("http://kungfoo", "bar") );
+    ok( $a->nodeName, "bar:kung" );
+}
 use Test;
 BEGIN { 
     if ($^O eq 'linux' && $ENV{MEMORY_TEST}) {
-        plan tests => 9;
+        plan tests => 12;
     }
     else {
-        print "1..0 # Skipping test on this platform\n";
+        plan tests => 0;
+        print "# Skipping test on this platform\n";
     }
 }
 use XML::LibXML;
-if ($^O eq 'linux' && $ENV{MEMORY_TEST}) {
-    require Devel::Peek;
-    my $peek = 0;
+{
+    if ($^O eq 'linux' && $ENV{MEMORY_TEST}) {
+        require Devel::Peek;
+        my $peek = 0;
     
-    ok(1);
+        ok(1);
 
-    my $times_through = $ENV{MEMORY_TIMES} || 100_000;
+        my $times_through = $ENV{MEMORY_TIMES} || 100_000;
     
-    print("# BASELINE\n");
-    check_mem(1);
+        print("# BASELINE\n");
+        check_mem(1);
 
-    print("# MAKE DOC IN SUB\n");
-    {
-        my $doc = make_doc();
-        ok($doc);
+        print("# MAKE DOC IN SUB\n");
+        {
+            my $doc = make_doc();
+            ok($doc);
+            ok($doc->toString);
+        }
+        check_mem();
+        print("# MAKE DOC IN SUB II\n");
+        # same test as the first one. if this still leaks, it's
+        # our problem, otherwise it's perl :/
+        {
+            my $doc = make_doc();
+            ok($doc);
+
+            ok($doc->toString);
+        }
+        check_mem();
+
+        {
+            my $elem = XML::LibXML::Element->new("foo");
+            my $elem2= XML::LibXML::Element->new("bar");
+            $elem->appendChild($elem2);
+            ok( $elem->toString );
+        }
+        check_mem();
+
+        print("# SET DOCUMENT ELEMENT\n");
+        {
+            my $doc2 = XML::LibXML::Document->new();
+            make_doc_elem( $doc2 );
+            ok( $doc2 );
+            ok( $doc2->documentElement );
+        }
+        check_mem();
+
+        # multiple parsers:
+        print("# MULTIPLE PARSERS\n");
+        for (1..$times_through) {
+            my $parser = XML::LibXML->new();
+        }
+        ok(1);
+
+        check_mem();
+
+        # multiple parses
+        print("# MULTIPLE PARSES\n");
+        for (1..$times_through) {
+            my $parser = XML::LibXML->new();
+            my $dom = $parser->parse_string("<sometag>foo</sometag>");
+        }
+        ok(1);
+
+        check_mem();
+
+        # multiple failing parses
+        print("# MULTIPLE FAILURES\n");
+        for (1..$times_through) {
+            # warn("$_\n") unless $_ % 100;
+            my $parser = XML::LibXML->new();
+            eval {
+                my $dom = $parser->parse_string("<sometag>foo</somtag>"); # Thats meant to be an error, btw!
+            };
+        }
+        ok(1);
     
-        ok($doc->toString);
-    }
-    check_mem();
+        check_mem();
 
-    print("# SET DOCUMENT ELEMENT\n");
-    {
-        my $doc2 = XML::LibXML::Document->new();
-        make_doc_elem( $doc2 );
-        ok( $doc2 );
-        ok( $doc2->documentElement );
-    }
-    check_mem();
-
-    # multiple parsers:
-    print("# MULTIPLE PARSERS\n");
-    for (1..$times_through) {
-        my $parser = XML::LibXML->new();
-    }
-    ok(1);
-
-    check_mem();
-
-    # multiple parses
-    print("# MULTIPLE PARSES\n");
-    for (1..$times_through) {
-        my $parser = XML::LibXML->new();
-        my $dom = $parser->parse_string("<sometag>foo</sometag>");
-    }
-    ok(1);
-
-    check_mem();
-
-    # multiple failing parses
-    print("# MULTIPLE FAILURES\n");
-    for (1..$times_through) {
-        # warn("$_\n") unless $_ % 100;
-        my $parser = XML::LibXML->new();
-        eval {
-        my $dom = $parser->parse_string("<sometag>foo</somtag>"); # Thats meant to be an error, btw!
-        };
-    }
-    ok(1);
-    
-    check_mem();
-
-    # building custom docs
-    print("# CUSTOM DOCS\n");
-    my $doc = XML::LibXML::Document->new();
-    for (1..$times_through) {
-        {
+        # building custom docs
+        print("# CUSTOM DOCS\n");
+        my $doc = XML::LibXML::Document->new();
+        for (1..$times_through)        {
             my $elem = $doc->createElement('x');
             
             if($peek) {
-            warn("Doc before elem\n");
-            Devel::Peek::Dump($doc);
-            warn("Elem alone\n");
-            Devel::Peek::Dump($elem);
+                warn("Doc before elem\n");
+                Devel::Peek::Dump($doc);
+                warn("Elem alone\n");
+                Devel::Peek::Dump($elem);
             }
             
-            # $doc->setDocumentElement($elem);
+            $doc->setDocumentElement($elem);
             
             if ($peek) {
-            warn("Elem after attaching\n");
-            Devel::Peek::Dump($elem);
-            warn("Doc after elem\n");
-            Devel::Peek::Dump($doc);
+                warn("Elem after attaching\n");
+                Devel::Peek::Dump($elem);
+                warn("Doc after elem\n");
+                Devel::Peek::Dump($doc);
             }
         }
         if ($peek) {
-        warn("Doc should be freed\n");
-        Devel::Peek::Dump($doc);
+            warn("Doc should be freed\n");
+            Devel::Peek::Dump($doc);
         }
+        ok(1);
+        check_mem();
     }
-    ok(1);
-
-    check_mem();
-
 }
 
 sub make_doc {
     # warn("document: $document\n");
     my ($parent);
 
-    { my $elem = $document->createElement(q(p));$document->setDocumentElement($elem); $parent = $elem; }
+    { 
+        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;
+    
+    { 
+        my $elem = $document->createElementNS(q(http://axkit.org/XSP/param),q(param:foo),);
+        $parent->appendChild($elem);
+        $parent = $elem;
+    }
+
+    $parent = $parent->parentNode;
     # warn("parent now: $parent\n");
-    $parent = $parent->getParentNode;
+    $parent = $parent->parentNode;
     # warn("parent now: $parent\n");
 
     return $document