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.0502

SYNOPSIS

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

  # Or on the command-line:
  # perl -MData::Dumper -MFoo::Bar -MTest::NaiveStubs -E \
  #   '$tns = Test::NaiveStubs->new(module => "Foo::Bar"); $tns->gather_subs; say Dumper $tns->subs'
  # perl -MFoo::Bar -MTest::NaiveStubs -E \
  #   '$tns = Test::NaiveStubs->new(module => "Foo::Bar"); $tns->create_test'

DESCRIPTION

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

Unfortunately Class::Sniff returns any imported methods as well as the ones in the module you have given. So you will have to remove those lines from the generated test file by hand.

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

ATTRIBUTES

module

  $module = $tns->module;

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

name

  $name = $tns->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 = $tns->subs;

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

METHODS

new()

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

Create a new Test::NaiveStubs object.

gather_subs()

  $tns->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 = $tns->unit_test($method);

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

create_test()

  $tns->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

Moo

Class::Sniff

AUTHOR

Gene Boggs <gene@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2019 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.