Add tests for the create script and fix the bugs that this shows up
Tomas Doran [Tue, 24 Nov 2009 00:15:45 +0000 (00:15 +0000)]
lib/Catalyst/Script/Create.pm
t/aggregate/unit_core_script_create.t [new file with mode: 0644]

index d53427e..ab85ded 100644 (file)
@@ -1,6 +1,5 @@
 package Catalyst::Script::Create;
 use Moose;
-use Catalyst::Helper;
 use MooseX::Types::Moose qw/Bool/;
 use namespace::autoclean;
 
@@ -32,14 +31,18 @@ has mechanize => (
     documentation => 'use WWW::Mechanize',
 );
 
+has helper_class => ( isa => 'Str', is => 'ro', default => 'Catalyst::Helper' );
+
 sub run {
     my ($self) = @_;
 
     $self->_exit_with_usage if !$ARGV[0];
 
-    my $helper = Catalyst::Helper->new( { '.newfiles' => !$self->force, mech => $self->mech } );
+    my $helper_class = $self->helper_class;
+    Class::MOP::load_class($helper_class);
+    my $helper = $helper_class->new( { '.newfiles' => !$self->force, mech => $self->mechanize } );
 
-    $self->_display_help unless $helper->mk_component( $self->app, @ARGV );
+    $self->_exit_with_usage unless $helper->mk_component( $self->application_name, @ARGV );
 
 }
 
diff --git a/t/aggregate/unit_core_script_create.t b/t/aggregate/unit_core_script_create.t
new file mode 100644 (file)
index 0000000..db2e5af
--- /dev/null
@@ -0,0 +1,75 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+use Test::Exception;
+
+use FindBin qw/$Bin/;
+use lib "$Bin/../lib";
+
+{
+    package TestCreateScript;
+    use Moose;
+    extends 'Catalyst::Script::Create';
+    our $help;
+    sub _exit_with_usage { $help++ }
+}
+
+{
+    package TestHelperClass;
+    use Moose;
+    
+    has 'newfiles' => ( is => 'ro', init_arg => '.newfiles' );
+    has 'mech' => ( is => 'ro' );
+    our @ARGS;
+    our %p;
+    sub mk_component {
+        my $self = shift;
+        @ARGS = @_;
+        %p = ( '.newfiles' => $self->newfiles, mech => $self->mech);
+        return $self->_mk_component_return;
+    }
+    sub _mk_component_return { 1 }
+}
+{
+    package TestHelperClass::False;
+    use Moose;
+    extends 'TestHelperClass';
+    sub _mk_component_return { 0 }
+}
+
+{
+    local $TestCreateScript::help;
+    local @ARGV;
+    lives_ok {
+        TestCreateScript->new_with_options(application_name => 'TestAppToTestScripts', helper_class => 'TestHelperClass')->run;
+    } "no argv";
+    ok $TestCreateScript::help, 'Exited with usage info';
+}
+{
+    local $TestCreateScript::help;
+    local @ARGV = 'foo';
+    local @TestHelperClass::ARGS;
+    local %TestHelperClass::p;
+    lives_ok {
+        TestCreateScript->new_with_options(application_name => 'TestAppToTestScripts', helper_class => 'TestHelperClass')->run;
+    } "with argv";
+    ok !$TestCreateScript::help, 'Did not exit with usage into';
+    is_deeply \@TestHelperClass::ARGS, ['TestAppToTestScripts', 'foo'], 'Args correct';
+    is_deeply \%TestHelperClass::p, { '.newfiles' => 1, mech => undef }, 'Params correct';
+}
+
+{
+    local $TestCreateScript::help;
+    local @ARGV = 'foo';
+    local @TestHelperClass::ARGS;
+    local %TestHelperClass::p;
+    lives_ok {
+        TestCreateScript->new_with_options(application_name => 'TestAppToTestScripts', helper_class => 'TestHelperClass::False')->run;
+    } "with argv";
+    ok $TestCreateScript::help, 'Did exit with usage into as mk_component returned false';
+    is_deeply \@TestHelperClass::ARGS, ['TestAppToTestScripts', 'foo'], 'Args correct';
+    is_deeply \%TestHelperClass::p, { '.newfiles' => 1, mech => undef }, 'Params correct';
+}
+
+done_testing;