Commit | Line | Data |
9d0e0466 |
1 | use strict; |
2 | use warnings; |
3 | |
d2ae14ad |
4 | use Test::More tests => 8; |
9d0e0466 |
5 | use Try::Tiny; |
6 | |
7 | sub _eval { |
8 | local $@; |
9 | local $Test::Builder::Level = $Test::Builder::Level + 2; |
10 | return ( scalar(eval { $_[0]->(); 1 }), $@ ); |
11 | } |
12 | |
13 | sub 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 | |
26 | throws_ok { |
27 | try { 1 }; catch { 2 }; |
28 | } qr/\QUseless bare catch()/, 'Bare catch() detected'; |
29 | |
30 | throws_ok { |
31 | try { 1 }; finally { 2 }; |
32 | } qr/\QUseless bare finally()/, 'Bare finally() detected'; |
33 | |
34 | throws_ok { |
35 | try { 1 }; catch { 2 } finally { 2 }; |
36 | } qr/\QUseless bare catch()/, 'Bare catch()/finally() detected'; |
37 | |
38 | throws_ok { |
39 | try { 1 }; finally { 2 } catch { 2 }; |
40 | } qr/\QUseless bare finally()/, 'Bare finally()/catch() detected'; |
41 | |
42 | |
43 | throws_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 | |
48 | throws_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 | |
54 | sub foo { |
55 | try { 0 }; catch { 2 } |
56 | } |
57 | |
58 | throws_ok { |
59 | if (foo()) { |
60 | # ... |
61 | } |
62 | } qr/\QUseless bare catch/, |
63 | 'Bare catch at the end of a function call'; |
64 | |
65 | sub bar { |
66 | try { 0 }; finally { 2 } |
67 | } |
68 | |
69 | throws_ok { |
70 | if (bar()) { |
71 | # ... |
72 | } |
73 | } qr/\QUseless bare finally/, |
74 | 'Bare finally at the end of a function call'; |