| 1 |
|
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
use lib './t/lib'; |
| 6 |
|
| 7 |
use Counter; |
| 8 |
|
| 9 |
# $Id$ |
| 10 |
|
| 11 |
# Should be 14. |
| 12 |
use Test::More tests => 14; |
| 13 |
|
| 14 |
use XML::LibXML; |
| 15 |
use IO::File; |
| 16 |
|
| 17 |
# --------------------------------------------------------------------- # |
| 18 |
# simple test |
| 19 |
# --------------------------------------------------------------------- # |
| 20 |
my $string = <<EOF; |
| 21 |
<x xmlns:xinclude="http://www.w3.org/2001/XInclude"><xml>test<xinclude:include href="/example/test2.xml"/></xml></x> |
| 22 |
EOF |
| 23 |
|
| 24 |
my $icb = XML::LibXML::InputCallback->new(); |
| 25 |
# TEST |
| 26 |
ok($icb, ' TODO : Add test name'); |
| 27 |
|
| 28 |
my $match_file_counter = Counter->new( |
| 29 |
{ |
| 30 |
gen_cb => sub { |
| 31 |
my $inc_cb = shift; |
| 32 |
|
| 33 |
sub { |
| 34 |
my $uri = shift; |
| 35 |
if ( $uri =~ /^\/example\// ){ |
| 36 |
$inc_cb->(); |
| 37 |
return 1; |
| 38 |
} |
| 39 |
return 0; |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
); |
| 44 |
|
| 45 |
my $open_file_counter = Counter->new( |
| 46 |
{ |
| 47 |
gen_cb => sub { |
| 48 |
my $inc_cb = shift; |
| 49 |
|
| 50 |
sub { |
| 51 |
my $uri = shift; |
| 52 |
open my $file, '<', ".$uri" |
| 53 |
or die "Cannot open '.$uri'"; |
| 54 |
$inc_cb->(); |
| 55 |
return $file; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
); |
| 60 |
|
| 61 |
my $read_file_counter = Counter->new( |
| 62 |
{ |
| 63 |
gen_cb => sub { |
| 64 |
my $inc_cb = shift; |
| 65 |
|
| 66 |
sub { |
| 67 |
my $h = shift; |
| 68 |
my $buflen = shift; |
| 69 |
my $rv = undef; |
| 70 |
|
| 71 |
$inc_cb->(); |
| 72 |
my $n = $h->read( $rv , $buflen ); |
| 73 |
|
| 74 |
return $rv; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
); |
| 79 |
|
| 80 |
my $close_file_counter = Counter->new( |
| 81 |
{ |
| 82 |
gen_cb => sub { |
| 83 |
my $inc_cb = shift; |
| 84 |
|
| 85 |
sub { |
| 86 |
my $h = shift; |
| 87 |
$inc_cb->(); |
| 88 |
$h->close(); |
| 89 |
return 1; |
| 90 |
|
| 91 |
}; |
| 92 |
} |
| 93 |
} |
| 94 |
); |
| 95 |
|
| 96 |
my $match_hash_counter = Counter->new( |
| 97 |
{ |
| 98 |
gen_cb => sub { |
| 99 |
my $inc_cb = shift; |
| 100 |
|
| 101 |
sub { |
| 102 |
my $uri = shift; |
| 103 |
if ( $uri =~ /^\/example\// ){ |
| 104 |
$inc_cb->(); |
| 105 |
return 1; |
| 106 |
} |
| 107 |
return 0; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
); |
| 112 |
|
| 113 |
my $open_hash_counter = Counter->new( |
| 114 |
{ |
| 115 |
gen_cb => sub { |
| 116 |
my $inc_cb = shift; |
| 117 |
|
| 118 |
sub { |
| 119 |
my $uri = shift; |
| 120 |
my $hash = { line => 0, |
| 121 |
lines => [ "<foo>", "bar", "<xsl/>", "..", "</foo>" ], |
| 122 |
}; |
| 123 |
$inc_cb->(); |
| 124 |
|
| 125 |
return $hash; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
); |
| 130 |
|
| 131 |
my $close_hash_counter = Counter->new( |
| 132 |
{ |
| 133 |
gen_cb => sub { |
| 134 |
my $inc_cb = shift; |
| 135 |
|
| 136 |
sub { |
| 137 |
my $h = shift; |
| 138 |
undef $h; |
| 139 |
$inc_cb->(); |
| 140 |
|
| 141 |
return; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
); |
| 146 |
|
| 147 |
my $read_hash_counter = Counter->new( |
| 148 |
{ |
| 149 |
gen_cb => sub { |
| 150 |
my $inc_cb = shift; |
| 151 |
|
| 152 |
sub { |
| 153 |
my $h = shift; |
| 154 |
my $buflen = shift; |
| 155 |
|
| 156 |
my $id = $h->{line}; |
| 157 |
$h->{line} += 1; |
| 158 |
my $rv= $h->{lines}->[$id]; |
| 159 |
|
| 160 |
$rv = "" unless defined $rv; |
| 161 |
|
| 162 |
$inc_cb->(); |
| 163 |
|
| 164 |
return $rv; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
); |
| 169 |
|
| 170 |
$icb->register_callbacks( [ $match_file_counter->cb(), $open_file_counter->cb(), |
| 171 |
$read_file_counter->cb(), $close_file_counter->cb() ] ); |
| 172 |
|
| 173 |
my $parser = XML::LibXML->new(); |
| 174 |
$parser->expand_xinclude(1); |
| 175 |
$parser->input_callbacks($icb); |
| 176 |
my $doc = $parser->parse_string($string); |
| 177 |
|
| 178 |
# TEST |
| 179 |
$match_file_counter->test(1, 'match_file matched once.'); |
| 180 |
|
| 181 |
# TEST |
| 182 |
$open_file_counter->test(1, 'open_file called once.'); |
| 183 |
|
| 184 |
# TEST |
| 185 |
$read_file_counter->test(2, 'read_file called twice.'); |
| 186 |
|
| 187 |
# TEST |
| 188 |
$close_file_counter->test(1, 'close_file called once.'); |
| 189 |
|
| 190 |
# TEST |
| 191 |
ok($doc, ' TODO : Add test name'); |
| 192 |
# TEST |
| 193 |
|
| 194 |
is($doc->string_value(),"test..", ' TODO : Add test name'); |
| 195 |
|
| 196 |
my $icb2 = XML::LibXML::InputCallback->new(); |
| 197 |
|
| 198 |
# TEST |
| 199 |
ok($icb2, ' TODO : Add test name'); |
| 200 |
|
| 201 |
$icb2->register_callbacks( [ $match_hash_counter->cb(), $open_hash_counter->cb(), |
| 202 |
$read_hash_counter->cb(), $close_hash_counter->cb() ] ); |
| 203 |
|
| 204 |
$parser->input_callbacks($icb2); |
| 205 |
$doc = $parser->parse_string($string); |
| 206 |
|
| 207 |
# TEST |
| 208 |
$match_hash_counter->test(1, 'match_hash matched once.'); |
| 209 |
|
| 210 |
# TEST |
| 211 |
$open_hash_counter->test(1, 'open_hash called once.'); |
| 212 |
|
| 213 |
# TEST |
| 214 |
$read_hash_counter->test(6, 'read_hash called six times.'); |
| 215 |
|
| 216 |
# TEST |
| 217 |
$close_hash_counter->test(1, 'close_hash called once.'); |
| 218 |
|
| 219 |
# TEST |
| 220 |
ok($doc, ' TODO : Add test name'); |
| 221 |
|
| 222 |
# TEST |
| 223 |
|
| 224 |
is($doc->string_value(),"testbar..", ' TODO : Add test name'); |
| 225 |
|