Add support for 'finally' blocks
[p5sagit/Try-Tiny.git] / t / basic.t
CommitLineData
3176feef 1#!/usr/bin/perl
2
3use strict;
06a9e570 4#use warnings;
3176feef 5
7195fc08 6use Test::More tests => 24;
3176feef 7
8BEGIN { use_ok 'Try::Tiny' };
9
10sub _eval {
11 local $@;
12 local $Test::Builder::Level = $Test::Builder::Level + 2;
13 return ( scalar(eval { $_[0]->(); 1 }), $@ );
14}
15
16
17sub lives_ok (&$) {
18 my ( $code, $desc ) = @_;
19 local $Test::Builder::Level = $Test::Builder::Level + 1;
20
21 my ( $ok, $error ) = _eval($code);
22
23 ok($ok, $desc );
24
25 diag "error: $@" unless $ok;
26}
27
28sub throws_ok (&$$) {
29 my ( $code, $regex, $desc ) = @_;
30 local $Test::Builder::Level = $Test::Builder::Level + 1;
31
32 my ( $ok, $error ) = _eval($code);
33
34 if ( $ok ) {
35 fail($desc);
36 } else {
37 like($error || '', $regex, $desc );
38 }
39}
40
41
511c05ca 42my $prev;
43
3176feef 44lives_ok {
45 try {
46 die "foo";
47 };
48} "basic try";
49
50throws_ok {
51 try {
52 die "foo";
53 } catch { die $_ };
54} qr/foo/, "rethrow";
55
56
57{
58 local $@ = "magic";
59 is( try { 42 }, 42, "try block evaluated" );
60 is( $@, "magic", '$@ untouched' );
61}
62
63{
64 local $@ = "magic";
65 is( try { die "foo" }, undef, "try block died" );
66 is( $@, "magic", '$@ untouched' );
67}
68
69{
70 local $@ = "magic";
71 like( (try { die "foo" } catch { $_ }), qr/foo/, "catch block evaluated" );
72 is( $@, "magic", '$@ untouched' );
73}
74
2ab8fb1c 75is( scalar(try { "foo", "bar", "gorch" }), "gorch", "scalar context" );
3176feef 76is_deeply( [ try {qw(foo bar gorch)} ], [qw(foo bar gorch)], "list context" );
77
7195fc08 78{
79 my ($sub) = catch { my $a = $_; };
80 is(ref($sub), 'Try::Tiny::Catch', 'Checking catch subroutine scalar reference is correctly blessed');
81 my ($sub) = finally { my $a = $_; };
82 is(ref($sub), 'Try::Tiny::Finally', 'Checking finally subroutine scalar reference is correctly blessed');
83}
3176feef 84
e605bd35 85lives_ok {
86 try {
87 die "foo";
88 } catch {
89 my $err = shift;
90
91 try {
92 like $err, qr/foo/;
93 } catch {
94 fail("shouldn't happen");
95 };
96
97 pass "got here";
98 }
99} "try in try catch block";
100
101throws_ok {
102 try {
103 die "foo";
104 } catch {
105 my $err = shift;
106
107 try { } catch { };
108
109 die "rethrowing $err";
110 }
111} qr/rethrowing foo/, "rethrow with try in catch block";
112
3176feef 113
1f7c5af6 114sub Evil::DESTROY {
115 eval { "oh noes" };
116}
117
118sub Evil::new { bless { }, $_[0] }
119
3176feef 120{
121 local $@ = "magic";
122 local $_ = "other magic";
123
124 try {
1f7c5af6 125 my $object = Evil->new;
3176feef 126 die "foo";
127 } catch {
128 pass("catch invoked");
129 local $TODO = "i don't think we can ever make this work sanely, maybe with SIG{__DIE__}";
130 like($_, qr/foo/);
131 };
132
133 is( $@, "magic", '$@ untouched' );
134 is( $_, "other magic", '$_ untouched' );
135}
511c05ca 136
137{
ac4f5f9f 138 my ( $caught, $prev );
511c05ca 139
140 {
141 local $@;
142
143 eval { die "bar\n" };
144
145 is( $@, "bar\n", 'previous value of $@' );
146
147 try {
148 die {
149 prev => $@,
150 }
151 } catch {
152 $caught = $_;
ac4f5f9f 153 $prev = $@;
511c05ca 154 }
155 }
156
157 is_deeply( $caught, { prev => "bar\n" }, 'previous value of $@ available for capture' );
ac4f5f9f 158 is( $prev, "bar\n", 'previous value of $@ also available in catch block' );
511c05ca 159}