File: basic.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 (166 lines) | stat: -rw-r--r-- 3,083 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 25;

use Try::Tiny;

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


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

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

  ok($ok, $desc );

  diag "error: $@" unless $ok;
}

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 );
  }
}


my $prev;

lives_ok {
  try {
    die "foo";
  };
} "basic try";

throws_ok {
  try {
    die "foo";
  } catch { die $_ };
} qr/foo/, "rethrow";


{
  local $@ = "magic";
  is( try { 42 }, 42, "try block evaluated" );
  is( $@, "magic", '$@ untouched' );
}

{
  local $@ = "magic";
  is( try { die "foo" }, undef, "try block died" );
  is( $@, "magic", '$@ untouched' );
}

{
  local $@ = "magic";
  like( (try { die "foo" } catch { $_ }), qr/foo/, "catch block evaluated" );
  is( $@, "magic", '$@ untouched' );
}

is( scalar(try { "foo", "bar", "gorch" }), "gorch", "scalar context try" );
is_deeply( [ try {qw(foo bar gorch)} ], [qw(foo bar gorch)], "list context try" );

is( scalar(try { die } catch { "foo", "bar", "gorch" }), "gorch", "scalar context catch" );
is_deeply( [ try { die } catch {qw(foo bar gorch)} ], [qw(foo bar gorch)], "list context catch" );


{
  my ($sub) = catch { my $a = $_; };
  is(ref($sub), 'Try::Tiny::Catch', 'Checking catch subroutine scalar reference is correctly blessed');
}

{
  my ($sub) = finally { my $a = $_; };
  is(ref($sub), 'Try::Tiny::Finally', 'Checking finally subroutine scalar reference is correctly blessed');
}

lives_ok {
  try {
    die "foo";
  } catch {
    my $err = shift;

    try {
      like $err, qr/foo/;
    } catch {
      fail("shouldn't happen");
    };

    pass "got here";
  }
} "try in try catch block";

throws_ok {
  try {
    die "foo";
  } catch {
    my $err = shift;

    try { } catch { };

    die "rethrowing $err";
  }
} qr/rethrowing foo/, "rethrow with try in catch block";


sub Evil::DESTROY {
  eval { "oh noes" };
}

sub Evil::new { bless { }, $_[0] }

{
  local $@ = "magic";
  local $_ = "other magic";

  try {
    my $object = Evil->new;
    die "foo";
  } catch {
    pass("catch invoked");
    local $TODO = "i don't think we can ever make this work sanely, maybe with SIG{__DIE__}" if $] < 5.014;
    like($_, qr/foo/);
  };

  is( $@, "magic", '$@ untouched' );
  is( $_, "other magic", '$_ untouched' );
}

{
  my ( $caught, $prev );

  {
    local $@;

    eval { die "bar\n" };

    is( $@, "bar\n", 'previous value of $@' );

    try {
      die {
        prev => $@,
      }
    } catch {
      $caught = $_;
      $prev = $@;
    }
  }

  is_deeply( $caught, { prev => "bar\n" }, 'previous value of $@ available for capture' );
  is( $prev, "bar\n", 'previous value of $@ also available in catch block' );
}