rename these classes to be more clear
[gitmo/MooseX-ConfigFromFile.git] / t / 05_default_sub.t
index 6c41191..f5a44f9 100644 (file)
@@ -1,14 +1,14 @@
 use strict;
 use warnings FATAL => 'all';
 
-use Test::Requires 'MooseX::SimpleConfig';      # skip all if not reuqired
-use Test::More tests => 19;
+use Test::More tests => 33;
 use Test::Fatal;
 use Test::Deep '!blessed';
 use Test::NoWarnings 1.04 ':early';
 use Scalar::Util 'blessed';
 
 my %loaded_file;
+my %configfile_sub;
 my %constructor_args;
 
 
@@ -16,7 +16,7 @@ my %constructor_args;
 {
     package Generic;
     use Moose;
-    with 'MooseX::SimpleConfig';
+    with 'MooseX::ConfigFromFile';
     sub get_config_from_file
     {
         my ($class, $file) = @_;
@@ -31,6 +31,7 @@ my %constructor_args;
     sub __my_configfile
     {
         my $class = blessed($_[0]) || $_[0];
+        $configfile_sub{$class}++;
         $class . ' file'
     }
 }
@@ -72,6 +73,27 @@ is(
     'no exceptions',
 );
 
+{
+    package OverriddenDefaultMethod;
+    use Moose;
+    extends 'Generic';
+    has '+configfile' => (
+        default => sub { shift->__my_configfile },
+    );
+}
+
+is(
+    exception {
+        my $obj = OverriddenDefaultMethod->new_with_config;
+        is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default');
+        is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
+        is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
+    },
+    undef,
+    'no exceptions',
+);
+
+
 # legacy usecase, and configfile init_arg has been changed
 {
     package OverriddenDefaultAndChangedName;
@@ -110,6 +132,8 @@ is(
     exception {
         my $obj = OverriddenMethod->new_with_config;
         is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
+        # this is not fixable - the reader method has been shadowed
+        # is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
         is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
     },
     undef,
@@ -137,6 +161,60 @@ is(
             {  my_configfile => blessed($obj) . ' file' },
             'correct constructor args passed',
         );
+        # this is not fixable - the reader method has been shadowed
+        # is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
+        is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
+    },
+    undef,
+    'no exceptions',
+);
+
+# newly-supported overridable method for configfile default
+{
+    package NewSub;
+    use Moose;
+    extends 'Generic';
+    sub _get_default_configfile { shift->__my_configfile }
+}
+
+is(
+    exception {
+        my $obj = NewSub->new_with_config;
+        is($obj->configfile, blessed($obj) . ' file', 'configfile set via new sub');
+        cmp_deeply(
+            $constructor_args{blessed($obj)},
+            {  configfile => blessed($obj) . ' file' },
+            'correct constructor args passed',
+        );
+        is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
+        is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
+    },
+    undef,
+    'no exceptions',
+);
+
+# newly-supported overridable method for configfile default, and configfile
+# init_arg has been changed
+{
+    package NewSubAndChangedName;
+    use Moose;
+    extends 'Generic';
+    has '+configfile' => (
+        init_arg => 'my_configfile',
+    );
+    sub _get_default_configfile { shift->__my_configfile }
+}
+
+is(
+    exception {
+        my $obj = NewSubAndChangedName->new_with_config;
+        is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
+        cmp_deeply(
+            $constructor_args{blessed($obj)},
+            {  my_configfile => blessed($obj) . ' file' },
+            'correct constructor args passed',
+        );
+        is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
         is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
     },
     undef,