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