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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
# $Id$
##
# this test checks the DOM Characterdata interface of XML::LibXML
use strict;
use warnings;
use Test::More tests => 58;
use XML::LibXML;
my $doc = XML::LibXML::Document->new();
{
# 1. creation
my $foo = "foobar";
my $textnode = $doc->createTextNode($foo);
# TEST
ok( $textnode, 'creation 1');
# TEST
is( $textnode->nodeName(), '#text', 'creation 2');
# TEST
is( $textnode->nodeValue(), $foo, 'creation 3',);
# 2. substring
my $tnstr = $textnode->substringData( 1,2 );
# TEST
is( $tnstr , "oo", 'substring 1');
$tnstr = $textnode->substringData( 0,3 );
# TEST
is( $tnstr , "foo", 'substring 2');
# TEST
is( $textnode->nodeValue(), $foo, 'substring - text node unchanged' );
# 3. Expansion
$textnode->appendData( $foo );
# TEST
is( $textnode->nodeValue(), $foo . $foo, 'expansion 1');
$textnode->insertData( 6, "FOO" );
# TEST
is( $textnode->nodeValue(), $foo."FOO".$foo, 'expansion 2' );
$textnode->setData( $foo );
$textnode->insertData( 6, "FOO" );
# TEST
is( $textnode->nodeValue(), $foo."FOO", 'expansion 3');
$textnode->setData( $foo );
$textnode->insertData( 3, "" );
# TEST
is( $textnode->nodeValue(), $foo, 'Empty insertion does not change value');
# 4. Removal
$textnode->deleteData( 1,2 );
# TEST
is( $textnode->nodeValue(), "fbar", 'Removal 1');
$textnode->setData( $foo );
$textnode->deleteData( 1,10 );
# TEST
is( $textnode->nodeValue(), "f", 'Removal 2');
$textnode->setData( $foo );
$textnode->deleteData( 10,1 );
# TEST
is( $textnode->nodeValue(), $foo, 'Removal 3');
$textnode->deleteData( 1,0 );
# TEST
is( $textnode->nodeValue(), $foo, 'Removal 4');
$textnode->deleteData( 0,0 );
# TEST
is( $textnode->nodeValue(), $foo, 'Removal 5');
$textnode->deleteData( 0,2 );
# TEST
is( $textnode->nodeValue(), "obar", 'Removal 6');
# 5. Replacement
$textnode->setData( "test" );
$textnode->replaceData( 1,2, "phish" );
# TEST
is( $textnode->nodeValue(), "tphisht", 'Replacement 1');
$textnode->setData( "test" );
$textnode->replaceData( 1,4, "phish" );
# TEST
is( $textnode->nodeValue(), "tphish", 'Replacement 2');
$textnode->setData( "test" );
$textnode->replaceData( 1,0, "phish" );
# TEST
is( $textnode->nodeValue(), "tphishest", 'Replacement 3');
# 6. XML::LibXML features
$textnode->setData( "test" );
$textnode->replaceDataString( "es", "new" );
# TEST
is( $textnode->nodeValue(), "tnewt", 'replaceDataString() 1');
$textnode->replaceDataRegEx( 'n(.)w', '$1s' );
# TEST
is( $textnode->nodeValue(), "test", 'replaceDataRegEx() 2');
$textnode->setData( "blue phish, white phish, no phish" );
$textnode->replaceDataRegEx( 'phish', 'test' );
# TEST
is( $textnode->nodeValue(), "blue test, white phish, no phish",
'replaceDataRegEx 3',);
# replace them all!
$textnode->replaceDataRegEx( 'phish', 'test', 'g' );
# TEST
is( $textnode->nodeValue(), "blue test, white test, no test",
'replaceDataRegEx g',);
# check if special chars are encoded properly
$textnode->setData( "te?st" );
$textnode->replaceDataString( "e?s", 'ne\w' );
# TEST
is( $textnode->nodeValue(), 'tne\wt', ' TODO : Add test name' );
# check if "." is encoded properly
$textnode->setData( "h.thrt");
$textnode->replaceDataString( "h.t", 'new', 1 );
# TEST
is( $textnode->nodeValue(), 'newhrt', ' TODO : Add test name' );
# check if deleteDataString does not delete dots.
$textnode->setData( 'hitpit' );
$textnode->deleteDataString( 'h.t' );
# TEST
is( $textnode->nodeValue(), 'hitpit', ' TODO : Add test name' );
# check if deleteDataString works
$textnode->setData( 'hitpithit' );
$textnode->deleteDataString( 'hit' );
# TEST
is( $textnode->nodeValue(), 'pithit', ' TODO : Add test name' );
# check if deleteDataString all works
$textnode->setData( 'hitpithit' );
$textnode->deleteDataString( 'hit', 1 );
# TEST
is( $textnode->nodeValue(), 'pit', ' TODO : Add test name' );
# check if entities don't get translated
$textnode->setData(q(foo&bar));
# TEST
is ( $textnode->getData(), q(foo&bar), ' TODO : Add test name' );
}
{
# UTF-8 tests
my $test_str = "te\xDFt";
# Latin1 strings still fail.
utf8::upgrade($test_str);
# 1. creation
my $textnode = $doc->createTextNode($test_str);
# TEST
ok( $textnode, 'UTF-8 creation 1');
# TEST
is( $textnode->nodeValue(), $test_str, 'UTF-8 creation 2',);
my $foo_str = "\x{0444}oo\x{0431}ar";
$textnode = $doc->createTextNode($foo_str);
# TEST
ok( $textnode, 'UTF-8 creation 3');
# TEST
is( $textnode->nodeValue(), $foo_str, 'UTF-8 creation 4',);
# 2. substring
my $tnstr = $textnode->substringData( 1,2 );
# TEST
is( $tnstr , "oo", 'UTF-8 substring 1');
$tnstr = $textnode->substringData( 0,3 );
# TEST
is( $tnstr , "\x{0444}oo", 'UTF-8 substring 2');
# 3. Expansion
$textnode->appendData( $foo_str );
# TEST
is( $textnode->nodeValue(), $foo_str . $foo_str, 'UTF-8 expansion 1');
my $ins_str = "\x{0424}OO";
$textnode->insertData( 6, $ins_str );
# TEST
is( $textnode->nodeValue(), $foo_str.$ins_str.$foo_str,
'UTF-8 expansion 2' );
$textnode->setData( $foo_str );
$textnode->insertData( 6, $ins_str );
# TEST
is( $textnode->nodeValue(), $foo_str.$ins_str, 'UTF-8 expansion 3');
# 4. Removal
$textnode->setData( $foo_str );
$textnode->deleteData( 1,3 );
# TEST
is( $textnode->nodeValue(), "\x{0444}ar", 'UTF-8 Removal 1');
$textnode->setData( $foo_str );
$textnode->deleteData( 1,10 );
# TEST
is( $textnode->nodeValue(), "\x{0444}", 'UTF-8 Removal 2');
$textnode->setData( $foo_str );
$textnode->deleteData( 6,100 );
# TEST
is( $textnode->nodeValue(), $foo_str, 'UTF-8 Removal 3');
# 5. Replacement
my $phish_str = "ph\x{2160}sh";
$textnode->setData( $test_str );
$textnode->replaceData( 1,2, $phish_str );
# TEST
is( $textnode->nodeValue(), "t".$phish_str."t", 'UTF-8 Replacement 1');
$textnode->setData( $test_str );
$textnode->replaceData( 1,4, $phish_str );
# TEST
is( $textnode->nodeValue(), "t".$phish_str, 'UTF-8 Replacement 2');
$textnode->setData( $test_str );
$textnode->replaceData( 1,0, $phish_str );
# TEST
is( $textnode->nodeValue(), "t".$phish_str."e\xDFt",
'UTF-8 Replacement 3');
# 6. XML::LibXML features
$textnode->setData( $test_str );
my $new_str = "n\x{1D522}w";
$textnode->replaceDataString( "e\xDF", $new_str );
# TEST
is( $textnode->nodeValue(), "t".$new_str."t",
'UTF-8 replaceDataString() 1');
$textnode->replaceDataRegEx( 'n(.)w', '$1s' );
# TEST
is( $textnode->nodeValue(), "t\x{1D522}st", 'UTF-8 replaceDataRegEx() 2');
$textnode->setData( "blue $phish_str, white $phish_str, no $phish_str" );
$textnode->replaceDataRegEx( $phish_str, $test_str );
# TEST
is( $textnode->nodeValue(),
"blue $test_str, white $phish_str, no $phish_str",
'UTF-8 replaceDataRegEx 3',);
# replace them all!
$textnode->replaceDataRegEx( $phish_str, $test_str, 'g' );
# TEST
is( $textnode->nodeValue(),
"blue $test_str, white $test_str, no $test_str",
'UTF-8 replaceDataRegEx g',);
# check if deleteDataString works
my $hit_str = "hi\x{1D54B}";
my $pit_str = "\x{2119}it";
$textnode->setData( "$hit_str$pit_str$hit_str" );
$textnode->deleteDataString( $hit_str );
# TEST
is( $textnode->nodeValue(), "$pit_str$hit_str", 'UTF-8 deleteDataString 1' );
# check if deleteDataString all works
$textnode->setData( "$hit_str$pit_str$hit_str" );
$textnode->deleteDataString( $hit_str, 1 );
# TEST
is( $textnode->nodeValue(), $pit_str, 'UTF-8 deleteDataString 2' );
}
{
# standalone test
my $node = XML::LibXML::Text->new("foo");
# TEST
ok($node, ' TODO : Add test name');
# TEST
is($node->nodeValue, "foo", ' TODO : Add test name' );
}
{
# CDATA node name test
my $node = XML::LibXML::CDATASection->new("test");
# TEST
is( $node->string_value(), "test", ' TODO : Add test name' );
# TEST
is( $node->nodeName(), "#cdata-section", ' TODO : Add test name' );
}
{
# Comment node name test
my $node = XML::LibXML::Comment->new("test");
# TEST
is( $node->string_value(), "test", ' TODO : Add test name' );
# TEST
is( $node->nodeName(), "#comment", ' TODO : Add test name' );
}
{
# Document node name test
my $node = XML::LibXML::Document->new();
# TEST
is( $node->nodeName(), "#document", ' TODO : Add test name' );
}
{
# Document fragment node name test
my $node = XML::LibXML::DocumentFragment->new();
# TEST
is( $node->nodeName(), "#document-fragment", ' TODO : Add test name' );
}
|