prep release
[p5sagit/Config-Any.git] / t / 20-parse.t
CommitLineData
f0e3c221 1package MockApp;
2use strict;
3use warnings;
e7be073a 4no warnings 'once';
f0e3c221 5
6$|++;
7use Test::More tests => 54;
8use Scalar::Util qw(blessed reftype);
9use Config::Any;
10use Config::Any::General;
11use Config::Any::INI;
12use Config::Any::JSON;
13use Config::Any::Perl;
14use Config::Any::XML;
15use Config::Any::YAML;
16
e7be073a 17$Config::Any::YAML::NO_YAML_XS_WARNING = 1;
18
f0e3c221 19our %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
28sub load_parser_for {
29 my $f = shift;
30 return unless $f;
31
92a04e78 32 my ( $ext ) = $f =~ m{ \. ( [^\.]+ ) \z }xms;
33 my $mod = $ext_map{ $ext };
f0e3c221 34 my $mod_load_result;
92a04e78 35 eval {
36 $mod_load_result = $mod->load( $f );
37 delete $INC{ $f } if $ext eq 'pl';
38 };
39 return $@ ? ( 1, $mod ) : ( 0, $mod );
f0e3c221 40}
41
92a04e78 42for my $f ( map { "t/conf/conf.$_" } keys %ext_map ) {
43 my ( $skip, $mod ) = load_parser_for( $f );
44SKIP: {
f0e3c221 45 skip "File loading backend for $mod not found", 9 if $skip;
92a04e78 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" );
f0e3c221 55 my $ref = blessed $c ? reftype $c : ref $c;
92a04e78 56 is( substr( $ref, 0, 4 ), "HASH", "hashref" );
57
58 my ( $name, $cfg ) = each %$c;
59 is( $name, $f, "filename matches" );
f0e3c221 60
f0e3c221 61 my $cfgref = blessed $cfg ? reftype $cfg : ref $cfg;
92a04e78 62 is( substr( $cfgref, 0, 4 ), "HASH", "hashref cfg" );
f0e3c221 63
92a04e78 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" );
f0e3c221 69 }
70}
71