File: test_safe_encode.t

package info (click to toggle)
libxml-twig-perl 1%3A3.34-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,036 kB
  • ctags: 806
  • sloc: perl: 18,640; xml: 202; makefile: 18
file content (46 lines) | stat: -rwxr-xr-x 2,023 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl -w
use strict; 

# $Id: /xmltwig/trunk/t/test_safe_encode.t 31 2007-11-13T19:55:41.920709Z mrodrigu  $

use File::Spec;
use lib File::Spec->catdir(File::Spec->curdir,"t");
use tools;

use XML::Twig;

my $DEBUG=0;

print "1..8\n";

if( $] >= 5.006) { eval "use utf8;"; } 

# suitable for perl 5.6.*
my $doc=q{<doc><élément att="été">été</élément></doc>};
(my $safe_xml_doc= $doc)=~ s{é}{&#233;}g;
(my $safe_hex_doc= $doc)=~ s{é}{&#xe9;}g;
(my $text_safe_xml_doc= $doc)=~ s{été}{&#233;t&233;}g;
(my $text_safe_hex_doc= $doc)=~ s{é}{&#xe9;t&xe9;}g;

is( XML::Twig->new( output_filter => 'safe')->parse( $doc)->sprint, $safe_xml_doc, "output_filter => 'safe'");
is( XML::Twig->new( output_filter => 'safe_hex')->parse( $doc)->sprint, $safe_hex_doc, "output_filter => 'safe_hex'");
is( XML::Twig->new( output_text_filter => 'safe')->parse( $doc)->sprint, $safe_xml_doc, "output_text_filter => 'safe'");
is( XML::Twig->new( output_text_filter => 'safe_hex')->parse( $doc)->sprint, $safe_hex_doc, "output_text_filter => 'safe_hex'");

# suitable for 5.8.* and above (you can't have utf-8 hash keys before that)

if( $] < 5.008)
  { skip( 4 => "cannot process utf-8 attribute names with a perl before 5.8"); }
else
  {
    my $doc='<doc><élément atté="été">été</élément></doc>';
    (my $safe_xml_doc= $doc)=~ s{é}{&#233;}g;
    (my $safe_hex_doc= $doc)=~ s{é}{&#xe9;}g;
    (my $text_safe_xml_doc= $doc)=~ s{été}{&#233;t&233;}g;
    (my $text_safe_hex_doc= $doc)=~ s{é}{&#xe9;t&xe9;}g;

    is( XML::Twig->new( output_filter => 'safe')->parse( $doc)->sprint, $safe_xml_doc, "output_filter => 'safe'");
    is( XML::Twig->new( output_filter => 'safe_hex')->parse( $doc)->sprint, $safe_hex_doc, "output_filter => 'safe_hex'");
    is( XML::Twig->new( output_text_filter => 'safe')->parse( $doc)->sprint, $safe_xml_doc, "output_text_filter => 'safe'");
    is( XML::Twig->new( output_text_filter => 'safe_hex')->parse( $doc)->sprint, $safe_hex_doc, "output_text_filter => 'safe_hex'");
  }