added test for config options to Config::General
[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
16
17our %ext_map = (
18 conf => 'Config::Any::General',
19 ini => 'Config::Any::INI',
20 json => 'Config::Any::JSON',
21 pl => 'Config::Any::Perl',
22 xml => 'Config::Any::XML',
23 yml => 'Config::Any::YAML'
24);
25
26sub load_parser_for {
27 my $f = shift;
28 return unless $f;
29
30 my ($ext) = $f =~ m{ \. ( [^\.]+ ) \z }xms;
31 my $mod = $ext_map{$ext};
32 my $mod_load_result;
33 eval { $mod_load_result = $mod->load( $f ); delete $INC{$f} if $ext eq 'pl' };
34 return $@ ? (1,$mod) : (0,$mod);
35}
36
37for 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 = Config::Any->load_files({files=>[$f], use_ext=>1}),
43 "load_files with use_ext works [$f]");
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 }, 'bar',
58 "component->cntrlr->foo = bar" );
59 is( $cfg->{Model}{ "Model::Baz" }->{ qux }, 'xyzzy',
60 "model->model::baz->qux = xyzzy" );
61 }
62}
63