Debian Codegraph @

t/91unique_key.t

raw
1 # -*- cperl -*-
2 # $Id$
3
4 ##
5 # This test checks that unique_key works correctly.
6 # it relies on the success of t/01basic.t, t/02parse.t,
7 # t/04node.t and namespace tests (not done yet)
8
9 use Test::More tests => 31;
10
11 use XML::LibXML;
12 use XML::LibXML::Common qw(:libxml);
13 use strict;
14 use warnings;
15 my $xmlstring = q{<foo>bar<foobar/><bar foo="foobar"/><!--foo--><![CDATA[&foo bar]]></foo>};
16
17 my $parser = XML::LibXML->new();
18 my $doc = $parser->parse_string( $xmlstring );
19
20 my $foo = $doc->documentElement;
21
22 # TEST:$num_children=5;
23 my @children_1 = $foo->childNodes;
24 my @children_2 = $foo->childNodes;
25
26 ok($children_1[0]->can('unique_key'), 'unique_key method available')
27 or exit -1;
28
29 # compare unique keys between all nodes in the above tiny document.
30 # Different nodes should have different keys; same nodes should have the same keys.
31 for my $c1(0..4){
32 for my $c2(0..4){
33 if($c1 == $c2){
34 # TEST*$num_children
35 ok($children_1[$c1]->unique_key == $children_2[$c2]->unique_key,
36 'Key for ' . $children_1[$c1]->nodeName .
37 ' matches key from same node');
38 }else{
39 # TEST*($num_children)*($num_children-1)
40 ok($children_1[$c1]->unique_key != $children_2[$c2]->unique_key,
41 'Key for ' . $children_1[$c1]->nodeName .
42 ' does not match key for' . $children_2[$c2]->nodeName);
43 }
44 }
45 }
46
47 my $foo_default_ns = XML::LibXML::Namespace->new('foo.com');
48 my $foo_ns = XML::LibXML::Namespace->new('foo.com','foo');
49 my $bar_default_ns = XML::LibXML::Namespace->new('bar.com');
50 my $bar_ns = XML::LibXML::Namespace->new('bar.com','bar');
51
52 # TEST
53 is(
54 XML::LibXML::Namespace->new('foo.com')->unique_key,
55 XML::LibXML::Namespace->new('foo.com')->unique_key,
56 'default foo ns key matches itself'
57 );
58
59 # TEST
60 isnt(
61 XML::LibXML::Namespace->new('foo.com', 'foo')->unique_key,
62 XML::LibXML::Namespace->new('foo.com', 'bar')->unique_key,
63 q[keys for ns's with different prefixes don't match]
64 );
65
66 # TEST
67 isnt(
68 XML::LibXML::Namespace->new('foo.com', 'foo')->unique_key,
69 XML::LibXML::Namespace->new('foo.com')->unique_key,
70 q[key for prefixed ns doesn't match key for default ns]
71 );
72
73 # TEST
74 isnt(
75 XML::LibXML::Namespace->new('foo.com', 'foo')->unique_key,
76 XML::LibXML::Namespace->new('bar.com', 'foo')->unique_key,
77 q[keys for ns's with different URI's don't match]
78 );
79
80 # TEST
81 isnt(
82 XML::LibXML::Namespace->new('foo.com', 'foo')->unique_key,
83 XML::LibXML::Namespace->new('bar.com', 'bar')->unique_key,
84 q[keys for ns's with different URI's and prefixes don't match]
85 );