summaryrefslogtreecommitdiff
path: root/t/op/defer.t
blob: c651312cb57c86d0413098ff19a80e487860858d (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!./perl

BEGIN {
    chdir 't' if -d 't';
    require './test.pl';
    set_up_inc('../lib');
}

plan 26;

use feature 'defer';
no warnings 'experimental::defer';

{
    my $x = "";
    {
        defer { $x = "a" }
    }
    is($x, "a", 'defer block is invoked');

    {
        defer {
            $x = "";
            $x .= "abc";
            $x .= "123";
        }
    }
    is($x, "abc123", 'defer block can contain multiple statements');

    {
       defer {}
    }
    ok(1, 'Empty defer block parses OK');
}

{
    my $x = "";
    {
        defer { $x .= "a" }
        defer { $x .= "b" }
        defer { $x .= "c" }
    }
    is($x, "cba", 'defer blocks happen in LIFO order');
}

{
    my $x = "";

    {
        defer { $x .= "a" }
        $x .= "A";
    }

    is($x, "Aa", 'defer blocks happen after the main body');
}

{
    my $x = "";

    foreach my $i (qw( a b c )) {
        defer { $x .= $i }
    }

    is($x, "abc", 'defer block happens for every iteration of foreach');
}

{
    my $x = "";

    my $cond = 0;
    if( $cond ) {
        defer { $x .= "XXX" }
    }

    is($x, "", 'defer block does not happen inside non-taken conditional branch');
}

{
    my $x = "";

    while(1) {
        last;
        defer { $x .= "a" }
    }

    is($x, "", 'defer block does not happen if entered but unencountered');
}

{
   my $x = "";

   my $counter = 1;
   {
      defer { $x .= "A" }
      redo if $counter++ < 5;
   }

   is($x, "AAAAA", 'defer block can happen multiple times');
}

{
    my $x = "";

    {
        defer {
            $x .= "a";
            defer {
                $x .= "b";
            }
        }
    }

    is($x, "ab", 'defer block can contain another defer');
}

{
    my $x = "";
    my $value = do {
        defer { $x .= "before" }
        "value";
    };

    is($x, "before", 'defer blocks run inside do { }');
    is($value, "value", 'defer block does not disturb do { } value');
}

{
    my $x = "";
    my $sub = sub {
        defer { $x .= "a" }
    };

    $sub->();
    $sub->();
    $sub->();

    is($x, "aaa", 'defer block inside sub');
}

{
    my $x = "";
    my $sub = sub {
        return;
        defer { $x .= "a" }
    };

    $sub->();

    is($x, "", 'defer block inside sub does not happen if entered but returned early');
}

{
   my $x = "";

   my sub after {
      $x .= "c";
   }

   my sub before {
      $x .= "a";
      defer { $x .= "b" }
      goto \&after;
   }

   before();

   is($x, "abc", 'defer block invoked before tail-call');
}

# Sequencing with respect to variable cleanup

{
    my $var = "outer";
    my $x;
    {
        my $var = "inner";
        defer { $x = $var }
    }

    is($x, "inner", 'defer block captures live value of same-scope lexicals');
}

{
    my $var = "outer";
    my $x;
    {
        defer { $x = $var }
        my $var = "inner";
    }

    is ($x, "outer", 'defer block correctly captures outer lexical when only shadowed afterwards');
}

{
    our $var = "outer";
    {
        local $var = "inner";
        defer { $var = "finally" }
    }

    is($var, "outer", 'defer after localization still unlocalizes');
}

{
    our $var = "outer";
    {
        defer { $var = "finally" }
        local $var = "inner";
    }

    is($var, "finally", 'defer before localization overwrites');
}

# Interactions with exceptions

{
    my $x = "";
    my $sub = sub {
        defer { $x .= "a" }
        die "Oopsie\n";
    };

    my $e = defined eval { $sub->(); 1 } ? undef : $@;

    is($x, "a", 'defer block still runs during exception unwind');
    is($e, "Oopsie\n", 'Thrown exception still occurs after defer');
}

{
    my $sub = sub {
        defer { die "Oopsie\n"; }
        return "retval";
    };

    my $e = defined eval { $sub->(); 1 } ? undef : $@;

    is($e, "Oopsie\n", 'defer block can throw exception');
}

{
    my $sub = sub {
        defer { die "Oopsie 1\n"; }
        die "Oopsie 2\n";
    };

    my $e = defined eval { $sub->(); 1 } ? undef : $@;

    # TODO: Currently the first exception gets lost without even a warning
    #   We should consider what the behaviour ought to be here
    # This test is happy for either exception to be seen, does not care which
    like($e, qr/^Oopsie \d\n/, 'defer block can throw exception during exception unwind');
}

{
    my $sub = sub {
        while(1) {
            goto HERE;
            defer { HERE: 1; }
        }
    };

    my $e = defined eval { $sub->(); 1 } ? undef : $@;
    like($e, qr/^Can't "goto" into a "defer" block /,
        'Cannot goto into defer block');
}

{
    # strictness failures are only checked at optree finalization time. This
    # is a good way to test if that happens.
    my $ok = eval 'defer { use strict; foo }';
    my $e = $@;

    ok(!$ok, 'defer BLOCK finalizes optree');
    like($e, qr/^Bareword "foo" not allowed while "strict subs" in use at /,
        'Error from finalization');
}