cf07aa0bfa52d40585d0444cb5b9293af2d33158
[p5sagit/Config-Any.git] / t / 20-parse.t
1 package MockApp;
2 use strict;
3 use warnings;
4 no warnings 'once';
5
6 $|++;
7 use Test::More tests => 54;
8 use Scalar::Util qw(blessed reftype);
9 use Config::Any;
10 use Config::Any::General;
11 use Config::Any::INI;
12 use Config::Any::JSON;
13 use Config::Any::Perl;
14 use Config::Any::XML;
15 use Config::Any::YAML;
16
17 $Config::Any::YAML::NO_YAML_XS_WARNING = 1;
18
19 our %ext_map = (
20     conf => 'Config::Any::General',
21     ini  => 'Config::Any::INI',
22     json => 'Config::Any::JSON',
23     pl   => 'Config::Any::Perl',
24     xml  => 'Config::Any::XML',
25     yml  => 'Config::Any::YAML'
26 );
27
28 sub load_parser_for {
29     my $f = shift;
30     return unless $f;
31
32     my ( $ext ) = $f =~ m{ \. ( [^\.]+ ) \z }xms;
33     my $mod = $ext_map{ $ext };
34     my $mod_load_result;
35     eval {
36         $mod_load_result = $mod->load( $f );
37         delete $INC{ $f } if $ext eq 'pl';
38     };
39     return $@ ? ( 1, $mod ) : ( 0, $mod );
40 }
41
42 for my $f ( map { "t/conf/conf.$_" } keys %ext_map ) {
43     my ( $skip, $mod ) = load_parser_for( $f );
44 SKIP: {
45         skip "File loading backend for $mod not found", 9 if $skip;
46
47         ok( my $c_arr
48                 = Config::Any->load_files(
49                 { files => [ $f ], use_ext => 1 } ),
50             "load_files with use_ext works [$f]"
51         );
52         ok( my $c = $c_arr->[ 0 ], "load_files returns an arrayref" );
53
54         ok( ref $c, "load_files arrayref contains a ref" );
55         my $ref = blessed $c ? reftype $c : ref $c;
56         is( substr( $ref, 0, 4 ), "HASH", "hashref" );
57
58         my ( $name, $cfg ) = each %$c;
59         is( $name, $f, "filename matches" );
60
61         my $cfgref = blessed $cfg ? reftype $cfg : ref $cfg;
62         is( substr( $cfgref, 0, 4 ), "HASH", "hashref cfg" );
63
64         is( $cfg->{ name }, 'TestApp', "appname parses" );
65         is( $cfg->{ Component }{ "Controller::Foo" }->{ foo },
66             'bar', "component->cntrlr->foo = bar" );
67         is( $cfg->{ Model }{ "Model::Baz" }->{ qux },
68             'xyzzy', "model->model::baz->qux = xyzzy" );
69     }
70 }
71