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]';
45 my @chunks = split /#+/, "$_ ";
46 s/^\s+//, s/\s+$// for @chunks;
47 my ($init, $code, $exp, $opts) = @chunks;
49 (my $var = $init) =~ s/[^\$@%\w].*//;
50 $init = $var eq $init ? '' : "$init;";
53 for (split ' ', $opts) {
55 $no = 0 if s/^([-+])// and $1 eq '-';
56 $use .= ($no ? 'no' : 'use') . " autovivification '$_';"
58 } elsif (defined $opts) {
60 $use = 'no autovivification;';
66 my @base = ([ $var, $init, $code, $exp, $use ]);
68 my ($name) = $var =~ /^\$(.*)/;
70 my @oldderef = @{$base[0]};
71 $oldderef[2] =~ s/\Q$var\E\->/\$$var/g;
72 push @base, \@oldderef;
74 my @nonref = @{$base[0]};
75 $nonref[0] = $sigil . $name;
76 for ($nonref[1], $nonref[2]) {
77 s/\@\Q$var\E([\[\{])/\@$name$1/g;
78 s/\Q$sigil$var\E/$nonref[0]/g;
81 my $simple = $nonref[2] !~ /->/;
82 my $plain_deref = $nonref[2] =~ /\Q$nonref[0]\E/;
83 my $empty = { '@' => '[ ]', '%' => '{ }' }->{$sigil};
85 and ( $nonref[3] =~ m!qr/\^Reference vivification forbidden.*?/!
86 or $nonref[3] =~ m!qr/\^Can't vivify reference.*?/!))
88 and $nonref[3] =~ m!qr/\^Can't use an undefined value as a.*?/!)) {
91 $nonref[3] = "'', 1, $empty";
93 $nonref[3] =~ s/,\s*undef\s*$/, $empty/;
98 my ($var, $init, $code, $exp, $use) = @$_;
99 [ $var, $init, $code, $exp, $use, $opts, 0 ],
100 [ $var, "use strict; $init", $code, $exp, $use, $opts, 1 ],
101 [ $var, "no strict; $init", $code, $exp, $use, $opts, 1 ],
105 my ($testcase, $desc) = generate(@$_);
108 diag "== This testcase failed to compile ==\n$testcase\n## Reason: $@" if $@;