Debian Codegraph @

t/01_utf8.t

raw
1 # copied over from JSON::XS and modified to use JSON
2
3 use strict;
4 use warnings;
5 use Test::More;
6 BEGIN { plan tests => 9 };
7
8 BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; }
9
10 use utf8;
11 use JSON;
12
13 my $pilcrow_utf8 = (ord "^" == 0x5E) ? "\xc2\xb6" # 8859-1
14 : (ord "^" == 0x5F) ? "\x80\x65" # CP 1024
15 : "\x78\x64"; # assume CP 037
16 is (JSON->new->allow_nonref (1)->utf8 (1)->encode ("¶"), "\"$pilcrow_utf8\"");
17 is (JSON->new->allow_nonref (1)->encode ("¶"), "\"¶\"");
18 is (JSON->new->allow_nonref (1)->ascii (1)->utf8 (1)->encode (chr 0x8000), '"\u8000"');
19 is (JSON->new->allow_nonref (1)->ascii (1)->utf8 (1)->pretty (1)->encode (chr 0x10402), "\"\\ud801\\udc02\"\n");
20
21 eval { JSON->new->allow_nonref (1)->utf8 (1)->decode ('"¶"') };
22 ok $@ =~ /malformed UTF-8/;
23
24 is (JSON->new->allow_nonref (1)->decode ('"¶"'), "¶");
25 is (JSON->new->allow_nonref (1)->decode ('"\u00b6"'), "¶");
26 is (JSON->new->allow_nonref (1)->decode ('"\ud801\udc02' . "\x{10204}\""), "\x{10402}\x{10204}");
27
28 my $controls = (ord "^" == 0x5E) ? "\012\\\015\011\014\010"
29 : (ord "^" == 0x5F) ? "\025\\\015\005\014\026" # CP 1024
30 : "\045\\\015\005\014\026"; # assume CP 037
31 is (JSON->new->allow_nonref (1)->decode ('"\"\n\\\\\r\t\f\b"'), "\"$controls");
32