perl-XML-LibXML / t / 14sax.t

  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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use strict;
use warnings;

use lib './t/lib';

use Counter;
use Stacker;

# should be 31.
use Test::More tests => 31;

# BEGIN { plan tests => 55 }

use XML::LibXML;
use XML::LibXML::SAX;
use XML::LibXML::SAX::Parser;
use XML::LibXML::SAX::Builder;
use XML::SAX;
use IO::File;
# TEST
ok(1, 'Loaded');

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

                sub {
                    $inc_cb->();
                    return;    
                }
            }
        }
    );
}

my $SAXTester_start_document_counter = _create_simple_counter();
my $SAXTester_end_document_counter = _create_simple_counter();

my $SAXTester_start_element_stacker = Stacker->new(
    {
        gen_cb => sub {
            my $push_cb = shift;
            return sub {
                my $el = shift;

                $push_cb->(
                    ($el->{LocalName} =~ m{\A(?:dromedaries|species|humps|disposition|legs)\z}) 
                    ? 'true'
                    : 'false'
                );

                return;
            };
        },
    }
);

my $SAXNSTester_start_element_stacker = Stacker->new(
    {
        gen_cb => sub {
            my $push_cb = shift;
            return sub {
                my $node = shift;

                $push_cb->(
                    scalar($node->{NamespaceURI} =~ /^urn:/)
                    ? 'true'
                    : 'false'
                );

                return;
            };
        },
    }
);

my $SAXNS2Tester_start_element_stacker = Stacker->new(
    {
        gen_cb => sub {
            my $push_cb = shift;
            return sub {
                my $elt = shift;

                if ($elt->{Name} eq "b")
                {
                    $push_cb->(
                        ($elt->{NamespaceURI} eq "xml://A") ? 'true' : 'false'
                    );
                }

                return;
            };
        },
    }
);


sub _create_urn_stacker
{
    return
    Stacker->new(
        {
            gen_cb => sub {
                my $push_cb = shift;
                return sub {
                    my $node = shift;

                    $push_cb->(
                        ($node->{NamespaceURI} =~ /\A(?:urn:camels|urn:mammals|urn:a)\z/)
                        ? 'true'
                        : 'false'
                    );

                    return;
                };
            },
        }
    );
}

my $SAXNSTester_start_prefix_mapping_stacker = _create_urn_stacker();
my $SAXNSTester_end_prefix_mapping_stacker = _create_urn_stacker();

# TEST
ok(XML::SAX->add_parser(q(XML::LibXML::SAX::Parser)), 'add_parser is successful.');

local $XML::SAX::ParserPackage = 'XML::LibXML::SAX::Parser';

my $parser;
{
    my $sax = SAXTester->new;
    # TEST
    ok($sax, ' TODO : Add test name');

    my $str = join('', IO::File->new("example/dromeds.xml")->getlines);
    my $doc = XML::LibXML->new->parse_string($str);
    # TEST
    ok($doc, ' TODO : Add test name');

    my $generator = XML::LibXML::SAX::Parser->new(Handler => $sax);
    # TEST
    ok($generator, ' TODO : Add test name');

    $generator->generate($doc); # start_element*10

    # TEST
    $SAXTester_start_element_stacker->test(
        [(qw(true)) x 10],
        'start_element was successful 10 times.',
    );
    # TEST
    $SAXTester_start_document_counter->test(1, 'start_document called once.');
    # TEST
    $SAXTester_end_document_counter->test(1, 'end_document called once.');

    my $builder = XML::LibXML::SAX::Builder->new();
    # TEST
    ok($builder, ' TODO : Add test name');
    my $gen2 = XML::LibXML::SAX::Parser->new(Handler => $builder);
    my $dom2 = $gen2->generate($doc);
    # TEST
    ok($dom2, ' TODO : Add test name');

    # TEST
    is($dom2->toString, $str, ' TODO : Add test name');
    # warn($dom2->toString);

########### XML::SAX Tests ###########
    $parser = XML::SAX::ParserFactory->parser(Handler => $sax);
    # TEST
    ok($parser, ' TODO : Add test name');
    $parser->parse_uri("example/dromeds.xml"); # start_element*10

    # TEST
    $SAXTester_start_element_stacker->test(
        [(qw(true)) x 10],
        'parse_uri(): start_element was successful 10 times.',
    );
    # TEST
    $SAXTester_start_document_counter->test(1, 'start_document called once.');
    # TEST
    $SAXTester_end_document_counter->test(1, 'end_document called once.');

    $parser->parse_string(<<EOT); # start_element*1
<?xml version='1.0' encoding="US-ASCII"?>
<dromedaries one="1" />
EOT
    # TEST
    $SAXTester_start_element_stacker->test(
        [qw(true)],
        'parse_string() : start_element was successful 1 times.',
    );
    # TEST
    $SAXTester_start_document_counter->test(1, 'start_document called once.');
    # TEST
    $SAXTester_end_document_counter->test(1, 'end_document called once.');
}

{
    my $sax = SAXNSTester->new;
    # TEST
    ok($sax, ' TODO : Add test name');

    $parser->set_handler($sax);

    $parser->parse_uri("example/ns.xml");

    # TEST
    $SAXNSTester_start_element_stacker->test(
        [
            qw(true true true)
        ],
        'Three successful SAXNSTester elements.',
    );
    # TEST
    $SAXNSTester_start_prefix_mapping_stacker->test(
        [
            qw(true true true)
        ],
        'Three successful SAXNSTester start_prefix_mapping.',
    );
    # TEST
    $SAXNSTester_end_prefix_mapping_stacker->test(
        [
            qw(true true true)
        ],
        'Three successful SAXNSTester end_prefix_mapping.',
    );
}

########### Namespace test ( empty namespaces ) ########

{
    my $h = "SAXNS2Tester";
    my $xml = "<a xmlns='xml://A'><b/></a>";
    my @tests = (
sub {
    XML::LibXML::SAX        ->new( Handler => $h )->parse_string( $xml );
    # TEST
    $SAXNS2Tester_start_element_stacker->test([qw(true)], 'XML::LibXML::SAX');
},

sub {
    XML::LibXML::SAX::Parser->new( Handler => $h )->parse_string( $xml );
    # TEST
    $SAXNS2Tester_start_element_stacker->test([qw(true)], 'XML::LibXML::SAX::Parser');
},
);  
    
    $_->() for @tests;

    
}


########### Error Handling ###########
{
  my $xml = '<foo><bar/><a>Text</b></foo>';

  my $handler = SAXErrorTester->new;

  foreach my $pkg (qw(XML::LibXML::SAX::Parser XML::LibXML::SAX)) {
    undef $@;
    eval {
      $pkg->new(Handler => $handler)->parse_string($xml);
    };
    # TEST*2
    ok($@, ' TODO : Add test name'); # We got an error
  }
  
  $handler = SAXErrorCallbackTester->new;
  eval { XML::LibXML::SAX->new(Handler => $handler )->parse_string($xml) };
  # TEST
  ok($@, ' TODO : Add test name'); # We got an error
  # TEST
  ok( $handler->{fatal_called}, ' TODO : Add test name' );

}

########### XML::LibXML::SAX::parse_chunk test ###########

{
  my $chunk = '<app>LOGOUT</app><bar/>';
  my $builder = XML::LibXML::SAX::Builder->new( Encoding => 'UTF-8' );
  my $parser = XML::LibXML::SAX->new( Handler => $builder );
  $parser->start_document();
  $builder->start_element({Name=>'foo'});
  $parser->parse_chunk($chunk);
  $parser->parse_chunk($chunk);
  $builder->end_element({Name=>'foo'});
  $parser->end_document();
  # TEST
  is($builder->result()->documentElement->toString(), '<foo>'.$chunk.$chunk.'</foo>', ' TODO : Add test name');
}


######## TEST error exceptions ############## 
{

  package MySAXHandler;
  use strict;
  use warnings;
  use base 'XML::SAX::Base';
  use Carp;
  sub start_element {
    my( $self, $elm) = @_;
    if ( $elm->{LocalName} eq 'TVChannel' ) {
      die bless({ Message => "My exception"},"MySAXException");
    }
  }
}
{
  use strict;
  use warnings;
  my $parser = XML::LibXML::SAX->new( Handler => MySAXHandler->new( )) ;
  eval { $parser->parse_string( <<'EOF' ) };
<TVChannel TVChannelID="71" TVChannelName="ARD">
        <Moin>Moin</Moin>
</TVChannel>
EOF
  # TEST
  is(ref($@), 'MySAXException', ' TODO : Add test name');
  # TEST
  is(ref($@) && $@->{Message}, "My exception", ' TODO : Add test name');
}
########### Helper class #############

package SAXTester;
use Test::More;

sub new {
    my $class = shift;
    return bless {}, $class;
}

sub start_document {

  $SAXTester_start_document_counter->cb()->();

  return;
}

sub end_document {
    $SAXTester_end_document_counter->cb()->();
    return;
}

sub start_element {
    my ($self, $el) = @_;

    $SAXTester_start_element_stacker->cb()->($el);

    # foreach my $attr (keys %{$el->{Attributes}}) {
    #   warn("Attr: $attr = $el->{Attributes}->{$attr}\n");
    # }
    # warn("start_element: $el->{Name}\n");

    return;
}

sub end_element {
  my ($self, $el) = @_;
  # warn("end_element: $el->{Name}\n");
}

sub characters {
  my ($self, $chars) = @_;
  # warn("characters: $chars->{Data}\n");
}

1;

package SAXNSTester;
use Test::More;

sub new {
    bless {}, shift;
}

sub start_element {
    my ($self, $node) = @_;

    $SAXNSTester_start_element_stacker->cb()->($node);

    return;
}

sub end_element {
    my ($self, $node) = @_;
    # warn("end_element: $node->{Name}\n");
}

sub start_prefix_mapping {
    my ($self, $node) = @_;

    $SAXNSTester_start_prefix_mapping_stacker->cb()->($node);

    return;
}

sub end_prefix_mapping {
    my ($self, $node) = @_;

    $SAXNSTester_end_prefix_mapping_stacker->cb()->($node);

    return;
}

1;

package SAXNS2Tester;
use Test::More;

#sub new {
#    my $class = shift;
#    return bless {}, $class;
#}

sub start_element {
    my $self = shift;
    my ( $elt ) = @_;

    $SAXNS2Tester_start_element_stacker->cb()->($elt);

    return;
}

1;

package SAXErrorTester;
use Test::More;

sub new {
    bless {}, shift;
}

sub end_document {
    print "End doc: @_\n";
    return 1; # Shouldn't be reached
}

package SAXErrorCallbackTester;
use Test::More;

sub fatal_error {
    $_[0]->{fatal_called} = 1;
}

sub start_element {
    # test if we can do other stuff 
    XML::LibXML->new->parse_string("<foo/>");
    return;
}
sub new {
    bless {}, shift;
}

sub end_document {
    print "End doc: @_\n";
    return 1; # Shouldn't be reached
}


1;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.