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