The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test::NaiveStubs - Generate test stubs for methods and functions

VERSION

version 0.0703

SYNOPSIS

  #use Foo::Bar; # <- Uncomment to load and process the module
  use Test::NaiveStubs;

  my $t = Test::NaiveStubs->new(
    module => 'Foo::Bar',
    name   => 't/foo-bar.t',
  );

  $t->gather_subs;
  print Dumper $t->subs;

  $t->create_test;

  # Or on the command-line:

  # perl -I/path/to/Foo-Bar/lib eg/stub Foo::Bar

  # perl -MFoo::Bar -MTest::NaiveStubs -E \
  #   '$t = Test::NaiveStubs->new(module => "Foo::Bar"); $t->create_test'

  # perl -MData::Dumper -MFoo::Bar -MTest::NaiveStubs -E \
  #   '$t = Test::NaiveStubs->new(module => "Foo::Bar"); $t->gather_subs; say Dumper $t->subs'

DESCRIPTION

Test::NaiveStubs generates a test file of stubs exercising all the methods or functions of the given module.

For a more powerful alternative, check out Test::StubGenerator.

ATTRIBUTES

module

  $module = $t->module;

The module name to use in the test generation. This is a required attribute.

name

  $name = $t->name;

The test output file name. If not given in the constructor, the filename is created from the module. So Foo::Bar would be converted to foo-bar.t.

subs

  $subs = $t->subs;

The subroutines in the given module. This is a computed attribute and as such, constructor arguments will be ignored.

is_oo

The subroutines contain new and thus object methods are produced by the unit_test method. This is a computed attribute and as such, constructor arguments will be ignored.

METHODS

new()

  $t = Test::NaiveStubs->new(%arguments);

Create a new Test::NaiveStubs object.

gather_subs()

  $t->gather_subs;

Set the subs attribute to the subroutines of the given module (as well as imported methods) as a hash reference.

unit_test()

  $test = $t->unit_test($method);

Return the text of a unit test as described below in create_test.

create_test()

  $t->create_test;

Create a test file with unit tests for each method.

A new method is extracted and processed first with use_ok, object instantiation, followed by isa_ok. Then each seen method is returned as an ok can("method") assertion. If no new method is present, an ok with the subroutine is produced.

SEE ALSO

The eg/stub example program and the t/01-methods.t test in this distribution

Moo

Package::Stash

Sub::Identify

THANK YOU

For helping me understand how to gather the subroutines:

Dan Book (DBOOK)

Matt S Trout (MSTROUT)

AUTHOR

Gene Boggs <gene@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2022 by Gene Boggs.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.