xref: /openbsd/gnu/usr.bin/perl/cpan/Encode/t/decode.t (revision f3efcd01)
1#
2# $Id: decode.t,v 1.5 2019/01/31 04:26:40 dankogai Exp $
3#
4use strict;
5use Encode qw(decode_utf8 FB_CROAK find_encoding decode);
6use Test::More tests => 17;
7use Test::Builder;
8
9sub croak_ok(&) {
10    local $Test::Builder::Level = $Test::Builder::Level + 1;
11    my $code = shift;
12    eval { $code->() };
13    like $@, qr/does not map/;
14}
15
16my $bytes = "L\x{e9}on";
17my $pad = "\x{30C9}";
18
19my $orig = $bytes;
20croak_ok { Encode::decode_utf8($orig, FB_CROAK) };
21
22my $orig2 = $bytes;
23croak_ok { Encode::decode('utf-8', $orig2, FB_CROAK) };
24
25chop(my $new = $bytes . $pad);
26croak_ok { Encode::decode_utf8($new, FB_CROAK) };
27
28my $latin1 = find_encoding('latin1');
29$orig = "\N{U+0080}";
30$orig =~ /(.)/;
31is($latin1->decode($1), $orig, '[cpan #115168] passing magic regex globals to decode');
32SKIP: {
33    skip "Perl Version ($]) is older than v5.16", 1 if $] < 5.016;
34    *a = $orig;
35    is($latin1->decode(*a), '*main::'.$orig, '[cpan #115168] passing typeglobs to decode');
36}
37
38$orig = "\x80";
39$orig =~ /(.)/;
40is($latin1->decode($1), "\N{U+0080}", 'passing magic regex to latin1 decode');
41
42$orig = "\x80";
43*a = $orig;
44is($latin1->decode(*a), "*main::\N{U+0080}", 'passing typeglob to latin1 decode');
45
46$orig = "\N{U+0080}";
47$orig =~ /(.)/;
48is($latin1->encode($1), "\x80", 'passing magic regex to latin1 encode');
49
50$orig = "\xC3\x80";
51$orig =~ /(..)/;
52is(Encode::decode_utf8($1), "\N{U+C0}", 'passing magic regex to Encode::decode_utf8');
53
54SKIP: {
55    skip "Perl Version ($]) is older than v5.27.1", 1 if $] < 5.027001;
56    $orig = "\xC3\x80";
57    *a = $orig;
58    is(Encode::decode_utf8(*a), "*main::\N{U+C0}", 'passing typeglob to Encode::decode_utf8');
59}
60
61$orig = "\N{U+C0}";
62$orig =~ /(.)/;
63is(Encode::encode_utf8($1), "\xC3\x80", 'passing magic regex to Encode::encode_utf8');
64
65$orig = "\xC3\x80";
66$orig =~ /(..)/;
67is(Encode::decode('utf-8', $1), "\N{U+C0}", 'passing magic regex to UTF-8 decode');
68
69$orig = "\xC3\x80";
70*a = $orig;
71is(Encode::decode('utf-8', *a), "*main::\N{U+C0}", 'passing typeglob to UTF-8 decode');
72
73$orig = "\N{U+C0}";
74$orig =~ /(.)/;
75is(Encode::encode('utf-8', $1), "\xC3\x80", 'passing magic regex to UTF-8 encode');
76
77SKIP: {
78    skip "Perl Version ($]) is older than v5.16", 3 if $] < 5.016;
79
80    $orig = "\N{U+0080}";
81    *a = $orig;
82    is($latin1->encode(*a), "*main::\x80", 'passing typeglob to latin1 encode');
83
84    $orig = "\N{U+C0}";
85    *a = $orig;
86    is(Encode::encode_utf8(*a), "*main::\xC3\x80", 'passing typeglob to Encode::encode_utf8');
87
88    $orig = "\N{U+C0}";
89    *a = $orig;
90    is(Encode::encode('utf-8', *a), "*main::\xC3\x80", 'passing typeglob to UTF-8 encode');
91}
92