xref: /openbsd/gnu/usr.bin/perl/cpan/JSON-PP/t/001_utf8.t (revision f2a19305)
1# copied over from JSON::XS and modified to use JSON::PP
2
3use strict;
4use warnings;
5use Test::More;
6BEGIN { plan tests => 9 };
7
8BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
9
10use utf8;
11use JSON::PP;
12
13my $pilcrow_utf8 = (ord "^" == 0x5E) ? "\xc2\xb6"  # 8859-1
14                 : (ord "^" == 0x5F) ? "\x80\x65"  # CP 1024
15                 :                     "\x78\x64"; # assume CP 037
16is (JSON::PP->new->allow_nonref (1)->utf8 (1)->encode ("¶"), "\"$pilcrow_utf8\"");
17is (JSON::PP->new->allow_nonref (1)->encode ("¶"), "\"¶\"");
18is (JSON::PP->new->allow_nonref (1)->ascii (1)->utf8 (1)->encode (chr 0x8000), '"\u8000"');
19is (JSON::PP->new->allow_nonref (1)->ascii (1)->utf8 (1)->pretty (1)->encode (chr 0x10402), "\"\\ud801\\udc02\"\n");
20
21eval { JSON::PP->new->allow_nonref (1)->utf8 (1)->decode ('"¶"') };
22ok $@ =~ /malformed UTF-8/;
23
24is (JSON::PP->new->allow_nonref (1)->decode ('"¶"'), "¶");
25is (JSON::PP->new->allow_nonref (1)->decode ('"\u00b6"'), "¶");
26is (JSON::PP->new->allow_nonref (1)->decode ('"\ud801\udc02' . "\x{10204}\""), "\x{10402}\x{10204}");
27
28my $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
31is (JSON::PP->new->allow_nonref (1)->decode ('"\"\n\\\\\r\t\f\b"'), "\"$controls");
32
33