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