Clarify exception on unexpected try() arguments
[p5sagit/Try-Tiny.git] / t / erroneous_usage.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 6;
7 use Try::Tiny;
8
9 sub _eval {
10   local $@;
11   local $Test::Builder::Level = $Test::Builder::Level + 2;
12   return ( scalar(eval { $_[0]->(); 1 }), $@ );
13 }
14
15 sub throws_ok (&$$) {
16   my ( $code, $regex, $desc ) = @_;
17   local $Test::Builder::Level = $Test::Builder::Level + 1;
18
19   my ( $ok, $error ) = _eval($code);
20
21   if ( $ok ) {
22     fail($desc);
23   } else {
24     like($error || '', $regex, $desc );
25   }
26 }
27
28 throws_ok {
29   try { 1 }; catch { 2 };
30 } qr/\QUseless bare catch()/, 'Bare catch() detected';
31
32 throws_ok {
33   try { 1 }; finally { 2 };
34 } qr/\QUseless bare finally()/, 'Bare finally() detected';
35
36 throws_ok {
37   try { 1 }; catch { 2 } finally { 2 };
38 } qr/\QUseless bare catch()/, 'Bare catch()/finally() detected';
39
40 throws_ok {
41   try { 1 }; finally { 2 } catch { 2 };
42 } qr/\QUseless bare finally()/, 'Bare finally()/catch() detected';
43
44
45 throws_ok {
46   try { 1 } catch { 2 } catch { 3 } finally { 4 } finally { 5 }
47 } qr/\QA try() may not be followed by multiple catch() blocks/, 'Multi-catch detected';
48
49
50 throws_ok {
51   try { 1 } catch { 2 }
52   do { 2 }
53 } qr/\Qtry() encountered an unexpected argument (2) - perhaps a missing semi-colon before or at/,
54   'Unterminated try detected';