* t/*.t: Fixed test plans.
[gitmo/MooseX-Getopt.git] / t / 008_configfromfile.t
index 148bd4f..5db9586 100644 (file)
@@ -6,13 +6,13 @@ use warnings;
 use Test::Exception;
 use Test::More;
 
-if ( !require MooseX::ConfigFromFile )
+if ( !eval { require MooseX::ConfigFromFile } )
 {
     plan skip_all => 'Test requires MooseX::ConfigFromFile';
 }
 else
 {
-    plan tests => 24;
+    plan tests => 123;
 }
 
 {
@@ -74,91 +74,183 @@ else
     );
 }
 
-# No config specified
-{
-    local @ARGV = qw( --required_from_argv 1 );
+foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser::Descriptive MooseX::Getopt::Parser::Default)) {
+    SKIP: {
+        if ($parser_name eq 'MooseX::Getopt::Parser::Descriptive') {
+            eval { require Getopt::Long::Descriptive };
+            skip "Getopt::Long::Descriptive not installed", 41 if $@;
+        }
 
-    throws_ok { App->new_with_options } qr/Required option missing: required_from_config/;
+        # No config specified
+        {
+            local @ARGV = qw( --required_from_argv 1 );
 
-    {
-        my $app = App::DefaultConfigFile->new_with_options;
-        isa_ok( $app, 'App::DefaultConfigFile' );
-        app_ok( $app );
+            throws_ok {
+                my $parser = $parser_name->new;
+                ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
 
-        ok(  !$app->config_from_override,
-            '... config_from_override false as expected' );
+                my $options = App->get_options_from_configfile;
+                my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App' } );
+                isa_ok($getopt, 'MooseX::Getopt::Session');
 
-        is( $app->configfile, '/notused/default',
-            '... configfile is /notused/default as expected' );
-    }
-}
+                App->new_with_options( getopt => $getopt );
+            } qr/required_from_config/;
 
-# Config specified
-{
-    local @ARGV = qw( --configfile /notused --required_from_argv 1 );
+            {
+                my $parser = $parser_name->new;
+                ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
 
-    {
-        my $app = App->new_with_options;
-        isa_ok( $app, 'App' );
-        app_ok( $app );
-    }
+                my $options = App::DefaultConfigFile->get_options_from_configfile;
+                my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App::DefaultConfigFile' } );
+                isa_ok($getopt, 'MooseX::Getopt::Session');
 
-    {
-        my $app = App::DefaultConfigFile->new_with_options;
-        isa_ok( $app, 'App::DefaultConfigFile' );
-        app_ok( $app );
+                my $app = App::DefaultConfigFile->new_with_options( getopt => $getopt );
+                isa_ok( $app, 'App::DefaultConfigFile' );
+                app_ok( $app );
 
-        ok( $app->config_from_override,
-             '... config_from_override true as expected' );
+                ok(  !$app->config_from_override,
+                    '... config_from_override false as expected' );
 
-        is( $app->configfile, '/notused',
-            '... configfile is /notused as expected' );
-    }
-}
+                is( $app->configfile, '/notused/default',
+                    '... configfile is /notused/default as expected' );
+            }
+        }
 
-# Required arg not supplied from cmdline
-{
-    local @ARGV = qw( --configfile /notused );
-    throws_ok { App->new_with_options } qr/Required option missing: required_from_argv/;
-}
+        # Config specified
+        {
+            local @ARGV = qw( --configfile /notused --required_from_argv 1 );
 
-# Config file value overriden from cmdline
-{
-    local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
+            {
+                my $parser = $parser_name->new;
+                ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
 
-    my $app = App->new_with_options;
-    isa_ok( $app, 'App' );
+                my $options = App->get_options_from_configfile;
+                my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App' } );
+                isa_ok($getopt, 'MooseX::Getopt::Session');
 
-    is( $app->required_from_config, 'override',
-        '... required_from_config is override as expected' );
+                my $app = App->new_with_options( getopt => $getopt );
+                isa_ok( $app, 'App' );
+                app_ok( $app );
+            }
 
-    is( $app->optional_from_config, 'from_config_2',
-        '... optional_from_config is from_config_2 as expected' );
-}
+            {
+                my $parser = $parser_name->new;
+                ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
 
-# No config file
-{
-    local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
+                my $options = App::DefaultConfigFile->get_options_from_configfile;
+                my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App::DefaultConfigFile' } );
+                isa_ok($getopt, 'MooseX::Getopt::Session');
 
-    my $app = App->new_with_options;
-    isa_ok( $app, 'App' );
+                my $app = App::DefaultConfigFile->new_with_options( getopt => $getopt );
+                isa_ok( $app, 'App::DefaultConfigFile' );
+                app_ok( $app );
 
-    is( $app->required_from_config, 'noconfig',
-        '... required_from_config is noconfig as expected' );
+                ok( $app->config_from_override,
+                     '... config_from_override true as expected' );
 
-    ok( !defined $app->optional_from_config,
-        '... optional_from_config is undef as expected' );
-}
+                is( $app->configfile, '/notused',
+                    '... configfile is /notused as expected' );
+            }
+        }
+
+        # Required arg not supplied from cmdline
+        {
+            local @ARGV = qw( --configfile /notused );
+            throws_ok {
+                my $parser = $parser_name->new;
+                ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
+
+                my $options = App->get_options_from_configfile;
+                my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App' } );
+                isa_ok($getopt, 'MooseX::Getopt::Session');
+
+                App->new_with_options( getopt => $getopt );
+            } qr/required_from_argv/;
+        }
+
+        # Config file value overriden from cmdline
+        {
+            local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
+
+            my $parser = $parser_name->new;
+            ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
 
-sub app_ok {
-    my $app = shift;
+            my $options = App->get_options_from_configfile;
+            my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App' } );
+            isa_ok($getopt, 'MooseX::Getopt::Session');
 
-    is( $app->required_from_config, 'from_config_1',
-        '... required_from_config is from_config_1 as expected' );
+            my $app = App->new_with_options( getopt => $getopt );
+            isa_ok( $app, 'App' );
 
-    is( $app->optional_from_config, 'from_config_2',
-        '... optional_from_config is from_config_2 as expected' );
+            is( $app->required_from_config, 'override',
+                '... required_from_config is override as expected' );
 
-    is( $app->required_from_argv, '1',
-        '... required_from_argv is 1 as expected' );
+            is( $app->optional_from_config, 'from_config_2',
+                '... optional_from_config is from_config_2 as expected' );
+        }
+
+        # No config file
+        {
+            local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
+
+            my $parser = $parser_name->new;
+            ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
+
+            my $options = App->get_options_from_configfile;
+            my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App' } );
+            isa_ok($getopt, 'MooseX::Getopt::Session');
+
+            my $app = App->new_with_options( getopt => $getopt );
+            isa_ok( $app, 'App' );
+
+            is( $app->required_from_config, 'noconfig',
+                '... required_from_config is noconfig as expected' );
+
+            ok( !defined $app->optional_from_config,
+                '... optional_from_config is undef as expected' );
+        }
+
+        {
+            package BaseApp::WithConfig;
+            use Moose;
+            with 'MooseX::ConfigFromFile';
+
+            sub get_config_from_file { return {}; }
+        }
+
+        {
+            package DerivedApp::Getopt;
+            use Moose;
+            extends 'BaseApp::WithConfig';
+            with 'MooseX::Getopt';
+        }
+
+        # With DerivedApp, the Getopt role was applied at a different level
+        # than the ConfigFromFile role
+        {
+            lives_ok {
+                my $parser = $parser_name->new;
+                ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
+
+                my $getopt = MooseX::Getopt::Session->new( parser => $parser, classes_filter => sub { $_ eq 'DerivedApp::Getopt' } );
+                isa_ok($getopt, 'MooseX::Getopt::Session');
+
+                DerivedApp::Getopt->new_with_options( getopt => $getopt );
+            } 'Can create DerivedApp';
+        }
+
+        sub app_ok {
+            my $app = shift;
+
+            is( $app->required_from_config, 'from_config_1',
+                '... required_from_config is from_config_1 as expected' );
+
+            is( $app->optional_from_config, 'from_config_2',
+                '... optional_from_config is from_config_2 as expected' );
+
+            is( $app->required_from_argv, '1',
+                '... required_from_argv is 1 as expected' );
+        }
+
+    }
 }