fix copyright date - was all garbled in licence
[gitmo/MooseX-ConfigFromFile.git] / t / 05_default_sub.t
CommitLineData
7acf2fe9 1use strict;
2use warnings;
3
4use Test::More tests => 10;
5use Test::Fatal;
6use Test::Deep '!blessed';
7use Test::NoWarnings 1.04 ':early';
8use Scalar::Util 'blessed';
9
10my %loaded_file;
11my %default_sub;
12
13
14# nothing special going on here
15{
16 package Generic;
17 use Moose;
18 with 'MooseX::SimpleConfig';
19 sub get_config_from_file { }
20}
21
22is(
23 exception {
24 my $obj = Generic->new_with_config;
25 is($obj->configfile, undef, 'no configfile set');
26 },
27 undef,
28 'no exceptions',
29);
30
31
32# this is a classic legacy usecase from old documentation that we must
33# continue to support
34{
35 package OverriddenDefault;
36 use Moose;
37 with 'MooseX::SimpleConfig';
38 sub get_config_from_file
39 {
40 my ($class, $file) = @_;
41 $loaded_file{$file}++;
42 +{}
43 }
44 has '+configfile' => (
45 default => 'OverriddenDefault file',
46 );
47}
48
49is(
50 exception {
51 my $obj = OverriddenDefault->new_with_config;
52 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default');
53 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
54 },
55 undef,
56 'no exceptions',
57);
58
59
60# "reader" method is overridden to provide for configfile default
61{
62 package OverriddenMethod;
63 use Moose;
64 with 'MooseX::SimpleConfig';
65 sub get_config_from_file {
66 my ($class, $file) = @_;
67 $loaded_file{$file}++;
68 +{}
69 }
70
71 around configfile => sub {
72 my $class = blessed($_[1]) || $_[1];
73 $default_sub{$class}++;
74 $class . ' file'
75 };
76}
77
78is(
79 exception {
80 my $obj = OverriddenMethod->new_with_config;
81 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
82 ok($default_sub{blessed($obj)}, 'default sub was called');
83 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
84 },
85 undef,
86 'no exceptions',
87);
88
89