File: fail.t

package info (click to toggle)
libcontextual-return-perl 0.003001-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 324 kB
  • ctags: 52
  • sloc: perl: 1,187; makefile: 2
file content (60 lines) | stat: -rw-r--r-- 1,723 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use Contextual::Return;

use Test::More 'no_plan';

sub eval_nok(&$$) {
    my ($block, $exception_pat, $message) = @_;
    my (undef, $file, $line) = caller;
    eval { $block->() };
    my $exception = $@;
    ok $exception => $message;
    like $exception, qr/\Q$exception_pat\E at \Q$file\E line $line/ => "Right message";
}


sub fail_with_message {
    return FAIL { 'fail_with_message() failed' }
}

if ( ::fail_with_message() ) {
    ok 0    => 'Unexpected succeeded in bool context';
}
else {
    ok 1    => 'Failed as expected in bool context';
}

eval_nok { fail_with_message() }
    'fail_with_message() failed' => 'Exception thrown in void context';

eval_nok { () = fail_with_message() }
    'fail_with_message() failed' => 'Exception thrown in list context';

eval_nok { my $x = fail_with_message(); $x+1 }
    'fail_with_message() failed' => 'Exception thrown in num context';

eval_nok { my $x = fail_with_message(); $x.'a' }
    'fail_with_message() failed' => 'Exception thrown in str context';


sub fail_auto_message {
    return FAIL;
}

if ( ::fail_auto_message() ) {
    ok 0    => 'Unexpected succeeded in bool context';
}
else {
    ok 1    => 'Failed as expected in bool context';
}

eval_nok { fail_auto_message() }
    'Call to main::fail_auto_message() failed' => 'Exception thrown in void context';

eval_nok { () = fail_auto_message() }
    'Call to main::fail_auto_message() failed' => 'Exception thrown in list context';

eval_nok { my $x = fail_auto_message(); $x+1 }
    'Call to main::fail_auto_message() failed' => 'Exception thrown in num context';

eval_nok { my $x = fail_auto_message(); $x.'a' }
    'Call to main::fail_auto_message() failed' => 'Exception thrown in str context';