perlperl-provetest-more

Cannot detect source of 'factorial'. Why?


I am getting the following traceback when I try and prove factorial:

Cannot detect source of 'factorial'! at /usr/share/perl/5.18/TAP/Parser/IteratorFactory.pm line 263.
    TAP::Parser::IteratorFactory::detect_source('TAP::Parser::IteratorFactory=HASH(0x2856b08)', 'TAP::Parser::Source=HASH(0x2856bc8)') called at /usr/share/perl/5.18/TAP/Parser/IteratorFactory.pm line 213
    TAP::Parser::IteratorFactory::make_iterator('TAP::Parser::IteratorFactory=HASH(0x2856b08)', 'TAP::Parser::Source=HASH(0x2856bc8)') called at /usr/share/perl/5.18/TAP/Parser.pm line 469
    TAP::Parser::_initialize('TAP::Parser=HASH(0x28569e8)', 'HASH(0x23d8cb8)') called at /usr/share/perl/5.18/TAP/Object.pm line 58
    TAP::Object::new('TAP::Parser', 'HASH(0x23d8cb8)') called at /usr/share/perl/5.18/TAP/Object.pm line 133
    TAP::Object::_construct('TAP::Harness=HASH(0x2131db8)', 'TAP::Parser', 'HASH(0x23d8cb8)') called at /usr/share/perl/5.18/TAP/Harness.pm line 779
    TAP::Harness::make_parser('TAP::Harness=HASH(0x2131db8)', 'TAP::Parser::Scheduler::Job=HASH(0x28235d0)') called at /usr/share/perl/5.18/TAP/Harness.pm line 578
    TAP::Harness::_aggregate_single('TAP::Harness=HASH(0x2131db8)', 'TAP::Parser::Aggregator=HASH(0x2740ae0)', 'TAP::Parser::Scheduler=HASH(0x2823570)') called at /usr/share/perl/5.18/TAP/Harness.pm line 670
    TAP::Harness::aggregate_tests('TAP::Harness=HASH(0x2131db8)', 'TAP::Parser::Aggregator=HASH(0x2740ae0)', 'factorial') called at /usr/share/perl/5.18/TAP/Harness.pm line 485
    TAP::Harness::__ANON__() called at /usr/share/perl/5.18/TAP/Harness.pm line 498
    TAP::Harness::runtests('TAP::Harness=HASH(0x2131db8)', 'factorial') called at /usr/share/perl/5.18/App/Prove.pm line 554
    App::Prove::_runtests('App::Prove=HASH(0x2120170)', 'HASH(0x2606428)', 'TAP::Harness', 'factorial') called at /usr/share/perl/5.18/App/Prove.pm line 512
    App::Prove::run('App::Prove=HASH(0x2120170)') called at /usr/bin/prove line 11

This is my factorial.pm:

package factorial;

use Exporter qw(import);

BEGIN {
    our @EXPORT_OK  = qw(factorial);
}

sub factorial
{
    my ($parm) = @_;

    my $factorial = 1;

    if ($parm == 0)
    {
        return $factorial;
    }

    foreach my $i (1..$parm) {
        $factorial *= $i;
    }

    return $factorial;
}

1;

And my factorial.t:

#!/usr/bin/perl -w
use Test::More;

use factorial 'factorial';
is(factorial(0), 1, "boundary case");
is(factorial(1), 1, "factorial(1)");
is(factorial(2), 2, "factorial(2)");
is(factorial(6), 720, "factorial(6)");

done_testing;

I verified that my factorial module works by running this program (or, at least, does one thing correctly).

#!/usr/bin/perl -w

use factorial 'factorial';

my $parm = 5;
print "Factorial($parm) = ", factorial($parm), "\n";

Solution

  • The man page for prove states that it accepts options and files or directories:

    prove [options] [files or directories]
    

    it will not do any name expansion for you.

    You have given prove an argument of factorial, while I suspect your test file is named factorial.t

    The following prove commands should all be valid:

    prove factorial.t
    prove fac*
    prove <directory that contains factorial.t>
    

    Note: the last two commands could run more than one test depending on the contents of the working directory, or the directory you pass to prove