xref: /openbsd-src/gnu/usr.bin/perl/cpan/autodie/t/context.t (revision b39c515898423c8d899e35282f4b395f7cad3298)
1*b39c5158Smillert#!/usr/bin/perl -w
2*b39c5158Smillertuse strict;
3*b39c5158Smillert
4*b39c5158Smillertuse Test::More;
5*b39c5158Smillert
6*b39c5158Smillertplan 'no_plan';
7*b39c5158Smillert
8*b39c5158Smillertsub list_return {
9*b39c5158Smillert    return if @_;
10*b39c5158Smillert    return qw(foo bar baz);
11*b39c5158Smillert}
12*b39c5158Smillert
13*b39c5158Smillertsub list_return2 {
14*b39c5158Smillert    return if @_;
15*b39c5158Smillert    return qw(foo bar baz);
16*b39c5158Smillert}
17*b39c5158Smillert
18*b39c5158Smillert# Returns a list presented to it, but also returns a single
19*b39c5158Smillert# undef if given a list of a single undef.  This mimics the
20*b39c5158Smillert# behaviour of many user-defined subs and built-ins (eg: open) that
21*b39c5158Smillert# always return undef regardless of context.
22*b39c5158Smillert
23*b39c5158Smillertsub list_mirror {
24*b39c5158Smillert    return undef if (@_ == 1 and not defined $_[0]);
25*b39c5158Smillert    return @_;
26*b39c5158Smillert
27*b39c5158Smillert}
28*b39c5158Smillert
29*b39c5158Smillertuse Fatal qw(list_return);
30*b39c5158Smillertuse Fatal qw(:void list_return2);
31*b39c5158Smillert
32*b39c5158SmillertTODO: {
33*b39c5158Smillert
34*b39c5158Smillert    # Clobbering context was documented as a bug in the original
35*b39c5158Smillert    # Fatal, so we'll still consider it a bug here.
36*b39c5158Smillert
37*b39c5158Smillert    local $TODO = "Fatal clobbers context, just like it always has.";
38*b39c5158Smillert
39*b39c5158Smillert    my @list = list_return();
40*b39c5158Smillert
41*b39c5158Smillert    is_deeply(\@list,[qw(foo bar baz)],'fatal sub works in list context');
42*b39c5158Smillert}
43*b39c5158Smillert
44*b39c5158Smillerteval {
45*b39c5158Smillert    my @line = list_return(1);  # Should die
46*b39c5158Smillert};
47*b39c5158Smillert
48*b39c5158Smillertok($@,"List return fatalised");
49*b39c5158Smillert
50*b39c5158Smillert### Tests where we've fatalised our function with :void ###
51*b39c5158Smillert
52*b39c5158Smillertmy @list2 = list_return2();
53*b39c5158Smillert
54*b39c5158Smillertis_deeply(\@list2,[qw(foo bar baz)],'fatal sub works in list context');
55*b39c5158Smillert
56*b39c5158Smillerteval {
57*b39c5158Smillert    my @line = list_return2(1);  # Shouldn't die
58*b39c5158Smillert};
59*b39c5158Smillert
60*b39c5158Smillertok(! $@,"void List return fatalised survives when non-void");
61*b39c5158Smillert
62*b39c5158Smillerteval {
63*b39c5158Smillert    list_return2(1);
64*b39c5158Smillert};
65*b39c5158Smillert
66*b39c5158Smillertok($@,"void List return fatalised");
67