summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/perl/cpan/Encode/t/utf32warnings.t
blob: 5759541922fbfa9faa2a38d866b62a73a8b882e1 (plain)
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
BEGIN {
    if ( $] < 5.009 ) {
        print "1..0 # Skip: Perl <= 5.9 or later required\n";
        exit 0;
    }
}
use strict;
use warnings;

my $script = quotemeta $0;

use Encode;
use Test::More tests => 38;

my $valid   = "\x61\x00\x00\x00";
my $invalid = "\x78\x56\x34\x12";

our $warn;
$SIG{__WARN__} = sub { $warn = $_[0] };

my $enc = find_encoding("UTF32-LE");

{
    local $warn;
    my $ret = $enc->encode( "a", Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    is($warn, undef, "Calling encode on UTF32-LE encode object with valid string produces no warnings");
    is($ret, $valid, "Calling encode on UTF32-LE encode object with valid string returns correct output");
}


{
    local $warn;
    $enc->encode( "\x{D800}", Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    like($warn, qr/UTF-16 surrogate.* at $script line /, "Calling encode on UTF32-LE encode object with invalid string warns");
}

{
    local $warn;
    no warnings 'utf8';
    $enc->encode( "\x{D800}", Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    is($warn, undef, "Warning from encode method of UTF32-LE encode object can be silenced via no warnings 'utf8'");
}

{
    local $warn;
    no warnings;
    $enc->encode( "\x{D800}", Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    is($warn, undef, "Warning from encode method of UTF32-LE encode object can be silenced via no warnings");
}

{
    local $warn;
    no warnings 'utf8';
    $enc->encode( "\x{D800}", Encode::WARN_ON_ERR | Encode::LEAVE_SRC );
    like($warn, qr/UTF-16 surrogate.* at $script line /, "Warning from encode method of UTF32-LE encode object cannot be silenced via no warnings 'utf8' when ONLY_PRAGMA_WARNINGS is not used");
}

{
    local $warn;
    no warnings;
    $enc->encode( "\x{D800}", Encode::WARN_ON_ERR | Encode::LEAVE_SRC );
    like($warn, qr/UTF-16 surrogate.* at $script line /, "Warning from encode method of UTF32-LE encode object cannot be silenced via no warnings when ONLY_PRAGMA_WARNINGS is not used");
}


{
    local $warn;
    my $ret = Encode::encode( $enc, "a", Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    is($warn, undef, "Calling Encode::encode for UTF32-LE with valid string produces no warnings");
    is($ret, $valid, "Calling Encode::encode for UTF32-LE with valid string returns correct output");
}


{
    local $warn;
    Encode::encode( $enc, "\x{D800}", Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    like($warn, qr/UTF-16 surrogate.* at $script line /, "Calling Encode::encode for UTF32-LE with invalid string warns");
}


{
    local $warn;
    no warnings 'utf8';
    Encode::encode( $enc, "\x{D800}", Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    is($warn, undef, "Warning from Encode::encode for UTF32-LE can be silenced via no warnings 'utf8'");
}

{
    local $warn;
    no warnings;
    Encode::encode( $enc, "\x{D800}", Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    is($warn, undef, "Warning from Encode::encode for UTF32-LE can be silenced via no warnings");
}

{
    local $warn;
    no warnings 'utf8';
    Encode::encode( $enc, "\x{D800}", Encode::WARN_ON_ERR | Encode::LEAVE_SRC );
    like($warn, qr/UTF-16 surrogate.* at $script line /, "Warning from Encode::encode for UTF32-LE cannot be silenced via no warnings 'utf8' when ONLY_PRAGMA_WARNINGS is not used");
}

{
    local $warn;
    no warnings;
    Encode::encode( $enc, "\x{D800}", Encode::WARN_ON_ERR | Encode::LEAVE_SRC );
    like($warn, qr/UTF-16 surrogate.* at $script line /, "Warning from Encode::encode for UTF32-LE cannot be silenced via no warnings when ONLY_PRAGMA_WARNINGS is not used");
}


{
    local $warn;
    my $ret = $enc->decode( $valid, Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    is($warn, undef, "Calling decode on UTF32-LE encode object with valid string produces no warnings");
    is($ret, "a", "Calling decode on UTF32-LE encode object with valid string returns correct output");
}


{
    local $warn;
    $enc->decode( $invalid, Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    like($warn, qr/may not be portable.* at $script line /, "Calling decode on UTF32-LE encode object with invalid string warns");
}

{
    local $warn;
    no warnings 'utf8';
    $enc->decode( $invalid, Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    is($warn, undef, "Warning from decode method of UTF32-LE encode object can be silenced via no warnings 'utf8'");
}

{
    local $warn;
    no warnings;
    $enc->decode( $invalid, Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    is($warn, undef, "Warning from decode method of UTF32-LE encode object can be silenced via no warnings");
}

{
    local $warn;
    no warnings 'utf8';
    $enc->decode( $invalid, Encode::WARN_ON_ERR | Encode::LEAVE_SRC );
    like($warn, qr/may not be portable.* at $script line /, "Warning from decode method of UTF32-LE encode object cannot be silenced via no warnings 'utf8' when ONLY_PRAGMA_WARNINGS is not used");
}

{
    local $warn;
    no warnings;
    $enc->decode( $invalid, Encode::WARN_ON_ERR | Encode::LEAVE_SRC );
    like($warn, qr/may not be portable.* at $script line /, "Warning from decode method of UTF32-LE encode object cannot be silenced via no warnings when ONLY_PRAGMA_WARNINGS is not used");
}


{
    local $warn;
    my $ret = Encode::decode( $enc, $valid, Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    is($warn, undef, "Calling Encode::decode for UTF32-LE with valid string produces no warnings");
    is($ret, "a", "Calling Encode::decode for UTF32-LE with valid string returns correct output");
}


{
    local $warn;
    Encode::decode( $enc, $invalid, Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    like($warn, qr/may not be portable.* at $script line /, "Calling Encode::decode for UTF32-LE with invalid string warns");
}

{
    local $warn;
    no warnings 'utf8';
    Encode::decode( $enc, $invalid, Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    is($warn, undef, "Warning from Encode::decode for UTF32-LE can be silenced via no warnings 'utf8'");
}

{
    local $warn;
    no warnings;
    Encode::decode( $enc, $invalid, Encode::WARN_ON_ERR | Encode::ONLY_PRAGMA_WARNINGS | Encode::LEAVE_SRC );
    is($warn, undef, "Warning from Encode::decode for UTF32-LE can be silenced via no warnings");
}

{
    local $warn;
    no warnings 'utf8';
    Encode::decode( $enc, $invalid, Encode::WARN_ON_ERR | Encode::LEAVE_SRC );
    like($warn, qr/may not be portable.* at $script line /, "Warning from Encode::decode for UTF32-LE cannot be silenced via no warnings 'utf8' when ONLY_PRAGMA_WARNINGS is not used");
}

{
    local $warn;
    no warnings;
    Encode::decode( $enc, $invalid, Encode::WARN_ON_ERR | Encode::LEAVE_SRC );
    like($warn, qr/may not be portable.* at $script line /, "Warning from Encode::decode for UTF32-LE cannot be silenced via no warnings when ONLY_PRAGMA_WARNINGS is not used");
}


use PerlIO::encoding;
$PerlIO::encoding::fallback |= Encode::ONLY_PRAGMA_WARNINGS;

{
    local $warn;
    my $tmp = $valid;
    $tmp .= ''; # de-COW
    open my $fh, '<:encoding(UTF32-LE)', \$tmp or die;
    my $str = <$fh>;
    close $fh;
    is($warn, undef, "Calling PerlIO :encoding on valid string produces no warnings");
    is($str, "a", "PerlIO decodes string correctly");
}


{
    local $warn;
    my $tmp = $invalid;
    use Devel::Peek;
    $tmp .= ''; # de-COW
    open my $fh, '<:encoding(UTF32-LE)', \$tmp or die;
    my $str = <$fh>;
    close $fh;
    like($warn, qr/may not be portable.* at $script line /, "Calling PerlIO :encoding on invalid string warns");
}

{
    local $warn;
    my $tmp = $invalid;
    $tmp .= ''; # de-COW
    no warnings 'utf8';
    open my $fh, '<:encoding(UTF32-LE)', \$tmp or die;
    my $str = <$fh>;
    close $fh;
    is($warn, undef, "Warning from PerlIO :encoding can be silenced via no warnings 'utf8'");
}

{
    local $warn;
    my $tmp = $invalid;
    $tmp .= ''; # de-COW
    no warnings;
    open my $fh, '<:encoding(UTF32-LE)', \$tmp or die;
    my $str = <$fh>;
    close $fh;
    is($warn, undef, "Warning from PerlIO :encoding can be silenced via no warnings");
}


{
    local $warn;
    my $str;
    open my $fh, '>:encoding(UTF32-LE)', \$str or die;
    print $fh "a";
    close $fh;
    is($warn, undef, "Calling PerlIO :encoding on valid string produces no warnings");
    is($str, $valid, "PerlIO encodes string correctly");
}


{
    local $warn;
    my $str;
    open my $fh, '>:encoding(UTF32-LE)', \$str or die;
    print $fh "\x{D800}";
    close $fh;
    like($warn, qr/UTF-16 surrogate.* at $script line /, "Calling PerlIO :encoding on invalid string warns");
}

{
    local $warn;
    my $str;
    no warnings 'utf8';
    open my $fh, '>:encoding(UTF32-LE)', \$str or die;
    print $fh "\x{D800}";
    close $fh;
    is($warn, undef, "Warning from PerlIO :encoding can be silenced via no warnings 'utf8'");
}

{
    local $warn;
    my $str;
    no warnings;
    open my $fh, '>:encoding(UTF32-LE)', \$str or die;
    print $fh "\x{D800}";
    close $fh;
    is($warn, undef, "Warning from PerlIO :encoding can be silenced via no warnings");
}