Commits

garfieldnate committed 5f92439

Added a unique_key method for Namespace object which is compatible with the unique_key method for Node objects. Includes tests and documentation.

Comments (0)

Files changed (3)

     OUTPUT:
         RETVAL
 
+SV*
+unique_key( self )
+        SV * self
+    PREINIT:
+        xmlNsPtr ns = INT2PTR(xmlNsPtr,SvIV(SvRV(self)));
+        xmlChar* key;
+    CODE:
+        /* Concatenate prefix and URI with vertical bar dividing*/
+        key = xmlStrdup(ns->prefix);
+        key = xmlStrcat(key, (const xmlChar*)"|");
+        key = xmlStrcat(key, ns->href);
+        RETVAL = C2Sv(key, NULL);
+    OUTPUT:
+        RETVAL
+
 int
 _isEqual(self, ref_node)
        SV * self
                     <para>Returns the string "xmlns"</para>
                 </listitem>
             </varlistentry>
+
+            <varlistentry>
+                <term>unique_key</term>
+
+                <listitem>
+                    <funcsynopsis>
+                        <funcsynopsisinfo>$key = $ns-&gt;unique_key();</funcsynopsisinfo>
+                    </funcsynopsis>
+
+                    <para>This method returns a key guaranteed to be unique for this namespace, and to always be the same value for this namespace. Two namespace objects return the same key if and only if they have the same prefix and the same URI. The returned key value is useful as a key in hashes.</para>
+                </listitem>
+            </varlistentry>
         </variablelist>
 	</sect1>
     </chapter>
 
 ##
 # This test checks that unique_key works correctly.
-# it relies on the success of t/01basic.t, t/02parse.t, and t/04node.t
+# it relies on the success of t/01basic.t, t/02parse.t,
+# t/04node.t and namespace tests (not done yet)
 
-use Test::More tests => 26;
+use Test::More tests => 31;
 
 use XML::LibXML;
 use XML::LibXML::Common qw(:libxml);
                 ' does not match key for' . $children_2[$c2]->nodeName);
         }
     }
-}
+}
+
+my $foo_default_ns = XML::LibXML::Namespace->new('foo.com');
+my $foo_ns = XML::LibXML::Namespace->new('foo.com','foo');
+my $bar_default_ns = XML::LibXML::Namespace->new('bar.com');
+my $bar_ns = XML::LibXML::Namespace->new('bar.com','bar');
+
+# TEST
+is(
+    XML::LibXML::Namespace->new('foo.com')->unique_key,
+    XML::LibXML::Namespace->new('foo.com')->unique_key,
+    'default foo ns key matches itself'
+);
+
+# TEST
+isnt(
+    XML::LibXML::Namespace->new('foo.com', 'foo')->unique_key,
+    XML::LibXML::Namespace->new('foo.com', 'bar')->unique_key,
+    q[keys for ns's with different prefixes don't match]
+);
+
+# TEST
+isnt(
+    XML::LibXML::Namespace->new('foo.com', 'foo')->unique_key,
+    XML::LibXML::Namespace->new('foo.com')->unique_key,
+    q[key for prefixed ns doesn't match key for default ns]
+);
+
+# TEST
+isnt(
+    XML::LibXML::Namespace->new('foo.com', 'foo')->unique_key,
+    XML::LibXML::Namespace->new('bar.com', 'foo')->unique_key,
+    q[keys for ns's with different URI's don't match]
+);
+
+# TEST
+isnt(
+    XML::LibXML::Namespace->new('foo.com', 'foo')->unique_key,
+    XML::LibXML::Namespace->new('bar.com', 'bar')->unique_key,
+    q[keys for ns's with different URI's and prefixes don't match]
+);