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;';
64 [ $var, $init, $code, $exp, $use, $opts, 0 ],
65 [ $var, "use strict; $init", $code, $exp, $use, $opts, 1 ],
66 [ $var, "no strict; $init", $code, $exp, $use, $opts, 1 ],
73 $new[0] =~ s/^\$/$sigil/;
74 for ($new[1], $new[2]) {
75 s/\Q$sigil$var\E/$new[0]/g;
78 my $simple = $new[2] !~ /->/;
79 my $plain_deref = $new[2] =~ /\Q$new[0]\E/;
80 my $empty = { '@' => '[ ]', '%' => '{ }' }->{$sigil};
82 and ( $new[3] =~ m!qr/\^Reference vivification forbidden.*?/!
83 or $new[3] =~ m!qr/\^Can't vivify reference.*?/!))
85 and $new[3] =~ m!qr/\^Can't use an undefined value as a.*?/!)) {
88 $new[3] = "'', 1, $empty";
90 $new[3] =~ s/,\s*undef\s*$/, $empty/;
94 push @testcases, @extra;
96 my ($testcase, $desc) = generate(@$_);
98 diag "== This testcase failed to compile ==\n$testcase\n## Reason: $@" if $@;