Source

perl-XML-LibXML / t / 03doc.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
# $Id$

##
# this test checks the DOM Document interface of XML::LibXML
# it relies on the success of t/01basic.t and t/02parse.t

# it will ONLY test the DOM capabilities as specified in DOM Level3
# XPath tests should be done in another test file

# since all tests are run on a preparsed 

use strict;
use warnings;

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

use XML::LibXML;
use XML::LibXML::Common qw(:libxml);

use IO::Handle;

sub is_empty_str 
{
    my $s = shift;
    return (!defined($s) or (length($s) == 0));
}

# TEST:$c=0;
sub _check_element_node
{
    local $Test::Builder::Level = $Test::Builder::Level + 1;

    my ($node, $name, $blurb) = @_;

    # TEST:$c++;
    ok($node, "$blurb - node was initialised");
    # TEST:$c++;
    is($node->nodeType, XML_ELEMENT_NODE, "$blurb - node is an element node");
    # TEST:$c++;
    is($node->nodeName, $name, "$blurb - node has the right name.");
}

# TEST:$_check_element_node=$c;

sub _check_created_element
{
    local $Test::Builder::Level = $Test::Builder::Level + 1;
    my ($doc, $given_name, $name, $blurb) = @_;

    return _check_element_node(
        $doc->createElement($given_name),
        $name,
        $blurb
    );
}
# TEST:$_check_created_element=$_check_element_node;

sub _multi_arg_generic_count
{
    local $Test::Builder::Level = $Test::Builder::Level + 1;

    my ($doc, $method, $params) = @_;
    
    my ($meth_params, $want_count, $blurb) = @$params;

    my @elems = $doc->$method( @$meth_params );

    return is (scalar(@elems), $want_count, $blurb);
}

sub _generic_count
{
    local $Test::Builder::Level = $Test::Builder::Level + 1;

    my ($doc, $method, $params) = @_;
    
    my ($name, $want_count, $blurb) = @$params;

    return _multi_arg_generic_count(
        $doc, $method, [[$name], $want_count, $blurb, ],
    );
}

sub _count_local_name
{
    local $Test::Builder::Level = $Test::Builder::Level + 1;
    my $doc = shift;

    return _generic_count($doc, 'getElementsByLocalName', [@_]);
}

sub _count_tag_name
{
    local $Test::Builder::Level = $Test::Builder::Level + 1;
    my $doc = shift;

    return _generic_count($doc, 'getElementsByTagName', [@_]);
}

sub _count_children_by_local_name
{
    local $Test::Builder::Level = $Test::Builder::Level + 1;
    my $doc = shift;

    return _generic_count($doc, 'getChildrenByLocalName', [@_]);
}

sub _count_children_by_name
{
    local $Test::Builder::Level = $Test::Builder::Level + 1;
    my $doc = shift;

    return _generic_count($doc, 'getChildrenByTagName', [@_]);
}

sub _count_elements_by_name_ns
{
    local $Test::Builder::Level = $Test::Builder::Level + 1;
    my ($doc, $ns_and_name, $want_count, $blurb) = @_;

    return _multi_arg_generic_count($doc, 'getElementsByTagNameNS',
        [$ns_and_name, $want_count, $blurb]
    );
}

sub _count_children_by_name_ns
{
    local $Test::Builder::Level = $Test::Builder::Level + 1;
    my ($doc, $ns_and_name, $want_count, $blurb) = @_;

    return _multi_arg_generic_count($doc, 'getChildrenByTagNameNS',
        [$ns_and_name, $want_count, $blurb]
    );
}

{
    # Document Attributes

    my $doc = XML::LibXML::Document->createDocument();
    # TEST
    ok($doc, ' TODO : Add test name');
    # TEST
    ok( ! defined($doc->encoding), ' TODO : Add test name'); 
    # TEST
    is( $doc->version,  "1.0", ' TODO : Add test name' );
    # TEST
    is( $doc->standalone, -1, ' TODO : Add test name' );  # is the value we get for undefined,
                                 # actually the same as 0 but just not set.
    # TEST
    ok( !defined($doc->URI), ' TODO : Add test name');  # should be set by default.
    # TEST
    is( $doc->compression, -1, ' TODO : Add test name' ); # -1 indicates NO compression at all!
                                 # while 0 indicates just no zip compression 
                                 # (big difference huh?)

    $doc->setEncoding( "iso-8859-1" );
    # TEST
    is( $doc->encoding, "iso-8859-1", 'Encoding was set.' );

    $doc->setVersion(12.5);
    # TEST
    is( $doc->version, "12.5", 'Version was set.' );

    $doc->setStandalone(1);
    # TEST
    is( $doc->standalone, 1, 'Standalone was set.' );

    $doc->setBaseURI( "localhost/here.xml" );
    # TEST
    is( $doc->URI, "localhost/here.xml", 'URI is set.' );

    my $doc2 = XML::LibXML::Document->createDocument("1.1", "iso-8859-2");
    # TEST
    is( $doc2->encoding, "iso-8859-2", 'doc2 encoding was set.' );
    # TEST
    is( $doc2->version,  "1.1", 'doc2 version was set.' );
    # TEST
    is( $doc2->standalone,  -1, 'doc2 standalone' );
}

{
    # 2. Creating Elements
    my $doc = XML::LibXML::Document->new();
    {
        my $node = $doc->createDocumentFragment();
        # TEST
        ok($node, ' TODO : Add test name');
        # TEST
        is($node->nodeType, XML_DOCUMENT_FRAG_NODE, ' TODO : Add test name');
    }

    # TEST*$_check_created_element
    _check_created_element($doc, 'foo', 'foo', 'Simple Element');
    
    {
        # document with encoding
        my $encdoc = XML::LibXML::Document->new( "1.0" );
        $encdoc->setEncoding( "iso-8859-1" );

        # TEST*$_check_created_element
        _check_created_element(
            $encdoc, 'foo', 'foo', 'Encdoc Element creation'
        );

        # SAX style document with encoding
        my $node_def = {
            Name => "object",
            LocalName => "object",
            Prefix => "",
            NamespaceURI => "",
                       };

        # TEST*$_check_created_element
        _check_created_element(
            $encdoc, $node_def->{Name}, 'object',
            'Encdoc element creation based on node_def->{name}',
        );
    }

    {
        # namespaced element test
        my $node = $doc->createElementNS( "http://kungfoo", "foo:bar" );
        # TEST
        ok($node, ' TODO : Add test name');
        # TEST
        is($node->nodeType, XML_ELEMENT_NODE, ' TODO : Add test name');
        # TEST
        is($node->nodeName, "foo:bar", ' TODO : Add test name');
        # TEST
        is($node->prefix, "foo", ' TODO : Add test name');
        # TEST
        is($node->localname, "bar", ' TODO : Add test name');
        # TEST
        is($node->namespaceURI, "http://kungfoo", ' TODO : Add test name');
    }

    {
        # bad element creation
        # TEST:$badnames_count=5;
        my @badnames = ( ";", "&", "<><", "/", "1A");

        foreach my $name ( @badnames ) {
            my $node = eval {$doc->createElement( $name );};
            # TEST*$badnames_count
            ok( !(defined $node), ' TODO : Add test name' );
        }

    }

    {
        my $node = $doc->createTextNode( "foo" );
        # TEST
        ok($node, ' TODO : Add test name');
        # TEST
        is($node->nodeType, XML_TEXT_NODE, ' TODO : Add test name' );
        # TEST
        is($node->nodeValue, "foo", ' TODO : Add test name' );
    }

    {
        my $node = $doc->createComment( "foo" );
        # TEST
        ok($node, ' TODO : Add test name');
        # TEST
        is($node->nodeType, XML_COMMENT_NODE, ' TODO : Add test name' );
        # TEST
        is($node->nodeValue, "foo", ' TODO : Add test name' );
        # TEST
        is($node->toString, "<!--foo-->", ' TODO : Add test name');
    }

    {
        my $node = $doc->createCDATASection( "foo" );
        # TEST
        ok($node, ' TODO : Add test name');
        # TEST
        is($node->nodeType, XML_CDATA_SECTION_NODE, ' TODO : Add test name' );
        # TEST
        is($node->nodeValue, "foo", ' TODO : Add test name' );
        # TEST
        is($node->toString, "<![CDATA[foo]]>", ' TODO : Add test name');
    }

    # -> Create Attributes
    {
        my $attr = $doc->createAttribute("foo", "bar");
        # TEST
        ok($attr, ' TODO : Add test name');
        # TEST
        is($attr->nodeType, XML_ATTRIBUTE_NODE, ' TODO : Add test name' );
        # TEST
        is($attr->name, "foo", ' TODO : Add test name');
        # TEST
        is($attr->value, "bar", ' TODO : Add test name' );
        # TEST
        is($attr->hasChildNodes, 0, ' TODO : Add test name');
        my $content = $attr->firstChild;
        # TEST
        ok( $content, ' TODO : Add test name' );
        # TEST
        is( $content->nodeType, XML_TEXT_NODE, ' TODO : Add test name' );
    }
    {
        # bad attribute creation
        # TEST:$badnames_count=5;
        my @badnames = ( ";", "&", "<><", "/", "1A");

        foreach my $name ( @badnames ) {
            my $node = eval {$doc->createAttribute( $name, "bar" );};
            # TEST*$badnames_count
            ok( !defined($node), ' TODO : Add test name' );
        }

    }
    {
      my $elem = $doc->createElement('foo');
      my $attr = $doc->createAttribute(attr => 'e & f');
      $elem->addChild($attr);
      # TEST
      ok ($elem->toString() eq '<foo attr="e &amp; f"/>', ' TODO : Add test name');
      $elem->removeAttribute('attr');
      $attr = $doc->createAttributeNS(undef,'attr2' => 'a & b');
      $elem->addChild($attr);
      # TEST
      ok ($elem->toString() eq '<foo attr2="a &amp; b"/>', ' TODO : Add test name');
    }
    {
        eval {
            my $attr = $doc->createAttributeNS("http://kungfoo", "kung:foo","bar");
        };
        # TEST
        ok($@, ' TODO : Add test name');

        my $root = $doc->createElement( "foo" );
        $doc->setDocumentElement( $root );

        my $attr;
        eval {
           $attr = $doc->createAttributeNS("http://kungfoo", "kung:foo","bar");
        };
        # TEST
        ok($attr, ' TODO : Add test name');
        # TEST
        is($attr->nodeName, "kung:foo", ' TODO : Add test name');
        # TEST
        is($attr->name,"foo", ' TODO : Add test name' );
        # TEST
        is($attr->value, "bar", ' TODO : Add test name' );

        $attr->setValue( q(bar&amp;) );
        # TEST
        is($attr->getValue, q(bar&amp;), ' TODO : Add test name' );
    }
    {
        # bad attribute creation
        # TEST:$badnames_count=5;
        my @badnames = ( ";", "&", "<><", "/", "1A");

        foreach my $name ( @badnames ) {
            my $node = eval {$doc->createAttributeNS( undef, $name, "bar" );};
            # TEST*$badnames_count
            ok( (!defined $node), ' TODO : Add test name' );
        }

    }

    # -> Create PIs
    {
        my $pi = $doc->createProcessingInstruction( "foo", "bar" );
        # TEST
        ok($pi, ' TODO : Add test name');
        # TEST
        is($pi->nodeType, XML_PI_NODE, ' TODO : Add test name');
        # TEST
        is($pi->nodeName, "foo", ' TODO : Add test name');
        # TEST
        is($pi->textContent, "bar", ' TODO : Add test name');
        # TEST
        is($pi->getData, "bar", ' TODO : Add test name');
    }

    {
        my $pi = $doc->createProcessingInstruction( "foo" );
        # TEST
        ok($pi, ' TODO : Add test name');
        # TEST
        is($pi->nodeType, XML_PI_NODE, ' TODO : Add test name');
        # TEST
        is($pi->nodeName, "foo", ' TODO : Add test name');
        my $data = $pi->textContent;
        # undef or "" depending on libxml2 version
        # TEST
        ok( is_empty_str($data), ' TODO : Add test name' );
        $data = $pi->getData;
        # TEST
        ok( is_empty_str($data), ' TODO : Add test name' );
        $pi->setData(q(bar&amp;));
        # TEST
        is( $pi->getData, q(bar&amp;), ' TODO : Add test name');
        # TEST
        is($pi->textContent, q(bar&amp;), ' TODO : Add test name');
    }
}

{
    # Document Manipulation
    # -> Document Elements 

    my $doc = XML::LibXML::Document->new();
    my $node = $doc->createElement( "foo" );
    $doc->setDocumentElement( $node );
    my $tn = $doc->documentElement;
    # TEST
    ok($tn, ' TODO : Add test name');
    # TEST
    ok($node->isSameNode($tn), ' TODO : Add test name');

    my $node2 = $doc->createElement( "bar" );
    { my $warn;
      eval {
        local $SIG{__WARN__} = sub { $warn = 1 };
        # TEST
        ok( !defined($doc->appendChild($node2)), ' TODO : Add test name' );
      };
      # TEST
      ok(($@ or $warn), ' TODO : Add test name');
    }
    my @cn = $doc->childNodes;
    # TEST
    is( scalar(@cn) , 1, ' TODO : Add test name');
    # TEST
    ok($cn[0]->isSameNode($node), ' TODO : Add test name');

    eval {
      $doc->insertBefore($node2, $node);
    };
    # TEST
    ok ($@, ' TODO : Add test name');
    @cn = $doc->childNodes;
    # TEST
    is( scalar(@cn) , 1, ' TODO : Add test name');
    # TEST
    ok($cn[0]->isSameNode($node), ' TODO : Add test name');

    $doc->removeChild($node);
    @cn = $doc->childNodes;
    # TEST
    is( scalar(@cn) , 0, ' TODO : Add test name');

    for ( 1..2 ) {
        my $nodeA = $doc->createElement( "x" );
        $doc->setDocumentElement( $nodeA );
    }
    # TEST
    ok(1, ' TODO : Add test name'); # must not segfault here :)

    $doc->setDocumentElement( $node2 );
    @cn = $doc->childNodes;
    # TEST
    is( scalar(@cn) , 1, ' TODO : Add test name');
    # TEST
    ok($cn[0]->isSameNode($node2), ' TODO : Add test name');

    my $node3 = $doc->createElementNS( "http://foo", "bar" );
    # TEST
    ok($node3, ' TODO : Add test name');

    # -> Processing Instructions
    {
        my $pi = $doc->createProcessingInstruction( "foo", "bar" );
        $doc->appendChild( $pi );
        @cn = $doc->childNodes;
        # TEST
        ok( $pi->isSameNode($cn[-1]), ' TODO : Add test name' );
        $pi->setData( 'bar="foo"' );
        # TEST
        is( $pi->textContent, 'bar="foo"', ' TODO : Add test name');
        $pi->setData( foo=>"foo" );
        # TEST
        is( $pi->textContent, 'foo="foo"', ' TODO : Add test name');
    }
}

{
    # Document Storing
    my $parser = XML::LibXML->new;
    my $doc = $parser->parse_string("<foo>bar</foo>");  

    # TEST

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

    # -> to file handle

    {
        open my $fh, '>', 'example/testrun.xml'
            or die "Cannot open example/testrun.xml for writing - $!.";

        $doc->toFH( $fh );
        $fh->close;
        # TEST
        ok(1, ' TODO : Add test name');
        # now parse the file to check, if succeeded
        my $tdoc = $parser->parse_file( "example/testrun.xml" );
        # TEST
        ok( $tdoc, ' TODO : Add test name' );
        # TEST
        ok( $tdoc->documentElement, ' TODO : Add test name' );
        # TEST
        is( $tdoc->documentElement->nodeName, "foo", ' TODO : Add test name' );
        # TEST
        is( $tdoc->documentElement->textContent, "bar", ' TODO : Add test name' );
        unlink "example/testrun.xml" ;
    }

    # -> to named file
    {
        $doc->toFile( "example/testrun.xml" );
        # TEST
        ok(1, ' TODO : Add test name');
        # now parse the file to check, if succeeded
        my $tdoc = $parser->parse_file( "example/testrun.xml" );
        # TEST
        ok( $tdoc, ' TODO : Add test name' );
        # TEST
        ok( $tdoc->documentElement, ' TODO : Add test name' );
        # TEST
        is( $tdoc->documentElement->nodeName, "foo", ' TODO : Add test name' );
        # TEST
        is( $tdoc->documentElement->textContent, "bar", ' TODO : Add test name' );
        unlink "example/testrun.xml" ;        
    }

    # ELEMENT LIKE FUNCTIONS
    {
        my $parser2 = XML::LibXML->new();
        my $string1 = "<A><A><B/></A><A><B/></A></A>";
        my $string2 = '<C:A xmlns:C="xml://D"><C:A><C:B/></C:A><C:A><C:B/></C:A></C:A>';
        my $string3 = '<A xmlns="xml://D"><A><B/></A><A><B/></A></A>';
        my $string4 = '<C:A><C:A><C:B/></C:A><C:A><C:B/></C:A></C:A>';
        my $string5 = '<A xmlns:C="xml://D"><C:A>foo<A/>bar</C:A><A><C:B/>X</A>baz</A>';
        {
            my $doc2 = $parser2->parse_string($string1);
            # TEST
            _count_tag_name($doc2, 'A', 3, q{3 As});
            # TEST
            _count_tag_name($doc2, '*', 5, q{5 elements of all names});

            # TEST
            _count_elements_by_name_ns($doc2, ['*', 'B'], 2,
                '2 Bs of any namespace'
            );

            # TEST
            _count_local_name($doc2, 'A', 3, q{3 A's});

            # TEST
            _count_local_name($doc2, '*', 5, q{5 Sub-elements});
        }
        {
            my $doc2 = $parser2->parse_string($string2);
            # TEST
            _count_tag_name( $doc2, 'C:A', 3, q{C:A count});
            # TEST
            _count_elements_by_name_ns($doc2, [ "xml://D", "A" ], 3, 
                q{3 elements of namespace xml://D and A},
            );
            # TEST
            _count_elements_by_name_ns($doc2, ['*', 'A'], 3,
                q{3 Elements A of any namespace}
            );
            # TEST
            _count_local_name($doc2, 'A', 3, q{3 As});
        }
        {
            my $doc2 = $parser2->parse_string($string3);
            # TEST
            _count_elements_by_name_ns($doc2, ["xml://D", "A"], 3,
                q{3 Elements A of any namespace}
            );
            # TEST
            _count_local_name($doc2, 'A', 3, q{3 As});
        }
        {
            $parser2->recover(1);
            local $SIG{'__WARN__'} = sub { 
                  print "warning caught: @_\n";
            }; 
            my $doc2 = $parser2->parse_string($string4);
            # TEST
            _count_local_name( $doc2, 'A', 3, q{3 As});
        }
        {
            my $doc2 = $parser2->parse_string($string5);
            # TEST
            _count_tag_name($doc2, 'C:A', 1, q{3 C:As});
            # TEST
            _count_tag_name($doc2, 'A', 3, q{3 As});
            # TEST
            _count_elements_by_name_ns($doc2, ["*", "A"], 4,
                q{4 Elements of A of any namespace}
            );
            # TEST
            _count_elements_by_name_ns($doc2, ['*', '*'], 5,
                q{4 Elements of any namespace},
            );
            # TEST
            _count_elements_by_name_ns( $doc2, ["xml://D", "*" ], 2,
                q{2 elements of any name in D}
            );

            my $A = $doc2->getDocumentElement;
            # TEST
            _count_children_by_name($A, 'A', 1, q{1 A});
            # TEST
            _count_children_by_name($A, 'C:A', 1, q{C:A});
            # TEST
            _count_children_by_name($A, 'C:B', 0, q{No C:B children});
            # TEST
            _count_children_by_name($A, "*", 2, q{2 Childern in $A in total});
            # TEST
            _count_children_by_name_ns($A, ['*', 'A'], 2, 
                q{2 As of any namespace});
            # TEST
            _count_children_by_name_ns($A, [ "xml://D", "*" ], 1,
                q{1 Child of D},
            );
            # TEST
            _count_children_by_name_ns($A, [ "*", "*" ], 2,
                q{2 Children in total},
            );
            # TEST
            _count_children_by_local_name($A, 'A', 2, q{2 As});
        }
    }
}

{
    # Bug fixes (to be used with valgrind)
    {  
       my $doc=XML::LibXML->createDocument(); # create a doc
       my $x=$doc->createPI(foo=>"bar");      # create a PI
       undef $doc;                            # should not free
       undef $x;                              # free the PI
       # TEST
       ok(1, ' TODO : Add test name');
    }
    {  
       my $doc=XML::LibXML->createDocument(); # create a doc
       my $x=$doc->createAttribute(foo=>"bar"); # create an attribute
       undef $doc;                            # should not free
       undef $x;                              # free the attribute
       # TEST
       ok(1, ' TODO : Add test name');
    }
    {  
       my $doc=XML::LibXML->createDocument(); # create a doc
       my $x=$doc->createAttributeNS(undef,foo=>"bar"); # create an attribute
       undef $doc;                            # should not free
       undef $x;                              # free the attribute
       # TEST
       ok(1, ' TODO : Add test name');
    }
    {  
       my $doc=XML::LibXML->new->parse_string('<foo xmlns:x="http://foo.bar"/>');
       my $x=$doc->createAttributeNS('http://foo.bar','x:foo'=>"bar"); # create an attribute
       undef $doc;                            # should not free
       undef $x;                              # free the attribute
       # TEST
       ok(1, ' TODO : Add test name');
    }
    {
      # rt.cpan.org #30610
      # valgrind this
      my $object=XML::LibXML::Element->new( 'object' );
      my $xml = qq(<?xml version="1.0" encoding="UTF-8"?>\n<lom/>);
      my $lom_doc=XML::LibXML->new->parse_string($xml);
      my $lom_root=$lom_doc->getDocumentElement();
      $object->appendChild( $lom_root );
      # TEST
      ok(!defined($object->firstChild->ownerDocument), ' TODO : Add test name');
    }   
}


{
  my $xml = q{<?xml version="1.0" encoding="UTF-8"?>
<test/>
};
  my $out = q{<?xml version="1.0"?>
<test/>
};
  my $dom = XML::LibXML->new->parse_string($xml);
  # TEST
  is($dom->getEncoding, "UTF-8", ' TODO : Add test name');
  $dom->setEncoding();
  # TEST
  is($dom->getEncoding, undef, ' TODO : Add test name');
  # TEST
  is($dom->toString, $out, ' TODO : Add test name');
}

# the following tests were added for #33810
SKIP:
{
    if (! eval { require Encode; })
    {
        skip "Encoding related tests require Encode", (3*8);
    }
    # TEST:$num_encs=3;
    # The count.
    # TEST:$c=0;
    for my $enc (qw(UTF-16 UTF-16LE UTF-16BE)) {
        my $xml = Encode::encode($enc,qq{<?xml version="1.0" encoding="$enc"?>
            <test foo="bar"/>
            });
        my $dom = XML::LibXML->new->parse_string($xml);
        # TEST:$c++;
        is($dom->getEncoding,$enc, ' TODO : Add test name');
        # TEST:$c++;
        is($dom->actualEncoding,$enc, ' TODO : Add test name');
        # TEST:$c++;
        is($dom->getDocumentElement->getAttribute('foo'),'bar', ' TODO : Add test name');
        # TEST:$c++;
        is($dom->getDocumentElement->getAttribute(Encode::encode('UTF-16','foo')), 'bar', ' TODO : Add test name');
        # TEST:$c++;
        is($dom->getDocumentElement->getAttribute(Encode::encode($enc,'foo')), 'bar', ' TODO : Add test name');
        my $exp_enc = $enc eq 'UTF-16' ? 'UTF-16LE' : $enc;
        # TEST:$c++;
        is($dom->getDocumentElement->getAttribute('foo',1), Encode::encode($exp_enc,'bar'), ' TODO : Add test name');
        # TEST:$c++;
        is($dom->getDocumentElement->getAttribute(Encode::encode('UTF-16','foo'),1), Encode::encode($exp_enc,'bar'), ' TODO : Add test name');
        # TEST:$c++;
        is($dom->getDocumentElement->getAttribute(Encode::encode($enc,'foo'),1), Encode::encode($exp_enc,'bar'), ' TODO : Add test name');
    }
    # TEST*$num_encs*$c
}
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.