Why do my Perl tests fail with use encoding 'utf8'?

前端 未结 3 468
再見小時候
再見小時候 2020-11-30 12:30

I\'m puzzled with this test script:

#!perl

use strict;
use warnings;
use encoding \'utf8\';
use Test::More \'no_plan\';

ok(\'áá\' =~ m/á/, \'ok direct matc         


        
3条回答
  •  时光说笑
    2020-11-30 13:18

    The Test::More documentation contains a fix for this issue, which I just found today (and this entry shows higher in the googles).

    utf8 / "Wide character in print"

    If you use utf8 or other non-ASCII characters with Test::More you might get a "Wide character in print" warning. Using binmode STDOUT, ":utf8" will not fix it. Test::Builder (which powers Test::More) duplicates STDOUT and STDERR. So any changes to them, including changing their output disciplines, will not be seem by Test::More. The work around is to change the filehandles used by Test::Builder directly.

    my $builder = Test::More->builder;
    binmode $builder->output,         ":utf8";
    binmode $builder->failure_output, ":utf8";
    binmode $builder->todo_output,    ":utf8";
    

    I added this bit of boilerplate to my testing code and it works a charm.

提交回复
热议问题