Test-Warnings

 view release on metacpan or  search on metacpan

examples/warning_like.t  view on Meta::CPAN

use warnings;

# this test demonstrates the issue described in the main documentation, where
# a test of the contents of a captured warning can inadvertently match another
# part of the stack trace (the pattern argument to the test sub, itself!)

use Test::More;
use Test::Warnings ':all';
use Carp 'cluck';

sub warning_like(&$;$)
{
    my ($code, $pattern, $name) = @_;
    like( &warning($code), $pattern, $name );
}

warning_like(sub { cluck 'blah blah' }, qr/foo/, 'foo seems to appear in the warning');

# the test only passes when we invert it
unlike(
    ( warning { cluck 'blah blah' } || '' ),

lib/Test/Warnings.pm  view on Meta::CPAN

    }
    my $code = shift;
    my @warnings;
    local $SIG{__WARN__} = sub {
        push @warnings, shift;
    };
    $code->();
    @warnings;
}

sub warning(&) {
    my @warnings = &warnings(@_);
    return @warnings == 1 ? $warnings[0] : \@warnings;
}

# check for any forbidden warnings, and record that we have done so
# so we do not check again via END
sub done_testing {
    if (Test2::Tools::Basic->can('done_testing')) {
        if (not $no_end_test and not $done_testing_called) {
            # we could use $ctx to create the test, which means not having to adjust Level,



( run in 0.285 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )