当我使用JSON::decode_json时,在Test::Deep::cmp_deep中比较unicode字符串失败



在Perl v5.10.1中,我需要将带有unicode字符的本地Perl结构与JSON::decode_json创建的类似结构进行比较。

的例子:

use strict; use warnings;
#use utf8;
use JSON;
use Test::Deep qw(cmp_deeply);
cmp_deeply(["1"], JSON::decode_json('["1"]'), 'utf8 test 11'); # will pass
cmp_deeply(["≥"], JSON::decode_json('["1"]'), 'utf8 test ≥1'); # will fail
cmp_deeply(["1"], JSON::decode_json('["≥"]'), 'utf8 test 1≥'); # will fail
cmp_deeply(["≥"], JSON::decode_json('["≥"]'), 'utf8 test ≥≥'); # should pass

我无法解释最后一种情况,为什么两个结构不等于?我试过RTFM这并没有真正提高我对这个问题的理解。

下面是输出(因为TAP太冗长,所以稍微编辑了一下):

ok 1 - utf8 test 11
not ok 2 - utf8 test ≥1
# Compared $data->[0]
#    got : '≥'
# expect : '1'
not ok 3 - utf8 test 1≥
Wide character in print at Test/Builder.pm line 1698.
# Compared $data->[0]
#    got : '1'
# expect : '≥'
not ok 4 - utf8 test ≥≥
Wide character in print at Test/Builder.pm line 1698.
# Compared $data->[0]
#    got : 'â¥'
# expect : '≥'

当我尝试使用use utf8;时,情况更糟(脚本在第二次测试后死亡):

ok 1 - utf8 test 11
not ok 2 - utf8 test ≥1
Wide character in print at Test/Builder.pm line 1698.
Wide character in print at Test/Builder.pm line 1698.
Wide character in print at Test/Builder.pm line 1698.
# Compared $data->[0]
#    got : '≥'
# expect : '1'
Wide character in subroutine entry at ...
# Tests were run but no plan was declared and done_testing() was not seen.

我还尝试了一个可以进行比较的解决方案…

use utf8;
cmp_deeply(["≥"], JSON->new->utf8(0)->decode('["≥"]'), 'utf8 test ≥≥');

…但是我仍然得到愚蠢的警告:

ok 1 - utf8 test ≥≥
Wide character in print at Test/Builder.pm line 1698.

是否有一种方法可以让它工作-像use magical_unicode_solution;一样?

或者我应该用一种不同的方式来做我的测试,使它与Unicode兼容?

此测试将通过:

use Encode;
cmp_deeply( [ Encode::decode("utf8","≥") ],
    JSON::decode_json('["≥"]'), 'utf8 test ≥≥');

JSON解码器将输入作为UTF-8编码并返回解码后的字符串。您的原始测试(测试4)比较了UTF-8编码字符串(两个八位字节)和解码字符串(单个宽字符)。

最新更新