File: 27new_callbacks_simple.t

package info (click to toggle)
libxml-libxml-perl 2.0116%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,420 kB
  • sloc: perl: 6,139; ansic: 3,752; xml: 182; sh: 64; makefile: 8
file content (225 lines) | stat: -rw-r--r-- 4,677 bytes parent folder | download | duplicates (7)
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

use strict;
use warnings;

use lib './t/lib';

use Counter;

# $Id$

# Should be 14.
use Test::More tests => 14;

use XML::LibXML;
use IO::File;

# --------------------------------------------------------------------- #
# simple test
# --------------------------------------------------------------------- #
my $string = <<EOF;
<x xmlns:xinclude="http://www.w3.org/2001/XInclude"><xml>test<xinclude:include href="/example/test2.xml"/></xml></x>
EOF

my $icb    = XML::LibXML::InputCallback->new();
# TEST
ok($icb, ' TODO : Add test name');

my $match_file_counter = Counter->new(
    {
        gen_cb => sub {
            my $inc_cb = shift;

            sub {
                my $uri = shift;
                if ( $uri =~ /^\/example\// ){
                    $inc_cb->();
                    return 1;
                }
                return 0;
            }
        }
    }
);

my $open_file_counter = Counter->new(
    {
        gen_cb => sub {
            my $inc_cb = shift;

            sub {
                my $uri = shift;
                open my $file, '<', ".$uri"
                    or die "Cannot open '.$uri'";
                $inc_cb->();
                return $file;
            }
        }
    }
);

my $read_file_counter = Counter->new(
    {
        gen_cb => sub {
            my $inc_cb = shift;

            sub {
                my $h   = shift;
                my $buflen = shift;
                my $rv   = undef;

                $inc_cb->();
                my $n = $h->read( $rv , $buflen );

                return $rv;
            }
        }
    }
);

my $close_file_counter = Counter->new(
    {
        gen_cb => sub {
            my $inc_cb = shift;

            sub {
                my $h   = shift;
                $inc_cb->();
                $h->close();
                return 1;

            };
        }
    }
);

my $match_hash_counter = Counter->new(
    {
        gen_cb => sub {
            my $inc_cb = shift;

            sub {
                my $uri = shift;
                if ( $uri =~ /^\/example\// ){
                    $inc_cb->();
                    return 1;
                }
                return 0;
            }
        }
    }
);

my $open_hash_counter = Counter->new(
    {
        gen_cb => sub {
            my $inc_cb = shift;

            sub {
                my $uri = shift;
                my $hash = { line => 0,
                    lines => [ "<foo>", "bar", "<xsl/>", "..", "</foo>" ],
                };
                $inc_cb->();

                return $hash;
            }
        }
    }
);

my $close_hash_counter = Counter->new(
    {
        gen_cb => sub {
            my $inc_cb = shift;

            sub {
                my $h   = shift;
                undef $h;
                $inc_cb->();

                return;
            }
        }
    }
);

my $read_hash_counter = Counter->new(
    {
        gen_cb => sub {
            my $inc_cb = shift;

            sub {
                my $h   = shift;
                my $buflen = shift;

                my $id = $h->{line};
                $h->{line} += 1;
                my $rv= $h->{lines}->[$id];

                $rv = "" unless defined $rv;

                $inc_cb->();

                return $rv;
            }
        }
    }
);

$icb->register_callbacks( [ $match_file_counter->cb(), $open_file_counter->cb(),
                            $read_file_counter->cb(), $close_file_counter->cb() ] );

my $parser = XML::LibXML->new();
$parser->expand_xinclude(1);
$parser->input_callbacks($icb);
my $doc = $parser->parse_string($string);

# TEST
$match_file_counter->test(1, 'match_file matched once.');

# TEST
$open_file_counter->test(1, 'open_file called once.');

# TEST
$read_file_counter->test(2, 'read_file called twice.');

# TEST
$close_file_counter->test(1, 'close_file called once.');

# TEST
ok($doc, ' TODO : Add test name');
# TEST

is($doc->string_value(),"test..", ' TODO : Add test name');

my $icb2    = XML::LibXML::InputCallback->new();

# TEST
ok($icb2, ' TODO : Add test name');

$icb2->register_callbacks( [ $match_hash_counter->cb(), $open_hash_counter->cb(),
                             $read_hash_counter->cb(), $close_hash_counter->cb() ] );

$parser->input_callbacks($icb2);
$doc = $parser->parse_string($string);

# TEST
$match_hash_counter->test(1, 'match_hash matched once.');

# TEST
$open_hash_counter->test(1, 'open_hash called once.');

# TEST
$read_hash_counter->test(6, 'read_hash called six times.');

# TEST
$close_hash_counter->test(1, 'close_hash called once.');

# TEST
ok($doc, ' TODO : Add test name');

# TEST

is($doc->string_value(),"testbar..", ' TODO : Add test name');