Test-More / test-more

Test2, Test::More, Test::Simple and Test::Builder Perl modules for writing tests
Other
140 stars 87 forks source link

Refcount assertions #943

Open leonerd opened 2 years ago

leonerd commented 2 years ago

It'd be great if there were some test assertions for refcounts. A copy of those in Test::Refcount would be lovely:

{
  my $arr = [];
  is_oneref( $arr, "Array has one reference initially" );

  my $acopy = $arr;
  is_refcount( $arr, 2, "Array has two references now" );

  undef $acopy;
  is_oneref( $arr, "Array is back to one reference now" );
}

For docs you can just copy the ones out of Test::Refcount if required:

=head2 is_refcount

  is_refcount( $object, $count, $name )

Test that $object has $count references to it.

=head2 is_oneref

  is_oneref( $object, $name )

Assert that the $object has only 1 reference to it.

=cut
leonerd commented 2 years ago

For an extra superspecial bonus I'd love it to also have the automatic integration with Devel::MAT::Dumper - you can just steal this code:

https://metacpan.org/dist/Test-Refcount/source/lib/Test/Refcount.pm#L121-132

wolfsage commented 2 years ago

https://github.com/wolfsage/scratchpg has a conversion of the .pm file and one of the tests to show how it could be done. I could maybe do more later to make it into a Test2::Suite addon...