]> git.vpit.fr Git - perl/modules/autovivification.git/blobdiff - t/20-hash.t
Remove trailing whitespace
[perl/modules/autovivification.git] / t / 20-hash.t
index 867152a2f84dbd82f71618140938d3df47c3452f..a3768397c97d2c1cacf41543d5837c74dfef3373 100644 (file)
@@ -3,90 +3,33 @@
 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 <<TESTCASE;
-my \@exp = ($exp);
-$decl
-$init
-my \$res = eval {
- local \$SIG{__WARN__} = sub { die join '', 'warn:', \@_ };
- $use
- $code
-};
-if (ref \$exp[0]) {
- like \$@, \$exp[0], \$desc . ' [exception]';
-} else {
- is   \$@, \$exp[0], \$desc . ' [exception]';
-}
-is_deeply \$res, \$exp[1], \$desc . ' [return]';
-is_deeply $test, \$exp[2], \$desc . ' [variable]';
-TESTCASE
-}
+use lib 't/lib';
+use Test::Leaner tests => 9 * 3 * 302;
+
+use autovivification::TestCases;
 
 while (<DATA>) {
  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__
 
 --- fetch ---
 
-$x # $x->{a} # '', undef, { } 
+$x # $x->{a} # '', undef, { }
 $x # $x->{a} # '', undef, undef #
 $x # $x->{a} # '', undef, undef # +fetch
 $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
@@ -151,18 +94,18 @@ $x->{a}->{b} = 1 # $x->{c}->{d} # '', undef, { a => { b => 1 }, c => { } }
 --- aliasing ---
 
 $x # 1 for $x->{a}; () # '', undef, { a => undef }
-$x # 1 for $x->{a}; () # '', undef, undef          #
-$x # 1 for $x->{a}; () # '', undef, undef          # +fetch
+$x # 1 for $x->{a}; () # '', undef, { a => undef } #
+$x # 1 for $x->{a}; () # '', undef, { a => undef } # +fetch
 $x # 1 for $x->{a}; () # '', undef, { a => undef } # +exists
 $x # 1 for $x->{a}; () # '', undef, { a => undef } # +delete
-$x # 1 for $x->{a}; () # '', undef, { a => undef } # +store
+$x # 1 for $x->{a}; () # qr/^Can't vivify reference/, undef, undef # +store
 
 $x # $_ = 1 for $x->{a}; () # '', undef, { a => 1 }
-$x # $_ = 1 for $x->{a}; () # '', undef, undef      #
-$x # $_ = 1 for $x->{a}; () # '', undef, undef      # +fetch
+$x # $_ = 1 for $x->{a}; () # '', undef, { a => 1 } #
+$x # $_ = 1 for $x->{a}; () # '', undef, { a => 1 } # +fetch
 $x # $_ = 1 for $x->{a}; () # '', undef, { a => 1 } # +exists
 $x # $_ = 1 for $x->{a}; () # '', undef, { a => 1 } # +delete
-$x # $_ = 1 for $x->{a}; () # '', undef, { a => 1 } # +store
+$x # $_ = 1 for $x->{a}; () # qr/^Can't vivify reference/, undef, undef # +store
 
 $x->{a} = 1 # 1 for $x->{a}; () # '', undef, { a => 1 }             # +fetch
 $x->{a} = 1 # 1 for $x->{b}; () # '', undef, { a => 1, b => undef } # +fetch
@@ -173,6 +116,56 @@ $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
 
+$x # do_nothing($x->{a}); () # '', undef, { }
+$x # do_nothing($x->{a}); () # '', undef, { } #
+$x # do_nothing($x->{a}); () # '', undef, { } # +fetch
+$x # do_nothing($x->{a}); () # '', undef, { } # +exists
+$x # do_nothing($x->{a}); () # '', undef, { } # +delete
+$x # do_nothing($x->{a}); () # qr/^Can't vivify reference/, undef, undef # +store
+
+$x # set_arg($x->{a}); () # '', undef, { a => 1 }
+$x # set_arg($x->{a}); () # '', undef, { a => 1 } #
+$x # set_arg($x->{a}); () # '', undef, { a => 1 } # +fetch
+$x # set_arg($x->{a}); () # '', undef, { a => 1 } # +exists
+$x # set_arg($x->{a}); () # '', undef, { a => 1 } # +delete
+$x # set_arg($x->{a}); () # qr/^Can't vivify reference/, undef, 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
+
+--- slice ---
+
+$x # my @a = @$x{'a', 'b'}; \@a # '', [ undef, undef ], { }
+$x # my @a = @$x{'a', 'b'}; \@a # '', [ undef, undef ], undef #
+$x # my @a = @$x{'a', 'b'}; \@a # '', [ undef, undef ], undef # +fetch
+$x # my @a = @$x{'a', 'b'}; \@a # '', [ undef, undef ], { }   # +exists
+$x # my @a = @$x{'a', 'b'}; \@a # '', [ undef, undef ], { }   # +delete
+$x # my @a = @$x{'a', 'b'}; \@a # '', [ undef, undef ], { }   # +store
+
+$x->{b} = 0 # my @a = @$x{'a', 'b'}; \@a # '', [ undef, 0 ], { b => 0 } # +fetch
+
+$x # @$x{'a', 'b'} = (1, 2); () # '', undef, { a => 1, b => 2 }
+$x # @$x{'a', 'b'} = (1, 2); () # '', undef, { a => 1, b => 2 } #
+$x # @$x{'a', 'b'} = (1, 2); () # '', undef, { a => 1, b => 2 } # +fetch
+$x # @$x{'a', 'b'} = (1, 2); () # '', undef, { a => 1, b => 2 } # +exists
+$x # @$x{'a', 'b'} = (1, 2); () # '', undef, { a => 1, b => 2 } # +delete
+$x # @$x{'a', 'b'} = (1, 2); () # qr/^Can't vivify reference/, undef, undef # +store
+
+$x->{a} = 0              # @$x{'a', 'b'} = (1, 2); () # '', undef, { a => 1, b => 2 } # +store
+$x->{c} = 0              # @$x{'a', 'b'} = (1, 2); () # '', undef, { a => 1, b => 2, c => 0 } # +store
+$x->{a} = 0, $x->{b} = 0 # @$x{'a', 'b'} = (1, 2); () # '', undef, { a => 1, b => 2 } # +store
+
 --- exists ---
 
 $x # exists $x->{a} # '', '', { }