62bd59e1d792ca1793d92858fd3823d60ee8f71d
[p5sagit/Config-Any.git] / t / 61-features.t
1 package MockApp;
2 use strict;
3 use warnings;
4
5 $|++;
6
7 use Test::More tests => 14;
8 use Scalar::Util qw(blessed reftype);
9
10 use Config::Any;
11 use Config::Any::INI;
12
13 our $cfg_file = 't/conf/conf.foo';
14
15 eval { Config::Any::INI->load( $cfg_file ); };
16
17 SKIP: {
18     skip "File loading backend for INI not found", 14 if $@;
19
20     my $struct; # used to make sure parsing works for arrays and hashes
21
22     # force_plugins
23     {
24         my $result = Config::Any->load_files(
25             {   files         => [ $cfg_file ],
26                 force_plugins => [ qw(Config::Any::INI) ]
27             }
28         );
29
30         ok( $result, 'load file with parser forced' );
31
32         ok( my $first = $result->[ 0 ], 'load_files returns an arrayref' );
33         ok( ref $first, 'load_files arrayref contains a ref' );
34
35         my $ref = blessed $first ? reftype $first : ref $first;
36         is( substr( $ref, 0, 4 ), 'HASH', 'hashref' );
37
38         $struct = $first;
39
40         my ( $name, $cfg ) = %$first;
41         is( $name, $cfg_file, 'filenames match' );
42
43         my $cfgref = blessed $cfg ? reftype $cfg : ref $cfg;
44         is( substr( $cfgref, 0, 4 ), 'HASH', 'hashref cfg' );
45
46         is( $cfg->{ name }, 'TestApp', 'appname parses' );
47         is( $cfg->{ Component }{ "Controller::Foo" }->{ foo },
48             'bar', 'component->cntrlr->foo = bar' );
49         is( $cfg->{ Model }{ "Model::Baz" }->{ qux },
50             'xyzzy', 'model->model::baz->qux = xyzzy' );
51     }
52
53     # flatten_to_hash
54     {
55         my $result = Config::Any->load_files(
56             {   files           => [ $cfg_file ],
57                 force_plugins   => [ qw(Config::Any::INI) ],
58                 flatten_to_hash => 1
59             }
60         );
61
62         ok( $result, 'load file with parser forced, flatten to hash' );
63         ok( ref $result, 'load_files hashref contains a ref' );
64
65         my $ref = blessed $result ? reftype $result : ref $result;
66         is( substr( $ref, 0, 4 ), 'HASH', 'hashref' );
67
68         is_deeply( $result, $struct, 'load_files return an hashref (flatten_to_hash)' );
69     }
70      
71     # use_ext  
72     {
73         ok( my $result = Config::Any->load_files(
74                 {   files         => [ $cfg_file ],
75                     force_plugins => [ qw(Config::Any::INI) ],
76                     use_ext       => 1
77                 }
78             ),
79             "load file with parser forced"
80         );
81     }
82 }
83