diff options
author | Michiel Beijen <mb@x14.nl> | 2021-12-26 11:31:39 +0100 |
---|---|---|
committer | James E Keenan <jkeenan@cpan.org> | 2022-01-04 12:17:07 -0500 |
commit | 676e7ef85e9ea1eb0f5e591ad8e1162bda7304de (patch) | |
tree | 01a7965e975e41b5a7acd588001528644101d559 | |
parent | 6a329fc304ab75d0ad398713e902226783157ac7 (diff) | |
download | perl-676e7ef85e9ea1eb0f5e591ad8e1162bda7304de.tar.gz |
Hash::Util::FieldHash: improve test suite
- do not count tests:
- use done_testing
- put tests requiring threading in their own subtests so we can
skip_all
- smaller improvements: use strict & warnings everywhere, tabs >>
spaces
-rw-r--r-- | ext/Hash-Util-FieldHash/lib/Hash/Util/FieldHash.pm | 2 | ||||
-rw-r--r-- | ext/Hash-Util-FieldHash/t/01_load.t | 7 | ||||
-rw-r--r-- | ext/Hash-Util-FieldHash/t/02_function.t | 41 | ||||
-rw-r--r-- | ext/Hash-Util-FieldHash/t/03_class.t | 21 | ||||
-rw-r--r-- | ext/Hash-Util-FieldHash/t/04_thread.t | 26 | ||||
-rw-r--r-- | ext/Hash-Util-FieldHash/t/05_perlhook.t | 18 | ||||
-rw-r--r-- | ext/Hash-Util-FieldHash/t/11_hashassign.t | 18 | ||||
-rw-r--r-- | ext/Hash-Util-FieldHash/t/12_hashwarn.t | 8 |
8 files changed, 52 insertions, 89 deletions
diff --git a/ext/Hash-Util-FieldHash/lib/Hash/Util/FieldHash.pm b/ext/Hash-Util-FieldHash/lib/Hash/Util/FieldHash.pm index ab915283b7..d967fb750f 100644 --- a/ext/Hash-Util-FieldHash/lib/Hash/Util/FieldHash.pm +++ b/ext/Hash-Util-FieldHash/lib/Hash/Util/FieldHash.pm @@ -4,7 +4,7 @@ use strict; use warnings; use builtin qw(reftype); -our $VERSION = '1.24'; +our $VERSION = '1.25'; use Exporter 'import'; our %EXPORT_TAGS = ( diff --git a/ext/Hash-Util-FieldHash/t/01_load.t b/ext/Hash-Util-FieldHash/t/01_load.t index ff9354cb1b..a8e6c15e89 100644 --- a/ext/Hash-Util-FieldHash/t/01_load.t +++ b/ext/Hash-Util-FieldHash/t/01_load.t @@ -1,7 +1,7 @@ -#!perl -use strict; use warnings; +use strict; +use warnings; -use Test::More tests => 8; +use Test::More; # see that Hash::Util::FieldHash and Hash::Util load and export what # they should @@ -47,3 +47,4 @@ BEGIN { ); } +done_testing; diff --git a/ext/Hash-Util-FieldHash/t/02_function.t b/ext/Hash-Util-FieldHash/t/02_function.t index b4294ddadc..1e56a5255f 100644 --- a/ext/Hash-Util-FieldHash/t/02_function.t +++ b/ext/Hash-Util-FieldHash/t/02_function.t @@ -1,12 +1,8 @@ -#!perl - use strict; use warnings; use builtin qw(refaddr); use Test::More; -my $n_tests = 0; - use Hash::Util::FieldHash qw( :all); my $ob_reg = Hash::Util::FieldHash::_ob_reg; @@ -15,15 +11,11 @@ my $ob_reg = Hash::Util::FieldHash::_ob_reg; my $fieldhash_mode = 2; # define ref types to use with some tests -my @test_types; -BEGIN { - # skipping CODE refs, they are differently scoped - @test_types = qw( SCALAR ARRAY HASH GLOB); -} +# skipping CODE refs, they are differently scoped +my @test_types = qw(SCALAR ARRAY HASH GLOB); ### The id() function { - BEGIN { $n_tests += 4 } my $ref = []; is id( $ref), refaddr( $ref), "id is refaddr"; my %h; @@ -39,7 +31,6 @@ BEGIN { ### idhash functionality { - BEGIN { $n_tests += 3 } Hash::Util::FieldHash::idhash my %h; my $ref = sub {}; my $val = 123; @@ -52,7 +43,6 @@ BEGIN { ### the register() and id_2obj functions { - BEGIN { $n_tests += 9 } my $obj = {}; my $id = id( $obj); is id_2obj( $id), undef, "unregistered object not retrieved"; @@ -69,12 +59,9 @@ BEGIN { is scalar keys %$ob_reg, 0, "object registry empty again"; eval { register( 1234) }; like $@, qr/^Attempt to register/, "registering non-ref is fatal"; - } ### Object auto-registry - -BEGIN { $n_tests += 3 } { { my $obj = {}; @@ -91,7 +78,6 @@ BEGIN { $n_tests += 3 } } ### existence/retrieval/deletion -BEGIN { $n_tests += 6 } { no warnings 'misc'; my $val = 123; @@ -107,7 +93,6 @@ BEGIN { $n_tests += 6 } } ### id-action (stringification independent of bless) -BEGIN { $n_tests += 5 } { my( %f, %g, %h, %i); Hash::Util::FieldHash::_fieldhash \ %f, $fieldhash_mode; @@ -127,10 +112,8 @@ BEGIN { $n_tests += 5 } bless $key; isnt( $h{ $key}, $val, "no access through blessed"); } - -# Garbage collection -BEGIN { $n_tests += 1 + 2*( 3*@test_types + 5) + 1 + 2 } +# Garbage collection { my %h; Hash::Util::FieldHash::_fieldhash \ %h, $fieldhash_mode; @@ -151,7 +134,7 @@ for my $preload ( [], [ map {}, 1 .. 3] ) { $f{ $ref} = $type; my ( $val) = grep $_ eq $type, values %f; is( $val, $type, "$type visible$pre"); - is( + is( keys %$ob_reg, 1 + @$preload, "$type obj registered$pre" @@ -159,7 +142,7 @@ for my $preload ( [], [ map {}, 1 .. 3] ) { } is( keys %f, @$preload, "$type gone$pre"); } - + # Garbage collection collectively is( keys %$ob_reg, @$preload, "no objs remaining$pre"); { @@ -191,9 +174,8 @@ is( keys %$ob_reg, 0, "preload gone after loop"); undef $ref; is keys %h, 0, "autovivified key collected"; } - + # big key sets -BEGIN { $n_tests += 8 } { my $size = 10_000; my %f; @@ -214,7 +196,7 @@ BEGIN { $n_tests += 8 } 0, "many objects singly unregistered", ); - + { my @refs = map [], 1 .. $size; @f{ @refs } = ( 1) x @refs; @@ -234,7 +216,6 @@ BEGIN { $n_tests += 8 } } # many field hashes -BEGIN { $n_tests += 6 } { my $n_fields = 1000; my @fields = map {}, $n_fields; @@ -259,7 +240,6 @@ BEGIN { $n_tests += 6 } # direct hash assignment -BEGIN { $n_tests += 4 } { Hash::Util::FieldHash::_fieldhash( $_, $fieldhash_mode) for \ my( %f, %g, %h); my $size = 6; @@ -293,19 +273,14 @@ BEGIN { $n_tests += 4 } is prototype( "Hash::Util::FieldHash::$_") || '', $proto_tab{ $_}, "$_ has prototype ($proto_tab{ $_})" for @Hash::Util::FieldHash::EXPORT_OK; - - BEGIN { $n_tests += 1 + @Hash::Util::FieldHash::EXPORT_OK } } { - BEGIN { $n_tests += 1 } Hash::Util::FieldHash::_fieldhash \ my( %h), $fieldhash_mode; bless \ %h, 'abc'; # this bus-errors with a certain bug ok( 1, "no bus error on bless") } -BEGIN { plan tests => $n_tests } - ####################################################################### use Symbol qw( gensym); @@ -321,3 +296,5 @@ BEGIN { sub gen_ref { $gen{ shift()}->() } } + +done_testing; diff --git a/ext/Hash-Util-FieldHash/t/03_class.t b/ext/Hash-Util-FieldHash/t/03_class.t index 1c35040df2..c350489fd9 100644 --- a/ext/Hash-Util-FieldHash/t/03_class.t +++ b/ext/Hash-Util-FieldHash/t/03_class.t @@ -1,10 +1,8 @@ -#!perl -use strict; use warnings; +use strict; +use warnings; use Test::More; -my $n_tests = 0; - use Config; -BEGIN { $n_tests += 2 } + { my $p = Impostor->new( 'Donald Duck'); is( $p->greeting, "Hi, I'm Donald Duck", "blank title"); @@ -13,9 +11,9 @@ BEGIN { $n_tests += 2 } } # thread support? -BEGIN { $n_tests += 5 } -SKIP: { - skip "No thread support", 5 unless $Config{ usethreads}; +subtest 'threads' => sub { + plan skip_all => "No thread support" unless $Config{usethreads}; + require threads; treads->import if threads->can( 'import'); @@ -42,9 +40,8 @@ SKIP: { } )->join; is( $ans, "Hi, I'm Mr Donald Duck", "double thread: got greeting"); -} +}; -BEGIN { plan tests => $n_tests } ############################################################################ @@ -100,7 +97,7 @@ BEGIN { } sub title { $assumed_title{ shift()} } - + sub assume_title { my $p = shift; $assumed_title{ $p} = shift || ''; @@ -108,3 +105,5 @@ BEGIN { } } } + +done_testing; diff --git a/ext/Hash-Util-FieldHash/t/04_thread.t b/ext/Hash-Util-FieldHash/t/04_thread.t index d9e6894496..fb85c052a0 100644 --- a/ext/Hash-Util-FieldHash/t/04_thread.t +++ b/ext/Hash-Util-FieldHash/t/04_thread.t @@ -1,29 +1,24 @@ -#!perl -use strict; use warnings; +use strict; +use warnings; use Test::More; -my $n_tests; +use Config; use Hash::Util::FieldHash qw( :all); + my $ob_reg = Hash::Util::FieldHash::_ob_reg; { - my $n_basic; - BEGIN { - $n_basic = 6; # 6 tests per call of basic_func() - $n_tests += 5*$n_basic; - } my %h; fieldhash %h; sub basic_func { my $level = shift; - my @res; my $push_is = sub { my ( $hash, $should, $name) = @_; push @res, [ scalar keys %$hash, $should, $name]; }; - + my $obj = []; $push_is->( \ %h, 0, "$level: initially clear"); $push_is->( $ob_reg, 0, "$level: ob_reg initially clear"); @@ -38,10 +33,9 @@ my $ob_reg = Hash::Util::FieldHash::_ob_reg; &is( @$_) for basic_func( "home"); - SKIP: { - require Config; - skip "No thread support", 3*$n_basic unless - $Config::Config{ usethreads}; + subtest 'threads' => sub { + plan skip_all => "No thread support" unless $Config{usethreads}; + require threads; my ( $t) = threads->create( \ &basic_func, "thread 1"); &is( @$_) for $t->join; @@ -53,10 +47,10 @@ my $ob_reg = Hash::Util::FieldHash::_ob_reg; $t->join; }); &is( @$_) for $t->join; - } + }; &is( @$_) for basic_func( "back home again"); } -BEGIN { plan tests => $n_tests } +done_testing(); diff --git a/ext/Hash-Util-FieldHash/t/05_perlhook.t b/ext/Hash-Util-FieldHash/t/05_perlhook.t index 7b5990998d..2c03ba251e 100644 --- a/ext/Hash-Util-FieldHash/t/05_perlhook.t +++ b/ext/Hash-Util-FieldHash/t/05_perlhook.t @@ -1,7 +1,6 @@ -#!perl -use strict; use warnings; +use strict; +use warnings; use Test::More; -my $n_tests; use Hash::Util::FieldHash; use builtin qw(weaken); @@ -38,7 +37,7 @@ sub numbers_first { # Sort helper: All digit entries sort in front of others weaken $magref; is( $counter, 1, "weaken doesn't trigger magic"); - + { my $x = $magref } is( $counter, 1, "read doesn't trigger magic"); @@ -54,13 +53,11 @@ sub numbers_first { # Sort helper: All digit entries sort in front of others $magref = my $other_ref = []; is( $counter, 2, "overwrite triggers"); - + undef $ref; is( $counter, 2, "ref expiry doesn't trigger after overwrite"); is( $magref, $other_ref, "weak ref doesn't kill overwritten value"); - - BEGIN { $n_tests += 10 } } # magical hash (patches to mg.c and hv.c) @@ -175,7 +172,7 @@ sub numbers_first { # Sort helper: All digit entries sort in front of others () = values %i; $x = each %i; () = each %i; - + is( $counter, 0, "normal set magic never triggers"); bless \ %i, 'abc'; @@ -206,9 +203,6 @@ sub numbers_first { # Sort helper: All digit entries sort in front of others bless \ %j, 'abc'; is( $counter, 1, "...except for bless"); - - BEGIN { $n_tests += 43 } } -BEGIN { plan tests => $n_tests } - +done_testing; diff --git a/ext/Hash-Util-FieldHash/t/11_hashassign.t b/ext/Hash-Util-FieldHash/t/11_hashassign.t index f807146391..3fd7067535 100644 --- a/ext/Hash-Util-FieldHash/t/11_hashassign.t +++ b/ext/Hash-Util-FieldHash/t/11_hashassign.t @@ -1,12 +1,10 @@ -#!./perl -w -use Test::More; +use strict; +use warnings; -# use strict; +use Test::More; use Hash::Util::FieldHash qw( :all); no warnings 'misc'; -plan tests => 215; - my @comma = ("key", "value"); # The peephole optimiser already knows that it should convert the string in @@ -26,7 +24,7 @@ my $key = 'ey'; is ($comma{"k" . $key}, "value", 'is key present? (unoptimised)'); # now with cunning: is ($comma{key}, "value", 'is key present? (maybe optimised)'); -#tokeniser may treat => differently. +# tokeniser may treat => differently. my @temp = (key=>undef); is ($comma{$temp[0]}, "value", 'is key present? (using LHS of =>)'); @@ -281,11 +279,11 @@ foreach my $chr (60, 200, 600, 6000, 60000) { my %h; my $x; my $ar; fieldhash %h; is( (join ':', %h = (1) x 8), '1:1', - 'hash assignment in list context removes duplicates' ); + 'hash assignment in list context removes duplicates' ); is( scalar( %h = (1,2,1,3,1,4,1,5) ), 8, - 'hash assignment in scalar context' ); + 'hash assignment in scalar context' ); is( scalar( ($x,%h) = (0,1,2,1,3,1,4,1,5) ), 9, - 'scalar + hash assignment in scalar context' ); + 'scalar + hash assignment in scalar context' ); $ar = [ %h = (1,2,1,3,1,4,1,5) ]; is( $#$ar, 1, 'hash assignment in list context' ); is( "@$ar", "1 5", '...gets the last values' ); @@ -311,3 +309,5 @@ foreach my $chr (60, 200, 600, 6000, 60000) { @expect{map "$_", @refs} = @types; ok (!eq_hash(\%h, \%expect), 'blessed ref stringification different'); } + +done_testing; diff --git a/ext/Hash-Util-FieldHash/t/12_hashwarn.t b/ext/Hash-Util-FieldHash/t/12_hashwarn.t index 3fd6bfd2dd..c2b7ac2514 100644 --- a/ext/Hash-Util-FieldHash/t/12_hashwarn.t +++ b/ext/Hash-Util-FieldHash/t/12_hashwarn.t @@ -1,10 +1,6 @@ -#!./perl -use Test::More; - -plan( tests => 12 ); - use strict; use warnings; +use Test::More; use Hash::Util::FieldHash qw( :all); our @warnings; @@ -51,3 +47,5 @@ my $fail_not_hr = 'Not a HASH reference at '; cmp_ok(scalar(@warnings),'==',0,'hashref assign'); } + +done_testing; |