From: Brian Cassidy Date: Mon, 28 Jan 2008 14:54:51 +0000 (+0000) Subject: fix for ini subsections (RT #32726), use from_json for JSON v2.x, refactor test suite. X-Git-Tag: v0.11~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2FConfig-Any.git;a=commitdiff_plain;h=5a2e0210fe5c7da046be37b4240008f2b667be67;hp=f103b82bc783be531637c754c37783aa6c144a45 fix for ini subsections (RT #32726), use from_json for JSON v2.x, refactor test suite. --- diff --git a/Changes b/Changes index f484f5d..2adfc71 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,10 @@ Revision history for Config-Any +0.11 Mon 28 Jan 2008 + - fix subsection parsing for existing keys in INI files (RT #32726) + - use from_json() if JSON version 2.x is available + - refactor the test suite slightly + 0.10 Tue 11 Dec 2007 - promote dev release to stable diff --git a/lib/Config/Any/INI.pm b/lib/Config/Any/INI.pm index d281e60..68e3f06 100644 --- a/lib/Config/Any/INI.pm +++ b/lib/Config/Any/INI.pm @@ -45,23 +45,21 @@ sub load { require Config::Tiny; my $config = Config::Tiny->read( $file ); - - my $main = delete $config->{ _ }; - my $out; - $out->{ $_ } = $main->{ $_ } for keys %$main; + my $out = delete $config->{ _ } || {}; for my $k ( keys %$config ) { - my @keys = split /\s+/, $k if $MAP_SECTION_SPACE_TO_NESTED_KEY; + my @keys = split /\s+/, $k; my $ref = $config->{ $k }; - if ( @keys > 1 ) { + if ( $MAP_SECTION_SPACE_TO_NESTED_KEY && @keys > 1 ) { my ( $a, $b ) = @keys[ 0, 1 ]; $out->{ $a }->{ $b } = $ref; } else { - $out->{ $k } = $ref; + $out->{ $k } = { %{ $out->{ $k } || {} }, %$ref }; } } + return $out; } diff --git a/lib/Config/Any/JSON.pm b/lib/Config/Any/JSON.pm index 3213128..6c6eb6e 100644 --- a/lib/Config/Any/JSON.pm +++ b/lib/Config/Any/JSON.pm @@ -50,7 +50,8 @@ sub load { eval { require JSON::Syck; }; if ( $@ ) { require JSON; - return JSON::jsonToObj( $content ); + eval { JSON->VERSION( 2 ); }; + return $@ ? JSON::jsonToObj( $content ) : JSON::from_json( $content ); } else { return JSON::Syck::Load( $content ); diff --git a/t/01-use.t b/t/01-use.t index 5b8dc61..631208a 100644 --- a/t/01-use.t +++ b/t/01-use.t @@ -1,3 +1,6 @@ +use strict; +use warnings; + use Test::More tests => 6; BEGIN { diff --git a/t/10-branches.t b/t/10-branches.t index 24c6209..8368bcc 100644 --- a/t/10-branches.t +++ b/t/10-branches.t @@ -1,3 +1,6 @@ +use strict; +use warnings; + use Test::More tests => 10; use_ok( 'Config::Any' ); diff --git a/t/50-general.t b/t/50-general.t index e684bf2..ec0dc53 100644 --- a/t/50-general.t +++ b/t/50-general.t @@ -1,19 +1,25 @@ -use Test::More tests => 4; +use strict; +use warnings; +use Test::More; use Config::Any::General; -my $config = eval { Config::Any::General->load( 't/conf/conf.conf' ) }; +if ( !Config::Any::General->is_supported ) { + plan skip_all => 'Config::General format not supported'; +} +else { + plan tests => 4; +} -SKIP: { - skip "Couldn't Load Config::General plugin", 4 if $@; +{ + my $config = Config::Any::General->load( 't/conf/conf.conf' ); ok( $config ); is( $config->{ name }, 'TestApp' ); ok( exists $config->{ Component } ); +} - $config = eval { - Config::Any::General->load( 't/conf/conf.conf', - { -LowerCaseNames => 1 } ); - }; - +{ + my $config = Config::Any::General->load( 't/conf/conf.conf', + { -LowerCaseNames => 1 } ); ok( exists $config->{ component } ); } diff --git a/t/51-ini.t b/t/51-ini.t index c7d6036..8040d5b 100644 --- a/t/51-ini.t +++ b/t/51-ini.t @@ -1,30 +1,48 @@ -use Test::More tests => 9; +use strict; +use warnings; +use Test::More; use Config::Any::INI; -my $config = eval { Config::Any::INI->load( 't/conf/conf.ini' ) }; -my $simpleconfig = eval { Config::Any::INI->load( 't/conf/conf2.ini' ) }; +if ( !Config::Any::INI->is_supported ) { + plan skip_all => 'INI format not supported'; +} +else { + plan tests => 11; +} -SKIP: { - skip "Couldn't Load INI plugin", 6 if $@; - ok( $config, "loaded INI config #1" ); +{ + my $config = Config::Any::INI->load( 't/conf/conf.ini' ); + ok( $config, 'config loaded' ); is( $config->{ name }, 'TestApp', "toplevel key lookup succeeded" ); - is( $config->{ Component }->{ Controller::Foo }->{ foo }, + is( $config->{ Component }->{ 'Controller::Foo' }->{ foo }, 'bar', "nested hashref hack lookup succeeded" ); +} - ok( $simpleconfig, "loaded INI config #1" ); - is( $simpleconfig->{ name }, 'TestApp', "toplevel key lookup succeeded" ); - is( $simpleconfig->{ Controller::Foo }->{ foo }, +{ + my $config = Config::Any::INI->load( 't/conf/conf2.ini' ); + ok( $config, 'config loaded' ); + is( $config->{ name }, 'TestApp', "toplevel key lookup succeeded" ); + is( $config->{ 'Controller::Foo' }->{ foo }, 'bar', "nested hashref hack lookup succeeded" ); } -$Config::Any::INI::MAP_SECTION_SPACE_TO_NESTED_KEY = 0; -my $unspaced_config = eval { Config::Any::INI->load( 't/conf/conf.ini' ); }; -SKIP: { - skip "Couldn't load INI plugin", 3 if $@; - ok( $unspaced_config, "loaded INI config #1 in no-map-space mode" ); - is( $unspaced_config->{ name }, - 'TestApp', "toplevel key lookup succeeded" ); - is( $unspaced_config->{ 'Component Controller::Foo' }->{ foo }, +{ + local $Config::Any::INI::MAP_SECTION_SPACE_TO_NESTED_KEY = 0; + my $config = Config::Any::INI->load( 't/conf/conf.ini' ); + ok( $config, 'config loaded (no-map-space mode)' ); + is( $config->{ name }, 'TestApp', "toplevel key lookup succeeded" ); + is( $config->{ 'Component Controller::Foo' }->{ foo }, 'bar', "unnested key lookup succeeded" ); } + +{ + my $config = Config::Any::INI->load( 't/conf/subsections.ini' ); + + my %expected + = ( section1 => + { a => 1, subsection1 => { b => 2 }, subsection2 => { c => 3 } } + ); + ok( $config, 'config loaded' ); + is_deeply( $config, \%expected, 'subsections parsed properly' ); +} diff --git a/t/52-json.t b/t/52-json.t index dcf084f..5467ebe 100644 --- a/t/52-json.t +++ b/t/52-json.t @@ -1,11 +1,18 @@ -use Test::More tests => 2; +use strict; +use warnings; +use Test::More; use Config::Any::JSON; -my $config = eval { Config::Any::JSON->load( 't/conf/conf.json' ) }; +if ( !Config::Any::JSON->is_supported ) { + plan skip_all => 'JSON format not supported'; +} +else { + plan tests => 2; +} -SKIP: { - skip "Couldn't Load JSON plugin", 2 if $@; +{ + my $config = Config::Any::JSON->load( 't/conf/conf.json' ); ok( $config ); is( $config->{ name }, 'TestApp' ); } diff --git a/t/53-perl.t b/t/53-perl.t index 5e51b41..6d2d5ca 100644 --- a/t/53-perl.t +++ b/t/53-perl.t @@ -1,17 +1,18 @@ +use strict; +use warnings; + use Test::More tests => 3; use Config::Any::Perl; -my $file = 't/conf/conf.pl'; -my $config = eval { Config::Any::Perl->load( $file ) }; - -SKIP: { - skip "Couldn't Load Perl plugin", 3 if $@; +{ + my $file = 't/conf/conf.pl'; + my $config = Config::Any::Perl->load( $file ); ok( $config ); is( $config->{ name }, 'TestApp' ); - my $config_2 = eval { Config::Any::Perl->load( $file ) }; - - is_deeply( $config_2, $config, 'multiple loads of perl configs' ); + my $config_load2 = Config::Any::Perl->load( $file ); + is_deeply( $config_load2, $config, 'multiple loads of the same file' ); } + diff --git a/t/54-xml.t b/t/54-xml.t index 4ddba3c..fb52d51 100644 --- a/t/54-xml.t +++ b/t/54-xml.t @@ -1,11 +1,18 @@ -use Test::More tests => 2; +use strict; +use warnings; +use Test::More; use Config::Any::XML; -my $config = eval { Config::Any::XML->load( 't/conf/conf.xml' ) }; +if ( !Config::Any::XML->is_supported ) { + plan skip_all => 'XML format not supported'; +} +else { + plan tests => 2; +} -SKIP: { - skip "Couldn't Load XML plugin", 2 if $@; +{ + my $config = Config::Any::XML->load( 't/conf/conf.xml' ); ok( $config ); is( $config->{ name }, 'TestApp' ); } diff --git a/t/55-yaml.t b/t/55-yaml.t index 049b79f..3c95831 100644 --- a/t/55-yaml.t +++ b/t/55-yaml.t @@ -1,11 +1,18 @@ -use Test::More tests => 2; +use strict; +use warnings; +use Test::More; use Config::Any::YAML; -my $config = eval { Config::Any::YAML->load( 't/conf/conf.yml' ) }; +if ( !Config::Any::YAML->is_supported ) { + plan skip_all => 'YAML format not supported'; +} +else { + plan tests => 2; +} -SKIP: { - skip "Couldn't Load YAML plugin", 2 if $@; +{ + my $config = Config::Any::YAML->load( 't/conf/conf.yml' ); ok( $config ); is( $config->{ name }, 'TestApp' ); } diff --git a/t/62-multi.t b/t/62-multi.t index 2705de3..df57486 100644 --- a/t/62-multi.t +++ b/t/62-multi.t @@ -1,8 +1,8 @@ -use Test::More tests => 3; - use strict; use warnings; +use Test::More tests => 3; + use Config::Any; use Config::Any::YAML; diff --git a/t/pod-coverage.t b/t/pod-coverage.t index 2ee90fa..0ae970f 100644 --- a/t/pod-coverage.t +++ b/t/pod-coverage.t @@ -1,7 +1,9 @@ -#!perl -T - +use strict; +use warnings; use Test::More; + eval "use Test::Pod::Coverage 1.04"; + plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; all_pod_coverage_ok(); diff --git a/t/pod.t b/t/pod.t index 976d7cd..4840188 100644 --- a/t/pod.t +++ b/t/pod.t @@ -1,6 +1,8 @@ -#!perl -T - +use strict; +use warnings; use Test::More; + eval "use Test::Pod 1.14"; + plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; all_pod_files_ok();