| 1 |
# $Id$ |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use lib './t/lib'; |
| 7 |
use TestHelpers qw(slurp); |
| 8 |
use Counter; |
| 9 |
use Stacker; |
| 10 |
|
| 11 |
# Should be 25. |
| 12 |
use Test::More tests => 25; |
| 13 |
use XML::LibXML; |
| 14 |
|
| 15 |
sub _create_counter_pair |
| 16 |
{ |
| 17 |
my ($worker_cb, $predicate_cb) = @_; |
| 18 |
|
| 19 |
my $non_global_counter = Counter->new( |
| 20 |
{ |
| 21 |
gen_cb => sub { |
| 22 |
my $inc_cb = shift; |
| 23 |
return sub { |
| 24 |
return $worker_cb->( |
| 25 |
sub { |
| 26 |
if (!$predicate_cb->()) |
| 27 |
{ |
| 28 |
$inc_cb->() |
| 29 |
} |
| 30 |
return; |
| 31 |
} |
| 32 |
)->(@_); |
| 33 |
} |
| 34 |
}, |
| 35 |
} |
| 36 |
); |
| 37 |
|
| 38 |
my $global_counter = Counter->new( |
| 39 |
{ |
| 40 |
gen_cb => sub { |
| 41 |
my $inc_cb = shift; |
| 42 |
return sub { |
| 43 |
return $worker_cb->( |
| 44 |
sub { |
| 45 |
if ($predicate_cb->()) |
| 46 |
{ |
| 47 |
$inc_cb->() |
| 48 |
} |
| 49 |
return; |
| 50 |
} |
| 51 |
)->(@_); |
| 52 |
} |
| 53 |
}, |
| 54 |
} |
| 55 |
); |
| 56 |
|
| 57 |
return ($non_global_counter, $global_counter); |
| 58 |
} |
| 59 |
|
| 60 |
my ($open1_non_global_counter, $open1_global_counter) = |
| 61 |
_create_counter_pair( |
| 62 |
sub { |
| 63 |
my $cond_cb = shift; |
| 64 |
return sub { |
| 65 |
my $fn = shift; |
| 66 |
# warn("open: $f\n"); |
| 67 |
|
| 68 |
if (open my $fh, '<', $fn) |
| 69 |
{ |
| 70 |
$cond_cb->(); |
| 71 |
return $fh; |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
return 0; |
| 76 |
} |
| 77 |
}; |
| 78 |
}, |
| 79 |
sub { return defined($XML::LibXML::open_cb); }, |
| 80 |
); |
| 81 |
|
| 82 |
my $open2_counter = Counter->new( |
| 83 |
{ |
| 84 |
gen_cb => sub { |
| 85 |
my $inc_cb = shift; |
| 86 |
return sub { |
| 87 |
my ($fn) = @_; |
| 88 |
# warn("open2: $_[0]\n"); |
| 89 |
|
| 90 |
$fn =~ s/([^\d])(\.xml)$/${1}4$2/; # use a different file |
| 91 |
my ($ret, $verdict); |
| 92 |
if ($verdict = open (my $file, '<', $fn)) |
| 93 |
{ |
| 94 |
$ret = $file; |
| 95 |
} |
| 96 |
else |
| 97 |
{ |
| 98 |
$ret = 0; |
| 99 |
} |
| 100 |
|
| 101 |
$inc_cb->(); |
| 102 |
|
| 103 |
return $ret; |
| 104 |
}; |
| 105 |
}, |
| 106 |
} |
| 107 |
); |
| 108 |
|
| 109 |
my ($match1_non_global_counter, $match1_global_counter) = |
| 110 |
_create_counter_pair( |
| 111 |
sub { |
| 112 |
my $cond_cb = shift; |
| 113 |
return sub { |
| 114 |
$cond_cb->(); |
| 115 |
|
| 116 |
return 1; |
| 117 |
}; |
| 118 |
}, |
| 119 |
sub { return defined($XML::LibXML::match_cb); }, |
| 120 |
); |
| 121 |
|
| 122 |
my ($close1_non_global_counter, $close1_global_counter) = |
| 123 |
_create_counter_pair( |
| 124 |
sub { |
| 125 |
my $cond_cb = shift; |
| 126 |
return sub { |
| 127 |
my ($fh) = @_; |
| 128 |
# warn("open: $f\n"); |
| 129 |
|
| 130 |
$cond_cb->(); |
| 131 |
|
| 132 |
if ($fh) |
| 133 |
{ |
| 134 |
$fh->close(); |
| 135 |
} |
| 136 |
|
| 137 |
return 1; |
| 138 |
}; |
| 139 |
}, |
| 140 |
sub { return defined($XML::LibXML::close_cb); }, |
| 141 |
); |
| 142 |
|
| 143 |
my ($read1_non_global_counter, $read1_global_counter) = |
| 144 |
_create_counter_pair( |
| 145 |
sub { |
| 146 |
my $cond_cb = shift; |
| 147 |
return sub { |
| 148 |
my ($fh) = @_; |
| 149 |
# warn "read!"; |
| 150 |
my $rv = undef; |
| 151 |
my $n = 0; |
| 152 |
if ( $fh ) { |
| 153 |
$n = $fh->read( $rv , $_[1] ); |
| 154 |
if ($n > 0) |
| 155 |
{ |
| 156 |
$cond_cb->(); |
| 157 |
} |
| 158 |
} |
| 159 |
return $rv; |
| 160 |
}; |
| 161 |
}, |
| 162 |
sub { return defined($XML::LibXML::read_cb); }, |
| 163 |
); |
| 164 |
|
| 165 |
{ |
| 166 |
# first test checks if local callbacks work |
| 167 |
my $parser = XML::LibXML->new(); |
| 168 |
# TEST |
| 169 |
ok($parser, 'Parser was initted.'); |
| 170 |
|
| 171 |
$parser->match_callback( $match1_non_global_counter->cb() ); |
| 172 |
$parser->read_callback( $read1_non_global_counter->cb() ); |
| 173 |
$parser->open_callback( $open1_non_global_counter->cb() ); |
| 174 |
$parser->close_callback( $close1_non_global_counter->cb() ); |
| 175 |
|
| 176 |
$parser->expand_xinclude( 1 ); |
| 177 |
|
| 178 |
my $dom = $parser->parse_file("example/test.xml"); |
| 179 |
|
| 180 |
# TEST |
| 181 |
$read1_non_global_counter->test(2, 'read1 for expand_include called twice.'); |
| 182 |
# TEST |
| 183 |
$close1_non_global_counter->test(2, 'close1 for expand_include called twice.'); |
| 184 |
# TEST |
| 185 |
$match1_non_global_counter->test(2, 'match1 for expand_include called twice.'); |
| 186 |
|
| 187 |
# TEST |
| 188 |
$open1_non_global_counter->test(2, 'expand_include open1 worked.'); |
| 189 |
|
| 190 |
# TEST |
| 191 |
ok($dom, 'DOM was returned.'); |
| 192 |
# warn $dom->toString(); |
| 193 |
|
| 194 |
my $root = $dom->getDocumentElement(); |
| 195 |
|
| 196 |
my @nodes = $root->findnodes( 'xml/xsl' ); |
| 197 |
# TEST |
| 198 |
ok( scalar(@nodes), 'Found nodes.' ); |
| 199 |
} |
| 200 |
|
| 201 |
{ |
| 202 |
# test per parser callbacks. These tests must not fail! |
| 203 |
|
| 204 |
my $parser = XML::LibXML->new(); |
| 205 |
my $parser2 = XML::LibXML->new(); |
| 206 |
|
| 207 |
# TEST |
| 208 |
ok($parser, '$parser was init.'); |
| 209 |
# TEST |
| 210 |
ok($parser2, '$parser2 was init.'); |
| 211 |
|
| 212 |
$parser->match_callback( $match1_non_global_counter->cb() ); |
| 213 |
$parser->read_callback( $read1_non_global_counter->cb() ); |
| 214 |
$parser->open_callback( $open1_non_global_counter->cb() ); |
| 215 |
$parser->close_callback( $close1_non_global_counter->cb() ); |
| 216 |
|
| 217 |
$parser->expand_xinclude( 1 ); |
| 218 |
|
| 219 |
$parser2->match_callback( \&match2 ); |
| 220 |
$parser2->read_callback( \&read2 ); |
| 221 |
$parser2->open_callback( $open2_counter->cb() ); |
| 222 |
$parser2->close_callback( \&close2 ); |
| 223 |
|
| 224 |
$parser2->expand_xinclude( 1 ); |
| 225 |
|
| 226 |
my $dom1 = $parser->parse_file( "example/test.xml"); |
| 227 |
my $dom2 = $parser2->parse_file("example/test.xml"); |
| 228 |
|
| 229 |
# TEST |
| 230 |
$read1_non_global_counter->test(2, 'read1 for $parser out of ($parser,$parser2)'); |
| 231 |
# TEST |
| 232 |
$close1_non_global_counter->test(2, 'close1 for $parser out of ($parser,$parser2)'); |
| 233 |
|
| 234 |
# TEST |
| 235 |
$match1_non_global_counter->test(2, 'match1 for $parser out of ($parser,$parser2)'); |
| 236 |
# TEST |
| 237 |
$open1_non_global_counter->test(2, 'expand_include for $parser out of ($parser,$parser2)'); |
| 238 |
# TEST |
| 239 |
$open2_counter->test(2, 'expand_include for $parser2 out of ($parser,$parser2)'); |
| 240 |
# TEST |
| 241 |
ok($dom1, '$dom1 was returned'); |
| 242 |
# TEST |
| 243 |
ok($dom2, '$dom2 was returned'); |
| 244 |
|
| 245 |
my $val1 = ( $dom1->findnodes( "/x/xml/text()") )[0]->string_value(); |
| 246 |
my $val2 = ( $dom2->findnodes( "/x/xml/text()") )[0]->string_value(); |
| 247 |
|
| 248 |
$val1 =~ s/^\s*|\s*$//g; |
| 249 |
$val2 =~ s/^\s*|\s*$//g; |
| 250 |
|
| 251 |
# TEST |
| 252 |
|
| 253 |
is( $val1, "test", ' TODO : Add test name' ); |
| 254 |
# TEST |
| 255 |
is( $val2, "test 4", ' TODO : Add test name' ); |
| 256 |
} |
| 257 |
|
| 258 |
chdir("example/complex") || die "chdir: $!"; |
| 259 |
|
| 260 |
my $str = slurp('complex.xml'); |
| 261 |
|
| 262 |
{ |
| 263 |
# tests if callbacks are called correctly within DTDs |
| 264 |
my $parser2 = XML::LibXML->new(); |
| 265 |
$parser2->expand_xinclude( 1 ); |
| 266 |
my $dom = $parser2->parse_string($str); |
| 267 |
# TEST |
| 268 |
ok($dom, '$dom was init.'); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
$XML::LibXML::match_cb = $match1_global_counter->cb(); |
| 273 |
$XML::LibXML::open_cb = $open1_global_counter->cb(); |
| 274 |
$XML::LibXML::read_cb = $read1_global_counter->cb(); |
| 275 |
$XML::LibXML::close_cb = $close1_global_counter->cb(); |
| 276 |
|
| 277 |
{ |
| 278 |
# tests if global callbacks are working |
| 279 |
my $parser = XML::LibXML->new(); |
| 280 |
# TEST |
| 281 |
ok($parser, '$parser was init'); |
| 282 |
|
| 283 |
# TEST |
| 284 |
ok($parser->parse_string($str), 'parse_string returns a true value.'); |
| 285 |
|
| 286 |
# TEST |
| 287 |
$open1_global_counter->test(3, 'open1 for global counter.'); |
| 288 |
|
| 289 |
# TEST |
| 290 |
$match1_global_counter->test(3, 'match1 for global callback.'); |
| 291 |
|
| 292 |
# TEST |
| 293 |
$close1_global_counter->test(3, 'close1 for global callback.'); |
| 294 |
|
| 295 |
# TEST |
| 296 |
$read1_global_counter->test(3, 'read1 for global counter.'); |
| 297 |
} |
| 298 |
|
| 299 |
sub match2 { |
| 300 |
# warn "match2: $_[0]\n"; |
| 301 |
return 1; |
| 302 |
} |
| 303 |
|
| 304 |
sub close2 { |
| 305 |
# warn "close2 $_[0]\n"; |
| 306 |
if ( $_[0] ) { |
| 307 |
$_[0]->close(); |
| 308 |
} |
| 309 |
return 1; |
| 310 |
} |
| 311 |
|
| 312 |
sub read2 { |
| 313 |
# warn "read2!"; |
| 314 |
my $rv = undef; |
| 315 |
my $n = 0; |
| 316 |
if ( $_[0] ) { |
| 317 |
$n = $_[0]->read( $rv , $_[1] ); |
| 318 |
# warn "read!" if $n > 0; |
| 319 |
} |
| 320 |
return $rv; |
| 321 |
} |
| 322 |
|