File: 34_utf8_flag.t

package info (click to toggle)
libxml-treepp-perl 0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 680 kB
  • sloc: perl: 810; xml: 58; sh: 41; makefile: 2
file content (177 lines) | stat: -rwxr-xr-x 5,579 bytes parent folder | download | duplicates (4)
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
# ----------------------------------------------------------------
#   this test script is written in utf8 but does not "use utf8" for 5.005-compatibility
# ----------------------------------------------------------------
    use strict;
    use Test::More;
# ----------------------------------------------------------------
{
    local $@;
    eval { require 5.008001; };
    plan skip_all => 'Perl 5.8.1 is required.' if $@;
}
# ----------------------------------------------------------------
{
    plan tests => 66;
    use_ok('XML::TreePP');
    &test_utf8();
}
# ----------------------------------------------------------------
sub test_utf8 {

    my $octxml = <<"EOT";
<root>
    <one>一</one>
    <two>二2</two>
    <three>三3参</three>
    <four>四4Ⅳⅳ</four>
    <five>5</five>
    <six>±6÷6</six>
</root>
EOT

    my $strxml = $octxml;
    utf8::decode( $strxml );

    my $strtpp = XML::TreePP->new( utf8_flag => 1 );
    my $octtpp = XML::TreePP->new();

    ok( ! utf8::is_utf8($octxml), '[source] XML: octets' );
    ok(   utf8::is_utf8($strxml), '[source] XML: string' );

    my $treeA = $strtpp->parse( $octxml );
    my $treeB = $strtpp->parse( $strxml );
    my $treeC = $octtpp->parse( $octxml );
    my $treeD = $octtpp->parse( $strxml );

    ok( ! utf8::is_utf8($octxml), "[source] XML: octets (no damaged)" );
    ok(   utf8::is_utf8($strxml), "[source] XML: string (no damaged)" );

    &check_string( 'A', $treeA );
    &check_string( 'B', $treeB );
    &check_octest( 'C', $treeC );
    &check_string( 'D', $treeD );

    &check_same( 'A-B', $treeA, $treeB );
    &check_same( 'B-D', $treeB, $treeB );
    &check_diff( 'A-C', $treeA, $treeC );

    foreach my $hash ( $treeA, $treeB, $treeD ) {
        my $root = $hash->{root};
        foreach my $key ( sort keys %$root ) {
            ok( utf8::is_utf8($root->{$key}), 'XML: string '.$key );
        }
    }

    foreach my $hash ( $treeC ) {
        my $root = $hash->{root};
        foreach my $key ( sort keys %$root ) {
            ok( ! utf8::is_utf8($root->{$key}), 'XML: octets '.$key );
        }
    }

    my $xmlH = $octtpp->write( $treeC );
    my $xmlE = $strtpp->write( $treeA );
    my $xmlF = $strtpp->write( $treeB );
    my $xmlG = $octtpp->write( $treeD );

    ok(   utf8::is_utf8($xmlE), '[E] XML: string' );
    ok(   utf8::is_utf8($xmlF), '[F] XML: string' );
    ok(   utf8::is_utf8($xmlG), '[G] XML: string' );
    ok( ! utf8::is_utf8($xmlH), '[H] XML: octets' );
}
# ----------------------------------------------------------------
sub check_string {
    my $name = shift;
    my $tree = shift;

    my $oct1 = '一';
    my $oct2 = "二2";
    my $str2 = $oct2;
    utf8::decode( $str2 );

    my $four = $tree->{root}->{four};
    ok( utf8::is_utf8($four), "[$name] 4: string" );

    my $five = $tree->{root}->{five};
    ok( utf8::is_utf8($five), "[$name] 5: string" );

    my $six = $tree->{root}->{six};
    ok( utf8::is_utf8($six), "[$name] 6: string" );

    my $one = "".$tree->{root}->{one};
    isnt( $one, $oct1, "[$name] 1: string != octets" );
    utf8::encode( $one );
    is( $one, $oct1, "[$name] 2: octets == octets" );

    my $two = "".$tree->{root}->{two};
    isnt( $two, $oct2, "[$name] 3: string != octets" );
    is( $two, $str2, "[$name] 4: string == string" );
}
# ----------------------------------------------------------------
sub check_octest {
    my $name = shift;
    my $tree = shift;

    my $oct1 = '一';
    my $oct2 = "二2";
    my $str2 = $oct2;
    utf8::decode( $str2 );

    my $four = $tree->{root}->{four};
    ok( ! utf8::is_utf8($four), "[$name] 4: octets" );

    my $five = $tree->{root}->{five};
    ok( ! utf8::is_utf8($five), "[$name] 5: octets" );

    my $six = $tree->{root}->{six};
    ok( ! utf8::is_utf8($six), "[$name] 6: octets" );

    my $one = $tree->{root}->{one};
    is( $one, $oct1, "[$name] 1: octets == octets" );

    my $two = "".$tree->{root}->{two};
    isnt( $two, $str2, "[$name] 2: octets != string" );
    utf8::decode( $two );
    is( $two, $str2, "[$name] 2: string == string" );
}
# ----------------------------------------------------------------
sub check_same {
    my $name = shift;
    my $tree1 = shift;
    my $tree2 = shift;

    my $three1 = $tree1->{root}->{three};
    my $three2 = $tree2->{root}->{three};
    is( $three1, $three2, "[$name] 4: same" );

#   octets' latin-1 and string's latin-1 are equal
#   my $five1 = $tree1->{root}->{five};
#   my $five2 = $tree2->{root}->{five};
#   is( $five1, $five2, "[$name] 5: same" );

    my $six1 = $tree1->{root}->{six};
    my $six2 = $tree2->{root}->{six};
    is( $six1, $six2, "[$name] 6: same" );
}
# ----------------------------------------------------------------
sub check_diff {
    my $name = shift;
    my $tree1 = shift;
    my $tree2 = shift;

    my $three1 = $tree1->{root}->{three};
    my $three2 = $tree2->{root}->{three};
    isnt( $three1, $three2, "[$name] 4: diff" );

#   octets' latin-1 and string's latin-1 are equal
#   my $five1 = $tree1->{root}->{five};
#   my $five2 = $tree2->{root}->{five};
#   isnt( $five1, $five2, "[$name] 5: diff" );

    my $six1 = $tree1->{root}->{six};
    my $six2 = $tree2->{root}->{six};
    isnt( $six1, $six2, "[$name] 6: diff" );
}
# ----------------------------------------------------------------
;1;
# ----------------------------------------------------------------