logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Test::Exports - Test that modules export the right symbols

Author

       Ben Morrow <ben@morrow.me.uk>

Bugs

       Please report any bugs to <bug-Test-Exports@rt.cpan.org>.

Description

       This module provides simple test functions for testing other modules' "import" methods. Testing is
       currently limited to checking which subs have been imported.

       In order to keep different calls to "->import" separate, Test::Exports performs these calls from a
       private package. The symbol-testing functions then test whether or not symbols are present in this
       private package, ensuring none of this interferes with your test script itself.

Functions

       These are all exported by default, as is usual with testing modules.

   "new_import_pkg"
       Create a new package to perform imports into. This is useful when you want to test "->import" with
       different arguments: otherwise you'd need to find some way of going back and clearing up the imports from
       the last call.

       This returns the name of the new package (which will look like "Test::Exports::TestAAAAB") in case you
       need it.

   "import_ok$module,\@args,$name"
       Call "$module->import" from the current testing package, passing @args, and check the call succeeded.
       'Success' means not throwing an exception: "use" doesn't care if "import" returns false, so neither do
       we.

       @args defaults to the empty list; $name defaults to something sensible.

   "import_nok$module,\@args,$name"
       Call "$module->import(@args)" and expect it to throw an exception.  Defaults as for "import_ok".

   "is_import@subs,$module,$name"
       For each name in @subs, check that the current testing package has a sub by that name and that it is the
       same as the equinominal sub in the $module package.

       Neither $module nor $name are optional.

   "cant_ok@subs,$name"
       For each sub in @subs, check that a sub of that name does not exist in the current testing package. If
       one is found the diagnostic will indicate where it was originally defined, to help track down the stray
       export.

Name

       Test::Exports - Test that modules export the right symbols

Synopsis

           use Test::More;
           use Test::Exports;

           require_ok "My::Module" or BAIL_OUT "can't load module";

           import_ok "My::Module", [],             "default import OK";
           is_import qw/foo bar/, "My::Module",    "imports subs";

           new_import_pkg;

           import_ok "My::Module", ["foo"],        "named import OK";
           is_import "foo", "My::Module",          "imports foo";
           cant_ok "bar",                          "doesn't import bar";

Todo

"is_import"
       Currently this just checks that "\&Our::Pkg::sub == \&Your::Pkg::sub", which means

       •   it is impossible to test for exports which have been renamed, and

       •   we can't be sure the sub originally came from Your::Pkg: it may have been exported into both packages
           from somewhere else.

       It would be good to fix at least the former.

See Also