[Vienna-pm] UTF-8 Output in Test::More u.ä.

Peter J. Holzer hjp-vienna-pm-list at hjp.at
Fri Aug 29 07:16:47 PDT 2008


Ich bin vermutlich nicht der einzige, der Nicht-US-ASCII-Daten
verarbeiten muss, und auch Tests schreibt, die überprüfen sollen, ob das
auch richtig geschieht. Leider schaut dann der Output oft so aus:

    1..5
    ok 1
    ok 2
    ok 3 - �erraschung!
    not ok 4 - short change
    #   Failed test 'short change'
    #   at ./foo line 16.
    Wide character in print at /usr/share/perl/5.10/Test/Builder.pm line 1266.
    #          got: '¥'
    #     expected: '€'
    not ok 5 # TODO machen wir sp�er
    #   Failed (TODO) test at ./foo line 19.
    # Looks like you failed 1 test of 5.

Verstümmelte Umlaute, "Wide character in print ..." Warnungen, etc. 
Nicht schön. Und »binmode STDOUT, "encoding(UTF-8)"« nützt nichts, weil
Test::More seine eigenen Filedescriptoren verwendet.

Aber: Test::More verwendet Test::Builder, und Test::Builder->new liefert
ein Singleton. Und das kann man dann ändern:

    #!/usr/bin/perl
    use Test::More tests => 5;
    use utf8;

    my $tb = Test::Builder->new;
    binmode $tb->output,         ':encoding(UTF-8)';
    binmode $tb->failure_output, ':encoding(UTF-8)';
    binmode $tb->todo_output,    ':encoding(UTF-8)';

    sleep(1);
    ok(1);
    sleep(2);
    ok(1);
    sleep(1);
    ok(1, "Überraschung!");
    is('¥', '€', "short change");
    TODO: {
	local $TODO = "machen wir später";
	ok(0);
    }
    __END__

Et voilà:

    1..5
    ok 1
    ok 2
    ok 3 - Überraschung!
    not ok 4 - short change
    #   Failed test 'short change'
    #   at ./foo line 16.
    #          got: '¥'
    #     expected: '€'
    not ok 5 # TODO machen wir später
    #   Failed (TODO) test at ./foo line 19.
    # Looks like you failed 1 test of 5.

Schaut doch gleich viel besser aus.

	hp

-- 
   _  | Peter J. Holzer    | Openmoko has already embedded
|_|_) | Sysadmin WSR       | voting system.
| |   | hjp at hjp.at         | Named "If you want it -- write it"
__/   | http://www.hjp.at/ |  -- Ilja O. on community at lists.openmoko.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://mail.pm.org/pipermail/vienna-pm/attachments/20080829/1e7d2042/attachment.bin>


More information about the Vienna-pm mailing list