]> git.vpit.fr Git - perl/modules/autovivification.git/blob - t/lib/autovivification/TestCases.pm
99d6672a2984d6cc51494108e7998371cd2da4a1
[perl/modules/autovivification.git] / t / lib / autovivification / TestCases.pm
1 package autovivification::TestCases;
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 sub import {
9  no strict 'refs';
10  *{caller().'::testcase_ok'} = \&testcase_ok;
11 }
12
13 sub in_strict { (caller 0)[8] & (eval { strict::bits(@_) } || 0) };
14
15 sub generate {
16  my ($var, $init, $code, $exp, $use, $opts, $global) = @_;
17  my $decl = $global ? "our $var; local $var;" : "my $var;";
18  my $test = $var =~ /^[@%]/ ? "\\$var" : $var;
19  my $desc = join('; ', map { my $x = $_; $x=~ s,;\s*$,,; $x }
20                                    grep /\S/, $decl, $init, $code) . " <$opts>";
21  return <<TESTCASE, $desc;
22 $decl
23 $init
24 my \$strict = autovivification::TestCases::in_strict('refs');
25 my \@exp = ($exp);
26 my \$res = eval {
27  local \$SIG{__WARN__} = sub { die join '', 'warn:', \@_ };
28  $use
29  $code
30 };
31 if (ref \$exp[0]) {
32  like \$@, \$exp[0], \$desc . ' [exception]';
33 } else {
34  is   \$@, \$exp[0], \$desc . ' [exception]';
35 }
36 is_deeply \$res, \$exp[1], \$desc . ' [return]';
37 is_deeply $test, \$exp[2], \$desc . ' [variable]';
38 TESTCASE
39 }
40
41 sub testcase_ok {
42  local $_  = shift;
43  my $sigil = shift;
44  my @chunks = split /#+/, "$_ ";
45  s/^\s+//, s/\s+$// for @chunks;
46  my ($init, $code, $exp, $opts) = @chunks;
47  (my $var = $init) =~ s/[^\$@%\w].*//;
48  $init = $var eq $init ? '' : "$init;";
49  my $use;
50  if ($opts) {
51   for (split ' ', $opts) {
52    my $no = 1;
53    $no = 0 if s/^([-+])// and $1 eq '-';
54    $use .= ($no ? 'no' : 'use') . " autovivification '$_';"
55   }
56  } elsif (defined $opts) {
57   $opts = 'empty';
58   $use  = 'no autovivification;';
59  } else {
60   $opts = 'default';
61   $use  = '';
62  }
63  my @base = ([ $var, $init, $code, $exp, $use ]);
64  if ($var =~ /\$/) {
65   my @oldderef = @{$base[0]};
66   $oldderef[2] =~ s/\Q$var\E\->/\$$var/g;
67   push @base, \@oldderef;
68
69   my @nonref = @{$base[0]};
70   $nonref[0] =~ s/^\$/$sigil/;
71   for ($nonref[1], $nonref[2]) {
72    s/\Q$sigil$var\E/$nonref[0]/g;
73    s/\Q$var\E\->/$var/g;
74   }
75   my $simple      = $nonref[2] !~ /->/;
76   my $plain_deref = $nonref[2] =~ /\Q$nonref[0]\E/;
77   my $empty  = { '@' => '[ ]', '%' => '{ }' }->{$sigil};
78   if (($simple
79        and (   $nonref[3] =~ m!qr/\^Reference vivification forbidden.*?/!
80             or $nonref[3] =~ m!qr/\^Can't vivify reference.*?/!))
81   or ($plain_deref
82        and $nonref[3] =~ m!qr/\^Can't use an undefined value as a.*?/!)) {
83    $nonref[1] = '';
84    $nonref[2] = 1;
85    $nonref[3] = "'', 1, $empty";
86   }
87   $nonref[3] =~ s/,\s*undef\s*$/, $empty/;
88   push @base, \@nonref;
89  }
90  my @testcases = map {
91   my ($var, $init, $code, $exp, $use) = @$_;
92   [ $var, $init,               $code, $exp, $use, $opts, 0 ],
93   [ $var, "use strict; $init", $code, $exp, $use, $opts, 1 ],
94   [ $var, "no strict;  $init", $code, $exp, $use, $opts, 1 ],
95  } @base;
96  for (@testcases) {
97   my ($testcase, $desc) = generate(@$_);
98   my @N = (0 .. 9);
99   eval $testcase;
100   diag "== This testcase failed to compile ==\n$testcase\n## Reason: $@" if $@;
101  }
102 }
103
104 1;