App-CmdDispatch
view release on metacpan or search on metacpan
lib/App/CmdDispatch/Exception.pm view on Meta::CPAN
die App::CmdDispatch::Exception::MissingCommand->new();
=head1 DESCRIPTION
These exception classes simplify exceptional conditions that need a bit more
context to report that the simple fact of the exception. These are used in the
parts of the run code where we want to catch the exception and perform some
recovery.
Other places in the code where we just want to leave the program, I've thrown
simple strings instead.
=head1 INTERFACE
=head2 new( @args )
Create an exception object. Store the supplied args if necessary to explain
context of exception.
=head2 why()
t/config-bad.t view on Meta::CPAN
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/lib";
use File::Temp;
use App::CmdDispatch;
throws_ok
{
App::CmdDispatch->new(
{
noop => {
code => sub { }
}
},
{ config => 'xyzzy' }
);
}
t/help_new-bad.t view on Meta::CPAN
use Test::Exception;
use strict;
use warnings;
use App::CmdDispatch::Help;
# Mock the creation of the dispatch object.
my $app = bless {}, 'App::CmdDispatch';
throws_ok { App::CmdDispatch::Help->new( $app, 'commands' ); } qr/not a hashref/,
'commands not hash';
throws_ok { App::CmdDispatch::Help->new( $app, undef ); } qr/not a hashref/,
'commands undef';
throws_ok { App::CmdDispatch::Help->new( $app, {} ); } qr/No commands/,
'empty commands hash';
throws_ok { App::CmdDispatch::Help->new( $app, { noop => { code => sub {} } }, 'config' ); } qr/Config .* not a hashref/,
'config not hash';
throws_ok { App::CmdDispatch::Help->new( {}, { noop => { code => sub {} } } ); } qr/Invalid owner object/,
'Owner object wrong type';
t/new-bad.t view on Meta::CPAN
#!/usr/bin/env perl
use Test::More tests => 7;
use Test::Exception;
use strict;
use warnings;
use App::CmdDispatch;
throws_ok { App::CmdDispatch->new } qr/Command definition is not a hashref/, 'No args';
throws_ok { App::CmdDispatch->new( 'foo' ) } qr/Command definition is not a hashref/, 'Non-hashref arg';
throws_ok { App::CmdDispatch->new( {} ) } qr/No commands specified/, 'Empty command hash';
throws_ok { App::CmdDispatch->new( { noop => { code => sub {} } }, 'foo' ) } qr/Options .* not a hashref/, 'Non-hashref options';
# Bad commands
throws_ok { App::CmdDispatch->new( { foo => 'aa' } ) }
qr/'foo' is an invalid/, 'Command description not a hash';
throws_ok { App::CmdDispatch->new( { foo => {} } ) }
qr/'foo' has no handler/, 'No handler for foo';
throws_ok { App::CmdDispatch->new( { foo => { code => 'ddd' } } ) }
qr/'foo' has no handler/, 'Handler for foo is not code';
t/table_new-bad.t view on Meta::CPAN
#!/usr/bin/env perl
use Test::More tests => 9;
use Test::Exception;
use strict;
use warnings;
use App::CmdDispatch::Table;
throws_ok { App::CmdDispatch::Table->new } qr/Command definition is not a hashref/, 'No args';
throws_ok { App::CmdDispatch::Table->new( 'foo' ) } qr/Command definition is not a hashref/, 'Non-hashref arg';
throws_ok { App::CmdDispatch::Table->new( {} ) } qr/No commands specified/, 'Empty command hash';
throws_ok { App::CmdDispatch::Table->new( { noop => { code => sub {} } }, 'foo' ) } qr/Aliases .* not a hashref/, 'Non-hashref options';
# Bad commands
throws_ok { App::CmdDispatch::Table->new( { foo => 'aa' } ) }
qr/'foo' is an invalid/, 'Command description not a hash';
throws_ok { App::CmdDispatch::Table->new( { foo => {} } ) }
qr/'foo' has no handler/, 'No handler for foo';
throws_ok { App::CmdDispatch::Table->new( { foo => { code => 'ddd' } } ) }
qr/'foo' has no handler/, 'Handler for foo is not code';
# Bad aliases
throws_ok { App::CmdDispatch::Table->new( { foo => { code => sub {} } }, { 'bar' => {} } ) }
qr/'bar' mapping is not a string/, 'Alias points to a hash';
throws_ok { App::CmdDispatch::Table->new( { foo => { code => sub {} } }, { 'bar' => [] } ) }
qr/'bar' mapping is not a string/, 'Alias points to an array';
t/table_run.t view on Meta::CPAN
use App::CmdDispatch::Table;
# TODO - need a mock object to pass as first parameter and need to test for it in run.
{
my $app = App::CmdDispatch::Table->new(
{
noop => { code => sub {} },
},
);
throws_ok { $app->run(); } 'App::CmdDispatch::Exception::MissingCommand', "Running with no parameters gives error.\n";
}
{
my $app = App::CmdDispatch::Table->new(
{
noop => { code => sub {} },
},
);
throws_ok { $app->run( {} ); } 'App::CmdDispatch::Exception::MissingCommand', "Running with no command gives error.\n";
}
{
my $app = App::CmdDispatch::Table->new(
{
noop => { code => sub {} },
},
);
throws_ok { $app->run( {}, '' ); } 'App::CmdDispatch::Exception::MissingCommand', "Running with empty command gives error.\n";
}
{
my $app = App::CmdDispatch::Table->new(
{
noop => { code => sub {} },
},
);
lives_ok { $app->run( {}, 'noop' ); } "noop command run successfully";
}
{
my $app = App::CmdDispatch::Table->new(
{
noop => { code => sub {} },
},
);
throws_ok { $app->run( {}, 'foo' ); } 'App::CmdDispatch::Exception::UnknownCommand', "Unrecognized command gives error";
}
( run in 0.266 second using v1.01-cache-2.11-cpan-496ff517765 )