use is_supported to figure out if we can test a loader
[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     return !$mod->is_supported ? ( 1, $mod ) : ( 0, $mod );
35 }
36
37 for my $f ( map { "t/conf/conf.$_" } keys %ext_map ) {
38     my ( $skip, $mod ) = load_parser_for( $f );
39 SKIP: {
40         skip "File loading backend for $mod not found", 9 if $skip;
41
42         ok( my $c_arr
43                 = Config::Any->load_files(
44                 { files => [ $f ], use_ext => 1 } ),
45             "load_files with use_ext works [$f]"
46         );
47         ok( my $c = $c_arr->[ 0 ], "load_files returns an arrayref" );
48
49         ok( ref $c, "load_files arrayref contains a ref" );
50         my $ref = blessed $c ? reftype $c : ref $c;
51         is( substr( $ref, 0, 4 ), "HASH", "hashref" );
52
53         my ( $name, $cfg ) = each %$c;
54         is( $name, $f, "filename matches" );
55
56         my $cfgref = blessed $cfg ? reftype $cfg : ref $cfg;
57         is( substr( $cfgref, 0, 4 ), "HASH", "hashref cfg" );
58
59         is( $cfg->{ name }, 'TestApp', "appname parses" );
60         is( $cfg->{ Component }{ "Controller::Foo" }->{ foo },
61             'bar', "component->cntrlr->foo = bar" );
62         is( $cfg->{ Model }{ "Model::Baz" }->{ qux },
63             'xyzzy', "model->model::baz->qux = xyzzy" );
64     }
65 }
66