| 1 |
# $Id$ |
| 2 |
|
| 3 |
## |
| 4 |
# this test checks the DOM Characterdata interface of XML::LibXML |
| 5 |
|
| 6 |
use strict; |
| 7 |
use warnings; |
| 8 |
|
| 9 |
use Test::More tests => 59; |
| 10 |
|
| 11 |
use XML::LibXML; |
| 12 |
|
| 13 |
my $doc = XML::LibXML::Document->new(); |
| 14 |
|
| 15 |
{ |
| 16 |
# 1. creation |
| 17 |
my $foo = "foobar"; |
| 18 |
my $textnode = $doc->createTextNode($foo); |
| 19 |
# TEST |
| 20 |
ok( $textnode, 'creation 1'); |
| 21 |
# TEST |
| 22 |
is( $textnode->nodeName(), '#text', 'creation 2'); |
| 23 |
# TEST |
| 24 |
is( $textnode->nodeValue(), $foo, 'creation 3',); |
| 25 |
|
| 26 |
{ |
| 27 |
# Test for https://rt.cpan.org/Ticket/Display.html?id=112470 |
| 28 |
my @attributes = $textnode->attributes(); |
| 29 |
# TEST |
| 30 |
is_deeply( |
| 31 |
(\@attributes), |
| 32 |
[], |
| 33 |
'::Text->attributes() returns an empty list in list context (RT#112470)', |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
# 2. substring |
| 38 |
my $tnstr = $textnode->substringData( 1,2 ); |
| 39 |
# TEST |
| 40 |
is( $tnstr , "oo", 'substring 1'); |
| 41 |
$tnstr = $textnode->substringData( 0,3 ); |
| 42 |
# TEST |
| 43 |
is( $tnstr , "foo", 'substring 2'); |
| 44 |
# TEST |
| 45 |
is( $textnode->nodeValue(), $foo, 'substring - text node unchanged' ); |
| 46 |
|
| 47 |
# 3. Expansion |
| 48 |
$textnode->appendData( $foo ); |
| 49 |
# TEST |
| 50 |
is( $textnode->nodeValue(), $foo . $foo, 'expansion 1'); |
| 51 |
|
| 52 |
$textnode->insertData( 6, "FOO" ); |
| 53 |
# TEST |
| 54 |
is( $textnode->nodeValue(), $foo."FOO".$foo, 'expansion 2' ); |
| 55 |
|
| 56 |
$textnode->setData( $foo ); |
| 57 |
$textnode->insertData( 6, "FOO" ); |
| 58 |
# TEST |
| 59 |
is( $textnode->nodeValue(), $foo."FOO", 'expansion 3'); |
| 60 |
$textnode->setData( $foo ); |
| 61 |
$textnode->insertData( 3, "" ); |
| 62 |
# TEST |
| 63 |
is( $textnode->nodeValue(), $foo, 'Empty insertion does not change value'); |
| 64 |
|
| 65 |
# 4. Removal |
| 66 |
$textnode->deleteData( 1,2 ); |
| 67 |
# TEST |
| 68 |
is( $textnode->nodeValue(), "fbar", 'Removal 1'); |
| 69 |
$textnode->setData( $foo ); |
| 70 |
$textnode->deleteData( 1,10 ); |
| 71 |
# TEST |
| 72 |
is( $textnode->nodeValue(), "f", 'Removal 2'); |
| 73 |
$textnode->setData( $foo ); |
| 74 |
$textnode->deleteData( 10,1 ); |
| 75 |
# TEST |
| 76 |
is( $textnode->nodeValue(), $foo, 'Removal 3'); |
| 77 |
$textnode->deleteData( 1,0 ); |
| 78 |
# TEST |
| 79 |
is( $textnode->nodeValue(), $foo, 'Removal 4'); |
| 80 |
$textnode->deleteData( 0,0 ); |
| 81 |
# TEST |
| 82 |
is( $textnode->nodeValue(), $foo, 'Removal 5'); |
| 83 |
$textnode->deleteData( 0,2 ); |
| 84 |
# TEST |
| 85 |
is( $textnode->nodeValue(), "obar", 'Removal 6'); |
| 86 |
|
| 87 |
# 5. Replacement |
| 88 |
$textnode->setData( "test" ); |
| 89 |
$textnode->replaceData( 1,2, "phish" ); |
| 90 |
# TEST |
| 91 |
is( $textnode->nodeValue(), "tphisht", 'Replacement 1'); |
| 92 |
$textnode->setData( "test" ); |
| 93 |
$textnode->replaceData( 1,4, "phish" ); |
| 94 |
# TEST |
| 95 |
is( $textnode->nodeValue(), "tphish", 'Replacement 2'); |
| 96 |
$textnode->setData( "test" ); |
| 97 |
$textnode->replaceData( 1,0, "phish" ); |
| 98 |
# TEST |
| 99 |
is( $textnode->nodeValue(), "tphishest", 'Replacement 3'); |
| 100 |
|
| 101 |
|
| 102 |
# 6. XML::LibXML features |
| 103 |
$textnode->setData( "test" ); |
| 104 |
|
| 105 |
$textnode->replaceDataString( "es", "new" ); |
| 106 |
# TEST |
| 107 |
is( $textnode->nodeValue(), "tnewt", 'replaceDataString() 1'); |
| 108 |
|
| 109 |
$textnode->replaceDataRegEx( 'n(.)w', '$1s' ); |
| 110 |
# TEST |
| 111 |
is( $textnode->nodeValue(), "test", 'replaceDataRegEx() 2'); |
| 112 |
|
| 113 |
$textnode->setData( "blue phish, white phish, no phish" ); |
| 114 |
$textnode->replaceDataRegEx( 'phish', 'test' ); |
| 115 |
# TEST |
| 116 |
is( $textnode->nodeValue(), "blue test, white phish, no phish", |
| 117 |
'replaceDataRegEx 3',); |
| 118 |
|
| 119 |
# replace them all! |
| 120 |
$textnode->replaceDataRegEx( 'phish', 'test', 'g' ); |
| 121 |
# TEST |
| 122 |
is( $textnode->nodeValue(), "blue test, white test, no test", |
| 123 |
'replaceDataRegEx g',); |
| 124 |
|
| 125 |
# check if special chars are encoded properly |
| 126 |
$textnode->setData( "te?st" ); |
| 127 |
$textnode->replaceDataString( "e?s", 'ne\w' ); |
| 128 |
# TEST |
| 129 |
is( $textnode->nodeValue(), 'tne\wt', ' TODO : Add test name' ); |
| 130 |
|
| 131 |
# check if "." is encoded properly |
| 132 |
$textnode->setData( "h.thrt"); |
| 133 |
$textnode->replaceDataString( "h.t", 'new', 1 ); |
| 134 |
# TEST |
| 135 |
is( $textnode->nodeValue(), 'newhrt', ' TODO : Add test name' ); |
| 136 |
|
| 137 |
# check if deleteDataString does not delete dots. |
| 138 |
$textnode->setData( 'hitpit' ); |
| 139 |
$textnode->deleteDataString( 'h.t' ); |
| 140 |
# TEST |
| 141 |
is( $textnode->nodeValue(), 'hitpit', ' TODO : Add test name' ); |
| 142 |
|
| 143 |
# check if deleteDataString works |
| 144 |
$textnode->setData( 'hitpithit' ); |
| 145 |
$textnode->deleteDataString( 'hit' ); |
| 146 |
# TEST |
| 147 |
is( $textnode->nodeValue(), 'pithit', ' TODO : Add test name' ); |
| 148 |
|
| 149 |
# check if deleteDataString all works |
| 150 |
$textnode->setData( 'hitpithit' ); |
| 151 |
$textnode->deleteDataString( 'hit', 1 ); |
| 152 |
# TEST |
| 153 |
is( $textnode->nodeValue(), 'pit', ' TODO : Add test name' ); |
| 154 |
|
| 155 |
# check if entities don't get translated |
| 156 |
$textnode->setData(q(foo&bar)); |
| 157 |
# TEST |
| 158 |
is ( $textnode->getData(), q(foo&bar), ' TODO : Add test name' ); |
| 159 |
} |
| 160 |
|
| 161 |
{ |
| 162 |
# UTF-8 tests |
| 163 |
|
| 164 |
my $test_str = "te\xDFt"; |
| 165 |
# Latin1 strings still fail. |
| 166 |
utf8::upgrade($test_str); |
| 167 |
|
| 168 |
# 1. creation |
| 169 |
my $textnode = $doc->createTextNode($test_str); |
| 170 |
# TEST |
| 171 |
ok( $textnode, 'UTF-8 creation 1'); |
| 172 |
# TEST |
| 173 |
is( $textnode->nodeValue(), $test_str, 'UTF-8 creation 2',); |
| 174 |
my $foo_str = "\x{0444}oo\x{0431}ar"; |
| 175 |
$textnode = $doc->createTextNode($foo_str); |
| 176 |
# TEST |
| 177 |
ok( $textnode, 'UTF-8 creation 3'); |
| 178 |
# TEST |
| 179 |
is( $textnode->nodeValue(), $foo_str, 'UTF-8 creation 4',); |
| 180 |
|
| 181 |
# 2. substring |
| 182 |
my $tnstr = $textnode->substringData( 1,2 ); |
| 183 |
# TEST |
| 184 |
is( $tnstr , "oo", 'UTF-8 substring 1'); |
| 185 |
$tnstr = $textnode->substringData( 0,3 ); |
| 186 |
# TEST |
| 187 |
is( $tnstr , "\x{0444}oo", 'UTF-8 substring 2'); |
| 188 |
|
| 189 |
# 3. Expansion |
| 190 |
$textnode->appendData( $foo_str ); |
| 191 |
# TEST |
| 192 |
is( $textnode->nodeValue(), $foo_str . $foo_str, 'UTF-8 expansion 1'); |
| 193 |
|
| 194 |
my $ins_str = "\x{0424}OO"; |
| 195 |
$textnode->insertData( 6, $ins_str ); |
| 196 |
# TEST |
| 197 |
is( $textnode->nodeValue(), $foo_str.$ins_str.$foo_str, |
| 198 |
'UTF-8 expansion 2' ); |
| 199 |
|
| 200 |
$textnode->setData( $foo_str ); |
| 201 |
$textnode->insertData( 6, $ins_str ); |
| 202 |
# TEST |
| 203 |
is( $textnode->nodeValue(), $foo_str.$ins_str, 'UTF-8 expansion 3'); |
| 204 |
|
| 205 |
# 4. Removal |
| 206 |
$textnode->setData( $foo_str ); |
| 207 |
$textnode->deleteData( 1,3 ); |
| 208 |
# TEST |
| 209 |
is( $textnode->nodeValue(), "\x{0444}ar", 'UTF-8 Removal 1'); |
| 210 |
$textnode->setData( $foo_str ); |
| 211 |
$textnode->deleteData( 1,10 ); |
| 212 |
# TEST |
| 213 |
is( $textnode->nodeValue(), "\x{0444}", 'UTF-8 Removal 2'); |
| 214 |
$textnode->setData( $foo_str ); |
| 215 |
$textnode->deleteData( 6,100 ); |
| 216 |
# TEST |
| 217 |
is( $textnode->nodeValue(), $foo_str, 'UTF-8 Removal 3'); |
| 218 |
|
| 219 |
# 5. Replacement |
| 220 |
my $phish_str = "ph\x{2160}sh"; |
| 221 |
$textnode->setData( $test_str ); |
| 222 |
$textnode->replaceData( 1,2, $phish_str ); |
| 223 |
# TEST |
| 224 |
is( $textnode->nodeValue(), "t".$phish_str."t", 'UTF-8 Replacement 1'); |
| 225 |
$textnode->setData( $test_str ); |
| 226 |
$textnode->replaceData( 1,4, $phish_str ); |
| 227 |
# TEST |
| 228 |
is( $textnode->nodeValue(), "t".$phish_str, 'UTF-8 Replacement 2'); |
| 229 |
$textnode->setData( $test_str ); |
| 230 |
$textnode->replaceData( 1,0, $phish_str ); |
| 231 |
# TEST |
| 232 |
is( $textnode->nodeValue(), "t".$phish_str."e\xDFt", |
| 233 |
'UTF-8 Replacement 3'); |
| 234 |
|
| 235 |
# 6. XML::LibXML features |
| 236 |
$textnode->setData( $test_str ); |
| 237 |
|
| 238 |
my $new_str = "n\x{1D522}w"; |
| 239 |
$textnode->replaceDataString( "e\xDF", $new_str ); |
| 240 |
# TEST |
| 241 |
is( $textnode->nodeValue(), "t".$new_str."t", |
| 242 |
'UTF-8 replaceDataString() 1'); |
| 243 |
|
| 244 |
$textnode->replaceDataRegEx( 'n(.)w', '$1s' ); |
| 245 |
# TEST |
| 246 |
is( $textnode->nodeValue(), "t\x{1D522}st", 'UTF-8 replaceDataRegEx() 2'); |
| 247 |
|
| 248 |
$textnode->setData( "blue $phish_str, white $phish_str, no $phish_str" ); |
| 249 |
$textnode->replaceDataRegEx( $phish_str, $test_str ); |
| 250 |
# TEST |
| 251 |
is( $textnode->nodeValue(), |
| 252 |
"blue $test_str, white $phish_str, no $phish_str", |
| 253 |
'UTF-8 replaceDataRegEx 3',); |
| 254 |
|
| 255 |
# replace them all! |
| 256 |
$textnode->replaceDataRegEx( $phish_str, $test_str, 'g' ); |
| 257 |
# TEST |
| 258 |
is( $textnode->nodeValue(), |
| 259 |
"blue $test_str, white $test_str, no $test_str", |
| 260 |
'UTF-8 replaceDataRegEx g',); |
| 261 |
|
| 262 |
# check if deleteDataString works |
| 263 |
my $hit_str = "hi\x{1D54B}"; |
| 264 |
my $pit_str = "\x{2119}it"; |
| 265 |
$textnode->setData( "$hit_str$pit_str$hit_str" ); |
| 266 |
$textnode->deleteDataString( $hit_str ); |
| 267 |
# TEST |
| 268 |
is( $textnode->nodeValue(), "$pit_str$hit_str", 'UTF-8 deleteDataString 1' ); |
| 269 |
|
| 270 |
# check if deleteDataString all works |
| 271 |
$textnode->setData( "$hit_str$pit_str$hit_str" ); |
| 272 |
$textnode->deleteDataString( $hit_str, 1 ); |
| 273 |
# TEST |
| 274 |
is( $textnode->nodeValue(), $pit_str, 'UTF-8 deleteDataString 2' ); |
| 275 |
} |
| 276 |
|
| 277 |
{ |
| 278 |
# standalone test |
| 279 |
my $node = XML::LibXML::Text->new("foo"); |
| 280 |
# TEST |
| 281 |
ok($node, ' TODO : Add test name'); |
| 282 |
# TEST |
| 283 |
is($node->nodeValue, "foo", ' TODO : Add test name' ); |
| 284 |
} |
| 285 |
|
| 286 |
{ |
| 287 |
# CDATA node name test |
| 288 |
|
| 289 |
my $node = XML::LibXML::CDATASection->new("test"); |
| 290 |
|
| 291 |
# TEST |
| 292 |
is( $node->string_value(), "test", ' TODO : Add test name' ); |
| 293 |
# TEST |
| 294 |
is( $node->nodeName(), "#cdata-section", ' TODO : Add test name' ); |
| 295 |
} |
| 296 |
|
| 297 |
{ |
| 298 |
# Comment node name test |
| 299 |
|
| 300 |
my $node = XML::LibXML::Comment->new("test"); |
| 301 |
|
| 302 |
# TEST |
| 303 |
is( $node->string_value(), "test", ' TODO : Add test name' ); |
| 304 |
# TEST |
| 305 |
is( $node->nodeName(), "#comment", ' TODO : Add test name' ); |
| 306 |
} |
| 307 |
|
| 308 |
{ |
| 309 |
# Document node name test |
| 310 |
|
| 311 |
my $node = XML::LibXML::Document->new(); |
| 312 |
|
| 313 |
# TEST |
| 314 |
is( $node->nodeName(), "#document", ' TODO : Add test name' ); |
| 315 |
} |
| 316 |
{ |
| 317 |
# Document fragment node name test |
| 318 |
|
| 319 |
my $node = XML::LibXML::DocumentFragment->new(); |
| 320 |
|
| 321 |
# TEST |
| 322 |
is( $node->nodeName(), "#document-fragment", ' TODO : Add test name' ); |
| 323 |
} |