Skip to content

Commit

Permalink
Merge branch 'fix/plugin-edgecase'
Browse files Browse the repository at this point in the history
  • Loading branch information
cromedome committed Apr 2, 2022
2 parents 7ca2665 + e37f5a5 commit 65d8a4e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{$NEXT}}

[ BUG FIXES ]
* None
* PR #1247: Fix edge case in plugin compat (Sawyer X)

[ ENHANCEMENTS ]
* None
Expand Down
8 changes: 6 additions & 2 deletions lib/Dancer2/Plugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,12 @@ sub register_plugin {
no strict 'refs';
$plugin_module->can($keyword)
or *{"${plugin_module}::$keyword"} = sub {
my $coderef = shift()->app->name->can($keyword);
$coderef->(@_);
$_[0]
? do {
my $cb = shift()->app->name->can($keyword);
$cb->(@_);
}
: $app_dsl_cb->(@_);
};
}
});
Expand Down
20 changes: 20 additions & 0 deletions t/plugin2/app_dsl_cb/app_dsl_cb.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use strict;
use warnings;
use lib 't/plugin2/app_dsl_cb/lib';
use Test::More 'tests' => 4;
use Plack::Test;
use HTTP::Request::Common 'GET';
use App with => { show_errors => 1 };
use Test::Fatal 'exception';

my $app = App->to_app;
my $test = Plack::Test->create($app);

my $res;
is(
exception { $res = $test->request( GET '/' ) },
undef,
'Did not crash',
);

is( $res->content, 'GET DONE', 'Ran successfully' );
13 changes: 13 additions & 0 deletions t/plugin2/app_dsl_cb/lib/App.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package App;
use strict;
use warnings;
use Dancer2 appname => 'Other';
use App::TestPlugin;

get '/' => sub {
my $res = foo_from_plugin('Foo');
::is( $res, 'OK', 'Plugin returned OK' );
return 'GET DONE';
};

1;
17 changes: 17 additions & 0 deletions t/plugin2/app_dsl_cb/lib/App/TestPlugin.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package App::TestPlugin;
use strict;
use warnings;
use Dancer2::Plugin;

plugin_keywords('foo_from_plugin');

sub foo_from_plugin {
my ( $self, $arg ) = @_;
::is( $arg, 'Foo', 'Correct argument to plugin' );
params();
return 'OK';
}

register_plugin();

1;

0 comments on commit 65d8a4e

Please sign in to comment.