X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F20-hash.t;h=e7a1703e9508e5b7b4f89bf4c77b630ef5e2d9c7;hb=0c717cedff4b08d709037ba6449abd1b517a2466;hp=867152a2f84dbd82f71618140938d3df47c3452f;hpb=ddbfd527f4c54458985145aae3a837a8f5868551;p=perl%2Fmodules%2Fautovivification.git diff --git a/t/20-hash.t b/t/20-hash.t index 867152a..e7a1703 100644 --- a/t/20-hash.t +++ b/t/20-hash.t @@ -3,77 +3,15 @@ use strict; use warnings; -use Test::More tests => 6 * 3 * 260; - -sub testcase { - my ($var, $init, $code, $exp, $use, $global) = @_; - my $decl = $global ? "our $var; local $var;" : "my $var;"; - my $test = $var =~ /^[@%]/ ? "\\$var" : $var; - return < 6 * 3 * 274; + +use lib 't/lib'; +use autovivification::TestCases; while () { 1 while chomp; next unless /#/; - my @chunks = split /#+/, "$_ "; - s/^\s+//, s/\s+$// for @chunks; - my ($init, $code, $exp, $opts) = @chunks; - (my $var = $init) =~ s/[^\$@%\w].*//; - $init = $var eq $init ? '' : "$init;"; - my $use; - if ($opts) { - for (split ' ', $opts) { - my $no = 1; - $no = 0 if s/^([-+])// and $1 eq '-'; - $use .= ($no ? 'no' : 'use') . " autovivification '$_';" - } - } elsif (defined $opts) { - $opts = 'empty'; - $use = 'no autovivification;'; - } else { - $opts = 'default'; - $use = ''; - } - my @testcases = ( - [ $var, $init, $code, $exp, $use, 0 ], - [ $var, "use strict; $init", $code, $exp, $use, 1 ], - [ $var, "no strict; $init", $code, $exp, $use, 1 ], - ); - my @extra; - for (@testcases) { - my $var = $_->[0]; - if ($var =~ /\$/) { - my @new = @$_; - $new[0] =~ s/^$/%/; - $new[1] =~ s/$var\->/$var/g; - $new[2] =~ s/$var\->/$var/g; - push @extra, \@new; - } - } - push @testcases, @extra; - for (@testcases) { - my $testcase = testcase(@$_); - my ($var, $init, $code) = @$_; - my $desc = do { (my $x = "$var | $init") =~ s,;\s+$,,; $x } . " | $code | $opts"; - eval $testcase; - diag "== This testcase failed to compile ==\n$testcase\n## Reason: $@" if $@; - } + testcase_ok($_, '%'); } __DATA__ @@ -87,6 +25,11 @@ $x # $x->{a} # '', undef, { } # +exists $x # $x->{a} # '', undef, { } # +delete $x # $x->{a} # '', undef, { } # +store +$x # $x->{a} # '', undef, { } # -fetch +$x # $x->{a} # '', undef, { } # +fetch -fetch +$x # $x->{a} # '', undef, undef # -fetch +fetch +$x # $x->{a} # '', undef, undef # +fetch -exists + $x # $x->{a} # qr/^Reference vivification forbidden/, undef, undef # +strict +fetch $x # $x->{a} # '', undef, { } # +strict +exists $x # $x->{a} # '', undef, { } # +strict +delete @@ -173,6 +116,20 @@ $x->{a} = 1 # 1 for $x->{b}; () # '', undef, { a => 1, b => undef } # +delete $x->{a} = 1 # 1 for $x->{a}; () # '', undef, { a => 1 } # +store $x->{a} = 1 # 1 for $x->{b}; () # '', undef, { a => 1, b => undef } # +store +--- dereferencing --- + +$x # no warnings 'uninitialized'; my @a = %$x; () # ($strict ? qr/^Can't use an undefined value as a HASH reference/ : ''), undef, undef +$x # no warnings 'uninitialized'; my @a = %$x; () # ($strict ? qr/^Can't use an undefined value as a HASH reference/ : ''), undef, undef # +$x # no warnings 'uninitialized'; my @a = %$x; () # ($strict ? qr/^Can't use an undefined value as a HASH reference/ : ''), undef, undef # +fetch +$x # no warnings 'uninitialized'; my @a = %$x; () # ($strict ? qr/^Can't use an undefined value as a HASH reference/ : ''), undef, undef # +exists +$x # no warnings 'uninitialized'; my @a = %$x; () # ($strict ? qr/^Can't use an undefined value as a HASH reference/ : ''), undef, undef # +delete +$x # no warnings 'uninitialized'; my @a = %$x; () # ($strict ? qr/^Can't use an undefined value as a HASH reference/ : ''), undef, undef # +store + +$x->{a} = 1 # my @a = %$x; () # '', undef, { a => 1 } # +fetch +$x->{a} = 1 # my @a = %$x; () # '', undef, { a => 1 } # +exists +$x->{a} = 1 # my @a = %$x; () # '', undef, { a => 1 } # +delete +$x->{a} = 1 # my @a = %$x; () # '', undef, { a => 1 } # +store + --- exists --- $x # exists $x->{a} # '', '', { }