File: constant-defer.t

package info (click to toggle)
libconstant-defer-perl 6-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 288 kB
  • sloc: perl: 1,603; sh: 128; makefile: 2
file content (381 lines) | stat: -rwxr-xr-x 9,861 bytes parent folder | download | duplicates (3)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/perl -w

# Copyright 2008, 2009, 2010, 2011, 2013, 2015 Kevin Ryde

# This file is part of constant-defer.
#
# constant-defer is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# constant-defer is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with constant-defer.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use constant::defer;
use Test;

use lib 't';
use MyTestHelpers;
MyTestHelpers::nowarnings();

plan tests => 49;

sub streq_array {
  my ($a1, $a2) = @_;
  while (@$a1 && @$a2) {
    unless ((! defined $a1->[0] && ! defined $a2->[0])
            || (defined $a1->[0]
                && defined $a2->[0]
                && $a1->[0] eq $a2->[0])) {
      return 0;
    }
    shift @$a1;
    shift @$a2;
  }
  return (@$a1 == @$a2);
}

#------------------------------------------------------------------------------
# VERSION

{
  my $want_version = 6;
  ok ($constant::defer::VERSION, $want_version, 'VERSION variable');
  ok (constant::defer->VERSION,  $want_version, 'VERSION class method');
  ok (eval { constant::defer->VERSION($want_version); 1 },
      1,
      "VERSION class check $want_version");
  my $check_version = $want_version + 1000;
  ok (! eval { constant::defer->VERSION($check_version); 1 },
      1,
      "VERSION class check $check_version");
}

#------------------------------------------------------------------------------
# plain name

my $have_weaken;
my $skip_weaken;

{
  my $foo_runs = 0;
  use constant::defer FOO => sub { $foo_runs++; return 123 };

  my $orig_code;
  $orig_code = \&FOO;

  ok (FOO, 123, 'FOO first run');
  ok ($foo_runs, 1, 'FOO first runs code');

  ok (&$orig_code(), 123, 'FOO orig second run');
  ok ($foo_runs, 1, "FOO orig second doesn't run code");

  $have_weaken = eval { require Scalar::Util;
                        my $x = [ 'hello' ];
                        Scalar::Util::weaken($x);
                        if (defined $x) {
                          die "Oops, weakening didn't garbage collect";
                        }
                        1
                      };
  if (! $have_weaken) {
    MyTestHelpers::diag ("Scalar::Util::weaken() not available -- ",$@);
    $skip_weaken = 'due to Scalar::Util::weaken() not available';
  }

  {
    if ($have_weaken) {
      Scalar::Util::weaken ($orig_code);
    }
    skip ($skip_weaken,
          ! defined $orig_code,
          1,
          'orig FOO code garbage collected');

    $foo_runs = 0;
    ok (FOO, 123, 'FOO second run');
    ok ($foo_runs, 0, "FOO second doesn't run code");
  }
}

#------------------------------------------------------------------------------
# fully qualified name

{
  my $runs = 0;
  use constant::defer 'Some::Non::Package::Place::func' => sub {
    $runs = 1; return 'xyz' };

  ok (Some::Non::Package::Place::func(), 'xyz',
      'explicit package first run');
  ok ($runs, 1,
      'explicit package first run runs code');

  $runs = 0;
  ok (Some::Non::Package::Place::func(), 'xyz',
      'explicit package second run');
  ok ($runs, 0,
      'explicit package second run doesn\'t run code');
}

#------------------------------------------------------------------------------
# array value

{
  my $runs = 0;
  use constant::defer THREE => sub { $runs = 1;
                                     return ('a','b','c') };

  ok (streq_array ([ THREE() ],
                   [ 'a', 'b', 'c' ]),
      1,
      'THREE return values first run');
  ok ($runs, 1,
      'THREE return values first run runs code');

  $runs = 0;
  ok (streq_array([ THREE() ],
                  [ 'a', 'b', 'c' ]),
      1,
      'THREE return values second run');
  ok ($runs, 0,
      'THREE return values second run doesn\'t run code');
}

{
  my $runs = 0;
  use constant::defer THREE_SCALAR => sub { $runs = 1;
                                            return ('a','b','c') };

  my $got = THREE_SCALAR();
  ok ($got, 3,
      'three values in scalar context return values first run');
  ok ($runs, 1,
      'three values in scalar context return values first run runs code');

  $runs = 0;
  $got = THREE_SCALAR();
  ok ($got, 3,
      'three values in scalar context return values second run');
  ok ($runs, 0,
      'three values in scalar context return values second run doesn\'t run code');
}

#------------------------------------------------------------------------------
# multiple names

{
  use constant::defer
    PAIR_ONE => sub { 123 },
    PAIR_TWO => sub { 456 };
  ok (PAIR_ONE, 123, 'PAIR_ONE');
  ok (PAIR_TWO, 456, 'PAIR_TWO');
}
{
  use constant::defer { HASH_ONE => sub { 123 },
                        HASH_TWO => sub { 456 } };
  ok (HASH_ONE, 123, 'HASH_ONE');
  ok (HASH_TWO, 456, 'HASH_TWO');
}
{
  use constant::defer SHASH_ONE => sub { 123 },
                      { SHASH_TWO => sub { 456 },
                        SHASH_THREE => sub { 789 } };
  ok (SHASH_ONE, 123, 'SHASH_ONE');
  ok (SHASH_TWO, 456, 'SHASH_TWO');
  ok (SHASH_THREE, 789, 'SHASH_THREE');
}

#------------------------------------------------------------------------------
# with can()

{
  my $runs = 0;
  package MyTestCan;
  use constant::defer CANNED => sub { $runs++; return 'foo' };

  package main;
  my $func = MyTestCan->can('CANNED');

  my $got = &$func();
  ok ($got, 'foo', 'through can() - result');
  ok ($runs, 1,    'through can() - run once');

  $got = &$func();
  ok ($got, 'foo', 'through can() - 2nd result');
  ok ($runs, 1,    'through can() - 2nd still run once');
}

#------------------------------------------------------------------------------
# with Exporter import()

{
  my $runs = 0;

  package MyTestImport;
  use vars qw(@ISA @EXPORT);
  use constant::defer TEST_IMPORT_FOO => sub { $runs++; return 'foo' };
  require Exporter;
  @ISA = ('Exporter');
  @EXPORT = ('TEST_IMPORT_FOO');

  package main;
  MyTestImport->import;

  my $got = TEST_IMPORT_FOO();
  ok ($got, 'foo', 'through import - result');
  ok ($runs, 1,    'through import - run once');

  $got = TEST_IMPORT_FOO();
  ok ($got, 'foo', 'through import - 2nd result');
  ok ($runs, 1,    'through import - 2nd still run once');
}

#------------------------------------------------------------------------------
# gc of supplied $subr after it's been called

{
  my $subr;
  BEGIN {
    # crib: anon sub must refer to a lexical or isn't gc'ed, at least in 5.6.0
    my $lexical_var = 'gc me';
    $subr = sub { return $lexical_var };
  }
  use constant::defer WEAKEN_CONST => $subr;

  # including when the can() first func is retained
  # my $cancode = main->can('WEAKEN_CONST');

  my @got = WEAKEN_CONST();
  ok (streq_array (\@got,
                   ['gc me']),
      1,
      'WEAKEN_CONST - result');

  if ($have_weaken) {
    Scalar::Util::weaken ($subr);
  }
  skip ($skip_weaken,
        ! defined $subr,
        1,
        'WEAKEN_CONST - subr now undef');
}

{
  my ($objref, $subr);
  my $runs = 0;
  BEGIN {
    my %obj = (foo => 'bar');
    $subr = sub { $runs++; return %obj };
    $objref = \%obj;
  }
  use constant::defer WEAKEN_OBJRET => $subr;

  # including when the can() first func is retained
  # my $cancode = main->can('WEAKEN_OBJRET');

  my @got = WEAKEN_OBJRET();
  ok (streq_array (\@got,
                   ['foo','bar']),
      1,
      'WEAKEN_OBJRET - result');
  ok ($runs, 1, 'WEAKEN_OBJRET - run once');

  if ($have_weaken) {
    Scalar::Util::weaken ($subr);
    Scalar::Util::weaken ($objref);
  }
  skip ($skip_weaken,
        ! defined $subr,
        1,
        'WEAKEN_OBJRET - subr now undef');
  skip ($skip_weaken,
        ! defined $objref,
        1,
        'WEAKEN_OBJRET - objref now undef');
}

#------------------------------------------------------------------------------
# gc of can() after called

{
  use constant::defer CAN_GC => sub { return 'gc me' };
  my $cancode = main->can('CAN_GC');

  my @got = CAN_GC();
  ok (streq_array (\@got,
                   ['gc me']),
      1,
      'CAN_GC - result');

  if ($have_weaken) {
    Scalar::Util::weaken ($cancode);
  }
  skip ($skip_weaken,
        ! defined $cancode,
        1,
        'CAN_GC - can() coderef now undef');
}

#------------------------------------------------------------------------------
# gc of initial definition after called

{
  use constant::defer INITIAL_GC => sub { return 'gc me' };
  my $coderef = \&INITIAL_GC;

  my @got = INITIAL_GC();
  ok (streq_array (\@got,
                   ['gc me']),
      1,
      'INITIAL_GC - result');

  if ($have_weaken) {
    Scalar::Util::weaken ($coderef);
  }
  skip ($skip_weaken,
        ! defined $coderef,
        1,
        'INITIAL_GC - saved initial now undef');
}

#------------------------------------------------------------------------------
# gc of runner helper after redefine

{
  use constant::defer RUNNER_GC => sub { return 'gc me' };
  my $runner;
  BEGIN {
    $runner = $constant::defer::DEBUG_LAST_RUNNER;
    undef $constant::defer::DEBUG_LAST_RUNNER;
  }
  if (! $runner) {
    MyTestHelpers::diag ("DEBUG_LAST_RUNNER not enabled in defer.pm");
  }

  do {
    local $^W = 0; # no warnings 'redefine';
    eval '*RUNNER_GC = sub () { "new value" }; 1';
  } or die "Oops, eval error: $@";

  if ($have_weaken) {
    Scalar::Util::weaken ($runner);
  }
  skip ($skip_weaken,
        ! defined $runner,
        1,
        'RUNNER_GC - now undef');
  if ($runner) {
    require Devel::FindRef;
    MyTestHelpers::diag (Devel::FindRef::track($runner));
  }
}

exit 0;