Skip to content

Commit

Permalink
Merge branch 'topic/test-fatal'
Browse files Browse the repository at this point in the history
  • Loading branch information
xsawyerx committed Mar 1, 2012
2 parents 677aa5a + e50c6df commit 5ec8961
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 190 deletions.
28 changes: 19 additions & 9 deletions t/app.t
@@ -1,6 +1,7 @@
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Dancer::Core::App;
use Dancer::Core::Dispatcher;
use Dancer::Core::Hook;
Expand Down Expand Up @@ -119,11 +120,13 @@ for my $path ('/foo', '/foo/second', '/foo/bar/second', '/root', '/somewhere') {
}

note "test a failure in the callback of a lexical prefix";
eval {
$app->lexical_prefix('/test' => sub { Failure->game_over() });
};
like $@, qr{Unable to run the callback for prefix '/test': Can't locate object method "game_over" via package "Failure"},
"caught an exception in the lexical prefix callback";
like(
exception {
$app->lexical_prefix('/test' => sub { Failure->game_over() });
},
qr{Unable to run the callback for prefix '/test': Can't locate object method "game_over" via package "Failure"},
"caught an exception in the lexical prefix callback",
);

$app->add_hook(Dancer::Core::Hook->new(
name => 'before',
Expand All @@ -141,16 +144,23 @@ my $env = {
PATH_INFO => '/',
};

eval { my $resp = $dispatcher->dispatch($env)->to_psgi };
like $@, qr{Exception caught in 'before' filter: Hook error: Can't locate object method "failure"};
like(
exception { my $resp = $dispatcher->dispatch($env)->to_psgi },
qr{Exception caught in 'before' filter: Hook error: Can't locate object method "failure"},
'before filter nonexistent method failure',
);

$app->replace_hooks('before', [ sub { 1 } ]);
$app->compile_hooks;
$env = {
REQUEST_METHOD => 'GET',
PATH_INFO => '/',
};
eval { my $resp = $dispatcher->dispatch($env)->to_psgi };
is $@, '';

is(
exception { my $resp = $dispatcher->dispatch($env)->to_psgi },
undef,
'Successful to_psgi of response',
);

done_testing;
31 changes: 21 additions & 10 deletions t/config.t
@@ -1,6 +1,7 @@
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Carp 'croak';

use Dancer::Core::Runner;
Expand Down Expand Up @@ -68,8 +69,11 @@ is_deeply [$fail->config_files],
"config_files() works";


eval { $fail->config };
like $@, qr{Unable to parse the configuration file};
like(
exception { $fail->config },
qr{Unable to parse the configuration file},
'Configuration file parsing failure',
);

note "config parsing";

Expand All @@ -85,8 +89,11 @@ note "default values";
is $f->setting('apphandler'), 'Standalone';
is $f->setting('content_type'), 'text/html';

eval { $f->_normalize_config({charset => 'BOGUS'}) };
like $@, qr{Charset defined in configuration is wrong : couldn't identify 'BOGUS'};
like(
exception { $f->_normalize_config({charset => 'BOGUS'}) },
qr{Charset defined in configuration is wrong : couldn't identify 'BOGUS'},
'Configuration file charset failure',
);

{
package Foo;
Expand All @@ -95,13 +102,17 @@ like $@, qr{Charset defined in configuration is wrong : couldn't identify 'BOGUS
}

is $f->setting('traces'), 0;
eval { Foo->foo() };
unlike $@, qr{Foo::foo},
"traces are not enabled";
unlike(
exception { Foo->foo() },
qr{Foo::foo},
"traces are not enabled",
);

$f->setting(traces => 1);
eval { Foo->foo() };
like $@, qr{Foo::foo},
"traces are enabled";
like(
exception { Foo->foo() },
qr{Foo::foo},
"traces are enabled",
);

done_testing;
10 changes: 7 additions & 3 deletions t/factory_engine.t
@@ -1,14 +1,18 @@
use Test::More;
use strict;
use warnings;
use Test::More;
use Test::Fatal;

use Dancer::Factory::Engine;

is Dancer::Factory::Engine::_camelize('foo_bar_baz'), 'FooBarBaz';
is Dancer::Factory::Engine::_camelize('FooBarBaz'), 'FooBarBaz';

eval { my $l = Dancer::Factory::Engine->create(unknown => 'stuff') };
like $@, qr{Unable to load class for Unknown engine Stuff: Can't locate};
like(
exception { my $l = Dancer::Factory::Engine->create(unknown => 'stuff') },
qr{Unable to load class for Unknown engine Stuff: Can't locate},
'Failure to load nonexistent class',
);

my $l = Dancer::Factory::Engine->create(logger => 'console');
isa_ok $l, 'Dancer::Logger::Console';
Expand Down
10 changes: 7 additions & 3 deletions t/file_utils.t
@@ -1,6 +1,7 @@
use Test::More tests => 9;
use strict;
use warnings;
use Test::More tests => 9;
use Test::Fatal;
use File::Spec;
use File::Temp 0.22;

Expand All @@ -20,8 +21,11 @@ sub hexe {
return $s;
}

eval { Dancer::FileUtils::open_file('<', '/slfkjsdlkfjsdlf') };
like $@, qr{/slfkjsdlkfjsdlf' using mode '<'};
like(
exception { Dancer::FileUtils::open_file('<', '/slfkjsdlkfjsdlf') },
qr{/slfkjsdlkfjsdlf' using mode '<'},
'Failure opening nonexistent file',
);

my $content = Dancer::FileUtils::read_file_content();
is $content, undef;
Expand Down

0 comments on commit 5ec8961

Please sign in to comment.