在Perl v5.10.1中,我需要将原生perl结构与unicode字符与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
我无法解释最后一个案例的情况,为什么2个结构不相等?我试过了 R T F M 这并没有真正提高我对这个问题的理解。
这是输出(由于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兼容?
答案 0 :(得分:3)
此测试将通过:
use Encode;
cmp_deeply( [ Encode::decode("utf8","≥") ],
JSON::decode_json('["≥"]'), 'utf8 test ≥≥');
JSON解码器将输入视为UTF-8编码并返回已解码的字符串。您的原始测试(测试4)将UTF-8编码的字符串(两个八位字节)与已解码的字符串(单个宽字符)进行比较。