File: erroneous_usage.t

package info (click to toggle)
libtry-tiny-perl 0.20-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 188 kB
  • sloc: perl: 494; makefile: 2
file content (77 lines) | stat: -rw-r--r-- 1,557 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 8;

use Try::Tiny;

sub _eval {
  local $@;
  local $Test::Builder::Level = $Test::Builder::Level + 2;
  return ( scalar(eval { $_[0]->(); 1 }), $@ );
}

sub throws_ok (&$$) {
  my ( $code, $regex, $desc ) = @_;
  local $Test::Builder::Level = $Test::Builder::Level + 1;

  my ( $ok, $error ) = _eval($code);

  if ( $ok ) {
    fail($desc);
  } else {
    like($error || '', $regex, $desc );
  }
}

throws_ok {
  try { 1 }; catch { 2 };
} qr/\QUseless bare catch()/, 'Bare catch() detected';

throws_ok {
  try { 1 }; finally { 2 };
} qr/\QUseless bare finally()/, 'Bare finally() detected';

throws_ok {
  try { 1 }; catch { 2 } finally { 2 };
} qr/\QUseless bare catch()/, 'Bare catch()/finally() detected';

throws_ok {
  try { 1 }; finally { 2 } catch { 2 };
} qr/\QUseless bare finally()/, 'Bare finally()/catch() detected';


throws_ok {
  try { 1 } catch { 2 } catch { 3 } finally { 4 } finally { 5 }
} qr/\QA try() may not be followed by multiple catch() blocks/, 'Multi-catch detected';


throws_ok {
  try { 1 } catch { 2 }
  do { 2 }
} qr/\Qtry() encountered an unexpected argument (2) - perhaps a missing semi-colon before or at/,
  'Unterminated try detected';

sub foo {
  try { 0 }; catch { 2 }
}

throws_ok {
  if (foo()) {
    # ...
  }
} qr/\QUseless bare catch/,
  'Bare catch at the end of a function call';

sub bar {
  try { 0 }; finally { 2 }
}

throws_ok {
  if (bar()) {
    # ...
  }
} qr/\QUseless bare finally/,
  'Bare finally at the end of a function call';