From: Vincent Pit Date: Sun, 5 Oct 2008 21:44:11 +0000 (+0200) Subject: Improve the class delegation mechanism in App::Rgit::Command, and test it X-Git-Tag: v0.02~8 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=d2c0f4071b90c3b50696c5b7f9cc4ae341628f33;p=perl%2Fmodules%2Frgit.git Improve the class delegation mechanism in App::Rgit::Command, and test it --- diff --git a/lib/App/Rgit/Command.pm b/lib/App/Rgit/Command.pm index ce7bac6..3754d83 100644 --- a/lib/App/Rgit/Command.pm +++ b/lib/App/Rgit/Command.pm @@ -44,12 +44,16 @@ sub new { my $cmd = $args{cmd}; $cmd = ' ' unless defined $cmd; my $action = $class->action($cmd); - croak "Command $cmd shouldn't be executed as an $action" - unless $class eq __PACKAGE__ or $class->isa($action); + if ($class eq __PACKAGE__) { + $class = $action; + } else { + croak "Command $cmd should be executed as a $action" + unless $class->isa($action); + } eval "require $action; 1" or croak "Couldn't load $action: $@"; my $r = App::Rgit::Repository->new(fake => 1); return unless defined $r; - $action->SUPER::new( + $class->SUPER::new( cmd => $cmd, args => $args{args} || [ ], repos => $args{repos}, diff --git a/t/15-failures.t b/t/15-failures.t index 6b823fe..5233267 100644 --- a/t/15-failures.t +++ b/t/15-failures.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 17; +use Test::More tests => 20; use App::Rgit; @@ -43,3 +43,16 @@ isa_ok($res, 'App::Rgit', '$ar->new(): no args: returns an object'); $res = eval { App::Rgit::new(undef, root => 't/repos', git => 't/bin/git', cmd => 'version'); }; is($@, '', 'undef->App::Rgit::new(): no args: does not croak'); isa_ok($res, 'App::Rgit','undef->App::Rgit::new(): no args: returns an object'); + +use App::Rgit::Command; + +eval { App::Rgit::Command::Once->App::Rgit::Command::new(cmd => 'dongs') }; +like($@, qr!Command\s+dongs\s+should\s+be\s+executed\s+as\s+a\s+App::Rgit::Command::Each!, 'App::Rgit::Command::Once->App::Rgit::Command::new(cmd => "dongs"): croaks'); + +{ + no strict 'refs'; + push @{'App::Rgit::Test::Foo::ISA'}, 'App::Rgit::Command::Once'; +} +$res = eval { App::Rgit::Test::Foo->App::Rgit::Command::new(cmd => 'version') }; +is($@, '', 'App::Rgit::Test::Foo->App::Rgit::Command::new(cmd => "version"): does not croak'); +isa_ok($res, 'App::Rgit::Test::Foo', 'App::Rgit::Test::Foo->App::Rgit::Command::new(cmd => "version"): returns valid object');