File: modify.t

package info (click to toggle)
libxml-dom-perl 1.25-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 420 kB
  • ctags: 454
  • sloc: xml: 3,918; perl: 3,411; makefile: 46
file content (131 lines) | stat: -rw-r--r-- 2,514 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
BEGIN {print "1..16\n";}
END {print "not ok 1\n" unless $loaded;}
use XML::DOM;
use CheckAncestors;
$loaded = 1;
print "ok 1\n";

my $test = 1;
sub assert_ok
{
    my $ok = shift;
    print "not " unless $ok;
    ++$test;
    print "ok $test\n";
    $ok;
}

#Test 2

my $str = <<END;
<southpark>
<chef>
Hello children
</chef>
<children>
Hello Chef
<kenny>
Whoowhoo whoo
</kenny>
<cartman>
Shut up you loser
</cartman>
<kyle>
Cartman, you fat ass
</kyle>
</children>
</southpark>
END

my $parser = new XML::DOM::Parser;
my $doc = $parser->parse ($str);

my $chef = $doc->getElementsByTagName ("chef")->item(0);
my $kenny = $doc->getElementsByTagName ("kenny")->item(0);
my $children = $doc->getElementsByTagName ("children")->item(0);

my $stan = $doc->createElement ("stan");
$children->appendChild ($stan);
my $snap1 =$doc->toString;

my $stanlist = $doc->getElementsByTagName ("stan");
assert_ok ($stanlist->getLength == 1);

$children->appendChild ($stan);
$stanlist = $doc->getElementsByTagName ("stan");
assert_ok ($stanlist->getLength == 1);

my $snap2 = $doc->toString;
assert_ok ($snap1 eq $snap2);

# can't add Attr node directly to Element
my $attr = $doc->createAttribute ("hey", "you");
eval {
    $kenny->appendChild ($attr);
};
assert_ok ($@);

$kenny->appendChild ($stan);
assert_ok ($kenny == $stan->getParentNode);

# force hierarchy exception
eval {
    $stan->appendChild ($kenny);
};
assert_ok ($@);

# force hierarchy exception
eval {
    $stan->appendChild ($stan);
};
assert_ok ($@);

my $frag = $doc->createDocumentFragment;
$frag->appendChild ($stan);
$frag->appendChild ($kenny);
$chef->appendChild ($frag);
assert_ok ($frag->getElementsByTagName ("*")->getLength == 0);
assert_ok (not defined $frag->getParentNode);

my $kenny2 = $chef->removeChild ($kenny);
assert_ok ($kenny == $kenny2);
assert_ok (!defined $kenny->getParentNode);

# force exception - can't have 2 element nodes in a document
eval {
    $doc->appendChild ($kenny);
};
assert_ok ($@);

$doc->getDocumentElement->appendChild ($kenny);
$kenny2 = $doc->getDocumentElement->replaceChild ($stan, $kenny);
assert_ok ($kenny == $kenny2);

$doc->getDocumentElement->appendChild ($kenny);

assert_ok (CheckAncestors::doit ($doc));

$str = $doc->toString;
$str =~ tr/\012/\n/;

my $end = <<END;
<southpark>
<chef>
Hello children
</chef>
<children>
Hello Chef

<cartman>
Shut up you loser
</cartman>
<kyle>
Cartman, you fat ass
</kyle>
</children>
<stan/><kenny>
Whoowhoo whoo
</kenny></southpark>
END

assert_ok ($str eq $end);