PageRenderTime 53ms CodeModel.GetById 12ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 0ms

/XML-TreePP/t/46_xml_deref.t

http://xml-treepp.googlecode.com/
Unknown | 122 lines | 113 code | 9 blank | 0 comment | 0 complexity | b6e94aae935e3ed3a2c5f10a77082c29 MD5 | raw file
  1# ----------------------------------------------------------------
  2#   this test script is written in utf8 but does not "use utf8" for 5.005-compatibility
  3# ----------------------------------------------------------------
  4    use strict;
  5    use Test::More;
  6# ----------------------------------------------------------------
  7{
  8    plan tests => 33;
  9    use_ok('XML::TreePP');
 10    &test_main();
 11}
 12# ----------------------------------------------------------------
 13sub test_main {
 14
 15    my $src = {};
 16
 17    $src->{Plain} = <<"EOT";
 18<root>
 19    <a>AA</a>
 20    <z>zz</z>
 21    <c>©©</c>
 22    <e>λλ</e>
 23    <n>??</n>
 24    <k>??</k>
 25</root>
 26EOT
 27
 28    $src->{XMLref} = <<"EOT";
 29<root>
 30    <a>&#x41;&#65;</a>
 31    <z>&#x7A;&#122;</z>
 32    <c>&#xA9;&#169;</c>
 33    <e>&#xEB;&#235;</e>
 34    <n>&#x3093;&#12435;</n>
 35    <k>&#x6F22;&#28450;</k>
 36</root>
 37EOT
 38
 39    $src->{Mixed} = <<"EOT";
 40<root>
 41    <a>A&#65;</a>
 42    <z>z&#122;</z>
 43    <c>©&#169;</c>
 44    <e>λ&#235;</e>
 45    <n>?&#12435;</n>
 46    <k>?&#28450;</k>
 47</root>
 48EOT
 49
 50    foreach my $key ( keys %$src ) {
 51        phase2( "$key octets", $src->{$key} );
 52    }
 53}
 54# ----------------------------------------------------------------
 55sub phase2 {
 56    my $subject = shift;
 57    my $srcxml  = shift;
 58    my $srcref  = ( $srcxml =~ /&#\w+;/ );
 59
 60    foreach my $xml_deref ( 0, 1 ) {
 61        my $subj3 = $subject .( $xml_deref ? ' xml_deref' : '' );
 62        my $opt = {
 63            xml_deref => $xml_deref,
 64        };
 65        my $tpp = XML::TreePP->new( %$opt );
 66        my $tree = $tpp->parse( $srcxml );
 67        if ( $xml_deref ) {
 68            check_octets( $subj3, $tree );
 69        } else {
 70            if ( $srcref ) {
 71                check_has_ref( $subj3, $tree );
 72            } else {
 73                check_no_ref( $subj3, $tree );
 74            }
 75        }
 76    }
 77}
 78# ----------------------------------------------------------------
 79sub check_has_ref {
 80    my $subject = shift;
 81    my $tree    = shift;
 82    my $root = $tree->{root};
 83
 84    my $HAS_REF = qr/&#\w+;/;
 85#   \x00-\x7F is always dereferenced.
 86#   like( $root->{a}, $HAS_REF, "$subject has_ref: a" );
 87#   like( $root->{z}, $HAS_REF, "$subject has_ref: z" );
 88    like( $root->{c}, $HAS_REF, "$subject has_ref: c" );
 89    like( $root->{e}, $HAS_REF, "$subject has_ref: e" );
 90    like( $root->{n}, $HAS_REF, "$subject has_ref: n" );
 91    like( $root->{k}, $HAS_REF, "$subject has_ref: k" );
 92}
 93# ----------------------------------------------------------------
 94sub check_no_ref {
 95    my $subject = shift;
 96    my $tree    = shift;
 97    my $root = $tree->{root};
 98
 99    my $HAS_REF = qr/&#\w+;/;
100    unlike( $root->{a}, $HAS_REF, "$subject no_ref: a" );
101    unlike( $root->{z}, $HAS_REF, "$subject no_ref: z" );
102    unlike( $root->{c}, $HAS_REF, "$subject no_ref: c" );
103    unlike( $root->{e}, $HAS_REF, "$subject no_ref: e" );
104    unlike( $root->{n}, $HAS_REF, "$subject no_ref: n" );
105    unlike( $root->{k}, $HAS_REF, "$subject no_ref: k" );
106}
107# ----------------------------------------------------------------
108sub check_octets {
109    my $subject = shift;
110    my $tree    = shift;
111    my $root = $tree->{root};
112
113    is( $root->{a}, 'AA', "$subject ok: a" );
114    is( $root->{z}, 'zz', "$subject ok: z" );
115    is( $root->{c}, '©©', "$subject ok: c" );
116    is( $root->{e}, 'λλ', "$subject ok: e" );
117    is( $root->{n}, '??', "$subject ok: n" );
118    is( $root->{k}, '??', "$subject ok: k" );
119}
120# ----------------------------------------------------------------
121;1;
122# ----------------------------------------------------------------