1 package autovivification::TestCases;
10 *{caller().'::testcase_ok'} = \&testcase_ok;
13 sub in_strict { (caller 0)[8] & (eval { strict::bits(@_) } || 0) };
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;
24 my \$strict = autovivification::TestCases::in_strict('refs');
27 local \$SIG{__WARN__} = sub { die join '', 'warn:', \@_ };
32 like \$@, \$exp[0], \$desc . ' [exception]';
34 is \$@, \$exp[0], \$desc . ' [exception]';
36 is_deeply \$res, \$exp[1], \$desc . ' [return]';
37 is_deeply $test, \$exp[2], \$desc . ' [variable]';
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;";
51 for (split ' ', $opts) {
53 $no = 0 if s/^([-+])// and $1 eq '-';
54 $use .= ($no ? 'no' : 'use') . " autovivification '$_';"
56 } elsif (defined $opts) {
58 $use = 'no autovivification;';
63 my @base = ([ $var, $init, $code, $exp, $use ]);
65 my @oldderef = @{$base[0]};
66 $oldderef[2] =~ s/\Q$var\E\->/\$$var/g;
67 push @base, \@oldderef;
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;
75 my $simple = $nonref[2] !~ /->/;
76 my $plain_deref = $nonref[2] =~ /\Q$nonref[0]\E/;
77 my $empty = { '@' => '[ ]', '%' => '{ }' }->{$sigil};
79 and ( $nonref[3] =~ m!qr/\^Reference vivification forbidden.*?/!
80 or $nonref[3] =~ m!qr/\^Can't vivify reference.*?/!))
82 and $nonref[3] =~ m!qr/\^Can't use an undefined value as a.*?/!)) {
85 $nonref[3] = "'', 1, $empty";
87 $nonref[3] =~ s/,\s*undef\s*$/, $empty/;
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 ],
97 my ($testcase, $desc) = generate(@$_);
100 diag "== This testcase failed to compile ==\n$testcase\n## Reason: $@" if $@;