Data-AnyXfer

 view release on metacpan or  search on metacpan

t/json/upstream/01_utf8.t  view on Meta::CPAN

use Test::More tests => 13;
use utf8;
use Data::AnyXfer::JSON;

is(Data::AnyXfer::JSON->new->allow_nonref (1)->utf8 (1)->encode ("ü"), "\"\xc3\xbc\"");
is(Data::AnyXfer::JSON->new->allow_nonref (1)->encode ("ü"), "\"ü\"");

is(Data::AnyXfer::JSON->new->allow_nonref (1)->ascii (1)->utf8 (1)->encode (chr 0x8000), '"\u8000"');
is(Data::AnyXfer::JSON->new->allow_nonref (1)->ascii (1)->utf8 (1)->pretty (1)->encode (chr 0x10402), "\"\\ud801\\udc02\"\n");

SKIP: {
  skip "5.6", 1 if $] < 5.008;
  eval { Data::AnyXfer::JSON->new->allow_nonref (1)->utf8 (1)->decode ('"ü"') };
  like $@, qr/malformed UTF-8/;
}

is(Data::AnyXfer::JSON->new->allow_nonref (1)->decode ('"ü"'), "ü");
is(Data::AnyXfer::JSON->new->allow_nonref (1)->decode ('"\u00fc"'), "ü");
is(Data::AnyXfer::JSON->new->allow_nonref (1)->decode ('"\ud801\udc02' . "\x{10204}\""), "\x{10402}\x{10204}");
is(Data::AnyXfer::JSON->new->allow_nonref (1)->decode ('"\"\n\\\\\r\t\f\b"'), "\"\012\\\015\011\014\010");

my $love = $] < 5.008 ? "I \342\235\244 perl" : "I ❤ perl";
is(Data::AnyXfer::JSON->new->ascii->encode ([$love]),
   $] < 5.008 ? '["I \u00e2\u009d\u00a4 perl"]' : '["I \u2764 perl"]', 'utf8 enc ascii');
is(Data::AnyXfer::JSON->new->latin1->encode ([$love]),
      $] < 5.008 ? "[\"I \342\235\244 perl\"]" : '["I \u2764 perl"]', 'utf8 enc latin1');

SKIP: {
  skip "5.6", 1 if $] < 5.008;
  require Encode;
  # [RT #84244] wrong complaint: JSON::XS double encodes to ["I ❤ perl"]
  #             and with utf8 triple encodes it to ["I ❤ perl"]
  if ($Encode::VERSION < 2.40 || $Encode::VERSION >= 2.54) { # Encode stricter check: Cannot decode string with wide characters
    # see also http://stackoverflow.com/questions/12994100/perl-encode-pm-cannot-decode-string-with-wide-character
    $love = "I \342\235\244 perl";
  }
  my $s = Encode::decode_utf8($love); # User tries to double decode wide-char to unicode with Encode
  is(Data::AnyXfer::JSON->new->utf8->encode ([$s]), "[\"I \342\235\244 perl\"]", 'utf8 enc utf8 [RT #84244]');
}
is(Data::AnyXfer::JSON->new->binary->encode ([$love]), '["I \xe2\x9d\xa4 perl"]', 'utf8 enc binary');



( run in 0.996 second using v1.01-cache-2.11-cpan-39bf76dae61 )