time to release as stable
[p5sagit/Try-Tiny.git] / t / erroneous_usage.t
CommitLineData
9d0e0466 1use strict;
2use warnings;
3
d2ae14ad 4use Test::More tests => 8;
9d0e0466 5use Try::Tiny;
6
7sub _eval {
8 local $@;
9 local $Test::Builder::Level = $Test::Builder::Level + 2;
10 return ( scalar(eval { $_[0]->(); 1 }), $@ );
11}
12
13sub throws_ok (&$$) {
14 my ( $code, $regex, $desc ) = @_;
15 local $Test::Builder::Level = $Test::Builder::Level + 1;
16
17 my ( $ok, $error ) = _eval($code);
18
19 if ( $ok ) {
20 fail($desc);
21 } else {
22 like($error || '', $regex, $desc );
23 }
24}
25
26throws_ok {
27 try { 1 }; catch { 2 };
28} qr/\QUseless bare catch()/, 'Bare catch() detected';
29
30throws_ok {
31 try { 1 }; finally { 2 };
32} qr/\QUseless bare finally()/, 'Bare finally() detected';
33
34throws_ok {
35 try { 1 }; catch { 2 } finally { 2 };
36} qr/\QUseless bare catch()/, 'Bare catch()/finally() detected';
37
38throws_ok {
39 try { 1 }; finally { 2 } catch { 2 };
40} qr/\QUseless bare finally()/, 'Bare finally()/catch() detected';
41
42
43throws_ok {
44 try { 1 } catch { 2 } catch { 3 } finally { 4 } finally { 5 }
45} qr/\QA try() may not be followed by multiple catch() blocks/, 'Multi-catch detected';
46
4c5b99d6 47
48throws_ok {
49 try { 1 } catch { 2 }
50 do { 2 }
51} qr/\Qtry() encountered an unexpected argument (2) - perhaps a missing semi-colon before or at/,
52 'Unterminated try detected';
d2ae14ad 53
54sub foo {
55 try { 0 }; catch { 2 }
56}
57
58throws_ok {
59 if (foo()) {
60 # ...
61 }
62} qr/\QUseless bare catch/,
63 'Bare catch at the end of a function call';
64
65sub bar {
66 try { 0 }; finally { 2 }
67}
68
69throws_ok {
70 if (bar()) {
71 # ...
72 }
73} qr/\QUseless bare finally/,
74 'Bare finally at the end of a function call';