1- use Test::More tests => 155 ;
1+ use Test::More tests => 162 ;
22use utf8;
33use Cpanel::JSON::XS;
4+ use warnings;
45
5- is(Cpanel::JSON::XS-> new-> allow_nonref (1) -> utf8 (1) -> encode (" ü" ), " \"\xc3\xbc\" " );
6- is(Cpanel::JSON::XS-> new-> allow_nonref (1) -> encode (" ü" ), " \" ü\" " );
6+ is(Cpanel::JSON::XS-> new-> allow_nonref-> utf8-> encode(" ü" ), " \"\xc3\xbc\" " );
7+ is(Cpanel::JSON::XS-> new-> allow_nonref-> encode(" ü" ), " \" ü\" " );
78
8- is(Cpanel::JSON::XS-> new-> allow_nonref (1)-> ascii (1)-> utf8 (1)-> encode (chr 0x8000), ' "\u8000"' );
9- is(Cpanel::JSON::XS-> new-> allow_nonref (1)-> ascii (1)-> utf8 (1)-> pretty (1)-> encode (chr 0x10402), " \"\\ ud801\\ udc02\"\n " );
9+ is(Cpanel::JSON::XS-> new-> allow_nonref-> ascii-> utf8-> encode(chr 0x8000), ' "\u8000"' );
10+ is(Cpanel::JSON::XS-> new-> allow_nonref-> ascii-> utf8-> pretty-> encode(chr 0x10402), " \"\\ ud801\\ udc02\"\n " );
11+
12+ ok not defined eval { Cpanel::JSON::XS-> new-> allow_nonref-> utf8-> decode(' "ü"' ) };
13+ like $@ , qr / malformed UTF-8/ ;
14+
15+ is(Cpanel::JSON::XS-> new-> allow_nonref-> decode(' "ü"' ), " ü" );
16+ is(Cpanel::JSON::XS-> new-> allow_nonref-> decode(' "\u00fc"' ), " ü" );
17+
18+ ok not defined eval { decode_json (' "\ud801\udc02' . " \x{10204}\" " , 1) };
19+ like $@ , qr / Wide character/ ;
1020
1121SKIP: {
1222 skip " 5.6" , 1 if $] < 5.008;
13- eval { Cpanel::JSON::XS-> new-> allow_nonref (1)-> utf8 (1)-> decode (' "ü"' ) };
14- like $@ , qr / malformed UTF-8/ ;
23+ is(Cpanel::JSON::XS-> new-> allow_nonref-> decode(' "\ud801\udc02' . " \x{10204}\" " ), " \x{10402}\x{10204} " );
1524}
1625
17- is(Cpanel::JSON::XS-> new-> allow_nonref (1)-> decode (' "ü"' ), " ü" );
18- is(Cpanel::JSON::XS-> new-> allow_nonref (1)-> decode (' "\u00fc"' ), " ü" );
19- if ($] < 5.008) {
20- eval { decode_json (' "\ud801\udc02' . " \x{10204}\" " , 1) };
21- like $@ , qr / malformed UTF-8/ ;
22- } else {
23- is(Cpanel::JSON::XS-> new-> allow_nonref (1)-> decode (' "\ud801\udc02' . " \x{10204}\" " ), " \x{10402}\x{10204} " );
24- }
25- is(Cpanel::JSON::XS-> new-> allow_nonref (1)-> decode (' "\"\n\\\\ \r\t\f\b"' ), " \"\012\\\015\011\014\010 " );
26+ is(Cpanel::JSON::XS-> new-> allow_nonref-> decode(' "\"\n\\\\ \r\t\f\b"' ), " \"\012\\\015\011\014\010 " );
2627
27- my $love = $] < 5.008 ? " I \342\235\244 perl " : " I ❤ perl" ;
28- is(Cpanel::JSON::XS-> new-> ascii-> encode ([$love ]),
29- $] < 5.008 ? ' [ "I \u00e2\u009d\u00a4 perl"] ' : ' ["I \u2764 perl"] ' , ' utf8 enc ascii ' );
30- is(Cpanel::JSON::XS-> new-> latin1 -> encode ([$love ]),
31- $] < 5.008 ? " [ \ " I \342\235\244 perl\" ] " : ' ["I \u2764 perl"] ' , ' utf8 enc latin1 ' );
28+ my $utf8_love = " I \342\235\244 perl" ;
29+ is(Cpanel::JSON::XS-> new-> ascii-> encode([$utf8_love ]), ' ["I \u00e2\u009d\u00a4 perl"] ' , ' utf8 enc ascii ' );
30+ is(Cpanel::JSON::XS -> new -> latin1 -> encode([ $utf8_love ]), " [ \ " I \342\235\244 perl\" ] " , ' utf8 enc latin1 ' );
31+ is(Cpanel::JSON::XS-> new-> utf8 -> encode([$utf8_love ]), " [ \" I \303\242\302\235\302\244 perl \" ] " , ' utf8 enc utf8 ' );
32+ is(Cpanel::JSON::XS -> new -> binary -> encode([ $utf8_love ]), ' [ "I \xe2\x9d\xa4 perl"] ' , ' utf8 enc binary ' );
3233
3334SKIP: {
34- skip " 5.6" , 1 if $] < 5.008;
35- require Encode;
36- # [RT #84244] wrong complaint: JSON::XS double encodes to ["I ⤠perl"]
37- # and with utf8 triple encodes it to ["I ⤠perl"]
38- if ($Encode::VERSION < 2.40 or $Encode::VERSION >= 2.54) { # Encode stricter check: Cannot decode string with wide characters
39- # see also http://stackoverflow.com/questions/12994100/perl-encode-pm-cannot-decode-string-with-wide-character
40- $love = " I \342\235\244 perl" ;
41- }
42- my $s = Encode::decode_utf8($love ); # User tries to double decode wide-char to unicode with Encode
43- is(Cpanel::JSON::XS-> new-> utf8-> encode ([$s ]), " [\" I \342\235\244 perl\" ]" , ' utf8 enc utf8 [RT #84244]' );
35+ skip " 5.6" , 4 if $] < 5.008;
36+ my $unicode_love = " I ❤ perl" ;
37+ is(Cpanel::JSON::XS-> new-> ascii-> encode([$unicode_love ]), ' ["I \u2764 perl"]' , ' unicode enc ascii' );
38+ is(Cpanel::JSON::XS-> new-> latin1-> encode([$unicode_love ]), " [\" I \\ u2764 perl\" ]" , ' unicode enc latin1' );
39+ is(Cpanel::JSON::XS-> new-> utf8-> encode([$unicode_love ]), " [\" I \342\235\244 perl\" ]" , ' unicode enc utf8' );
40+ is(Cpanel::JSON::XS-> new-> binary-> encode([$unicode_love ]), ' ["I \xe2\x9d\xa4 perl"]' , ' unicode enc binary' );
4441}
45- is(Cpanel::JSON::XS-> new-> binary-> encode ([$love ]), ' ["I \xe2\x9d\xa4 perl"]' , ' utf8 enc binary' );
4642
4743# TODO: test utf8 hash keys,
4844# test utf8 strings without any char > 0x80.
4945
5046# warn on the 66 non-characters as in core
5147{
52- my $w ;
53- require warnings;
54- warnings-> unimport($] < 5.014 ? ' utf8' : ' nonchar' );
48+ BEGIN { ' warnings' -> import ($] < 5.014 ? ' utf8' : ' nonchar' ) }
49+ my $w = ' ' ;
5550 $SIG {__WARN__ } = sub { $w = shift };
5651 my $d = Cpanel::JSON::XS-> new-> allow_nonref-> decode(' "\ufdd0"' );
5752 my $warn = $w ;
58- is ($d , " \x{fdd0} " , substr ($warn ,0,31)." ..." );
53+ {
54+ no warnings ' utf8' ;
55+ is ($d , " \x{fdd0} " , substr ($warn ,0,31)." ..." );
56+ }
5957 like ($warn , qr / ^Unicode non-character U\+ FDD0 is/ );
6058 $w = ' ' ;
6159 # higher planes
6260 $d = Cpanel::JSON::XS-> new-> allow_nonref-> decode(' "\ud83f\udfff"' );
6361 $warn = $w ;
64- is ($d , " \x{1ffff} " , substr ($warn ,0,31)." ..." );
62+ {
63+ no warnings ' utf8' ;
64+ is ($d , " \x{1ffff} " , substr ($warn ,0,31)." ..." );
65+ }
6566 like ($w , qr / ^Unicode non-character U\+ 1FFFF is/ );
6667 $w = ' ' ;
6768 $d = Cpanel::JSON::XS-> new-> allow_nonref-> decode(' "\ud87f\udffe"' );
6869 $warn = $w ;
69- is ($d , " \x{2fffe} " , substr ($warn ,0,31)." ..." );
70+ {
71+ no warnings ' utf8' ;
72+ is ($d , " \x{2fffe} " , substr ($warn ,0,31)." ..." );
73+ }
7074 like ($w , qr / ^Unicode non-character U\+ 2FFFE is/ );
7175
7276 $w = ' ' ;
@@ -77,12 +81,15 @@ is(Cpanel::JSON::XS->new->binary->encode ([$love]), '["I \xe2\x9d\xa4 perl"]', '
7781}
7882{
7983 my $w ;
80- warnings-> unimport ($] < 5.014 ? ' utf8' : ' nonchar' );
84+ BEGIN { ' warnings' -> import ($] < 5.014 ? ' utf8' : ' nonchar' ) }
8185 $SIG {__WARN__ } = sub { $w = shift };
8286 # no warning with relaxed
8387 my $d = Cpanel::JSON::XS-> new-> allow_nonref-> relaxed-> decode(' "\ufdd0"' );
8488 my $warn = $w ;
85- is ($d , " \x{fdd0} " , " no warning with relaxed" );
89+ {
90+ no warnings ' utf8' ;
91+ is ($d , " \x{fdd0} " , " no warning with relaxed" );
92+ }
8693 is($w , undef );
8794}
8895
@@ -144,9 +151,9 @@ my @ill =
144151{
145152 # these are no multibyte codepoints, just raw utf8 bytes,
146153 # so most of them work with 5.6 also.
147- $^W = 1;
154+ BEGIN { $^W = 1 }
155+ BEGIN { ' warnings' -> import ($] < 5.014 ? ' utf8' : ' nonchar' ) }
148156 my $w ;
149- warnings-> import ($] < 5.014 ? ' utf8' : ' nonchar' );
150157 $SIG {__WARN__ } = sub { $w = shift };
151158
152159 for my $ill (@ill ) {
0 commit comments