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