Adding information with diag


All the ok() and related functions return true or false depending on their reporting success or failure. One can use this to print extra information using diag()


examples/test-perl/t/dice_any_diag.t
use strict;
use warnings;

use MyTools;

use List::MoreUtils qw(any);

use Test::More tests => 4;

my @expected = (1, 2, 3, 4, 5, 6);

for (1..4) {
    my $value = dice();
    ok( (any {$_ eq $value} @expected), 'correct number')
        or diag "Received: $value\nExpected:\n" .
            join "", map {"         $_\n"} @expected;
}

Output:


1..4
not ok 1 - correct number
#   Failed test 'correct number'
#   at t/dice_any_diag.t line 16.
# Received: 1.5
# Expected:
#          1
#          2
#          3
#          4
#          5
#          6
ok 2 - correct number
ok 3 - correct number
ok 4 - correct number
# Looks like you failed 1 test of 4.