perl – 当我使用JSON :: decode_json时,Test :: Deep :: cmp_de
发布时间:2020-12-16 06:15:45 所属栏目:大数据 来源:网络整理
导读:在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'); # w
在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(["≥"],'utf8 test ≥1'); # will fail cmp_deeply(["1"],JSON::decode_json('["≥"]'),'utf8 test 1≥'); # will fail cmp_deeply(["≥"],'utf8 test ≥≥'); # should pass 我无法解释最后一个案例的情况,为什么2个结构不相等?我试过了 这是输出(由于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 : 'a¥' # expect : '≥' 当我尝试使用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. 有没有办法让它工作 – 像使用magical_unicode_solution;? 或者我应该以不同的方式进行测试,以使其与Unicode兼容? 解决方法
该测试将通过:
use Encode; cmp_deeply( [ Encode::decode("utf8","≥") ],'utf8 test ≥≥'); JSON解码器将输入视为UTF-8编码并返回已解码的字符串.您的原始测试(测试4)将UTF-8编码的字符串(两个八位字节)与解码的字符串(单个宽字符)进行比较. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |