]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blob - t/lib/Scope/Upper/TestGenerator.pm
Rework how Scope::Upper::TestGenerator generates its 'local' tests
[perl/modules/Scope-Upper.git] / t / lib / Scope / Upper / TestGenerator.pm
1 package Scope::Upper::TestGenerator;
2
3 use strict;
4 use warnings;
5
6 our ($call, $test, $allblocks);
7
8 our $local_var = '$x';
9
10 our $local_decl = sub {
11  my $x = $_[3];
12  return "local $local_var = $x;\n";
13 };
14
15 our $local_cond = sub {
16  my $x = $_[3];
17  return defined $x ? "($local_var eq $x)" : "(!defined($local_var))";
18 };
19
20 our $local_test = sub {
21  my ($height, $level, $i, $x) = @_;
22  my $cond = $local_cond->(@_);
23  return "ok($cond, 'local h=$height, l=$level, i=$i');\n";
24 };
25
26 my @blocks = (
27  [ '{',         '}' ],
28  [ 'sub {',     '}->();' ],
29  [ 'do {',      '};' ],
30  [ 'eval {',    '};' ],
31  [ 'for (1) {', '}' ],
32  [ 'eval q[',   '];' ],
33 );
34
35 sub import {
36  if ("$]" >= 5.010_001) {
37   push @blocks, [ 'given (1) {', '}' ];
38   require feature;
39   feature->import('switch');
40  }
41 }
42
43 @blocks = map [ map "$_\n", @$_ ], @blocks;
44
45 sub _block {
46  my ($height, $level, $i) = @_;
47  my $j = $height - $i;
48  $j = 0 if $j > $#blocks or $j < 0;
49  return [ map "$_\n", @{$blocks[$j]} ];
50 }
51
52 sub gen {
53  my ($height, $level, $i, $x) = @_;
54  push @_, $i = 0 if @_ == 2;
55  return $call->(@_) if $height < $i;
56  my @res;
57  my @blks = $allblocks ? @blocks : _block(@_);
58  my $up   = gen($height, $level, $i + 1, $x);
59  for my $base (@$up) {
60   for my $blk (@blks) {
61    push @res, $blk->[0] . $base . $test->(@_) . $local_test->(@_) . $blk->[1];
62   }
63  }
64  $_[3] = $i + 1;
65  $up = gen($height, $level, $i + 1, $i + 1);
66  for my $base (@$up) {
67   for my $blk (@blks) {
68    push @res, $blk->[0] .
69                $local_decl->(@_) . $base . $test->(@_) . $local_test->(@_)
70               . $blk->[1];
71   }
72  }
73  return \@res;
74 }
75
76 1;