Config-Perl
view release on metacpan or search on metacpan
t/10_basic.t view on Meta::CPAN
#!/usr/bin/env perl
use warnings;
use strict;
# Tests for the Perl module Config::Perl
#
# Copyright (c) 2015 Hauke Daempfling (haukex@zero-g.net).
#
# This library is free software; you can redistribute it and/or modify
# it under the same terms as Perl 5 itself.
#
# For more information see the "Perl Artistic License",
# which should have been distributed with your copy of Perl.
# Try the command "perldoc perlartistic" or see
# http://perldoc.perl.org/perlartistic.html .
use FindBin ();
use lib $FindBin::Bin;
use Config_Perl_Testlib;
use Test::More;
use Test::Fatal 'exception';
use File::Temp 'tempfile';
use Config::Perl;
# ### Basics & Structure ###
test_ppconf q{ $foo="bar"; }, { '$foo'=>"bar" }, 'basic test';
test_ppconf q{ $foo="bar" }, { '$foo'=>"bar" }, 'no semicolon';
test_ppconf q{ our $foo="bar"; }, { '$foo'=>"bar" }, '"our"';
test_ppconf q{ our $foo=123; }, { '$foo'=>123 }, 'number';
test_ppconf q{ my $foo=123; }, { '$foo'=>123 }, '"my" in outermost block',
{del_syms=>['$foo']};
test_ppconf q{}, {}, 'empty doc';
test_ppconf q{ "foo";; }, { '_'=>["foo"] }, 'two semicolons';
test_ppconf q{ "foo" }, { _=>["foo"] }, 'plain value';
test_ppconf q{ ('foo','bar') }, { _=>["foo","bar"] }, 'plain list';
test_ppconf q{ ["foo","bar"] }, { _=>[["foo","bar"]] }, 'plain arrayref';
test_ppconf q{ {foo=>"bar"} }, { _=>[{foo=>"bar"}] }, 'plain hashref';
test_ppconf q{ $foo=123; $bar=456; }, { '$foo'=>123, '$bar'=>456 }, 'two vars';
test_ppconf q{ $VAR1 = { 'foo' => 'bar' }; }, { '$VAR1'=>{foo=>"bar"} }, 'Data::Dumper hashref';
test_ppconf q{ $VAR1 = 'quz'; $VAR2 = 'baz'; }, { '$VAR1'=>"quz", '$VAR2'=>"baz" }, 'Data::Dumper list';
ok exception {
Config::Perl->new->parse_or_die(
\q{ our @x = qw/a b/, our @y = qw/c d/ });
}, 'accidental comma fails';
test_ppconf q{ LBL: "foo" }, { _=>["foo"] }, 'plain value w/ label';
test_ppconf q{ LBL: $foo = "bar"; }, { '$foo'=>"bar" }, 'assignment w/ label';
test_ppconf q{ LBL: our $foo="bar"; }, { '$foo'=>"bar" }, 'decl w/ label';
test_ppconf q{ LBL: do { "foo" } }, { _=>["foo"] }, 'do block w/ label';
test_ppconf q{ our $test = ("Hello","World"); },
{ '$test' => 'World' }, 'list in scalar';
test_ppconf q{ our $test = ("Hello","World",()); },
{ '$test' => undef }, 'list to scalar w/ empty list';
test_ppconf q{ our $test = qw/ foo bar quz /; },
{ '$test' => 'quz' }, 'qw in scalar';
# parse from file
my ($fh, $fn) = tempfile(UNLINK=>1);
print $fh <<'END';
$foo = "bar";
$quz = 123;
END
close $fh;
my $cp = Config::Perl->new;
is_deeply $cp->parse_or_undef($fn),
{ '$foo'=>"bar", '$quz'=>123 }, 'parse file';
# ### Interpolation ###
test_ppconf q{ our $foo='$bar'; }, { '$foo'=>'$bar' }, 'non-interpolated';
test_ppconf q{ our $foo= -bar; }, { '$foo'=>-bar }, 'dashed bareword';
test_ppconf q{ our $foo="bar"; our $quz="baz$foo"; },
{ '$foo'=>"bar", '$quz'=>"bazbar" }, 'interpolation';
test_ppconf q{ our $foo="bar"; our $quz="baz$foo$foo"; },
{ '$foo'=>"bar", '$quz'=>"bazbarbar" }, 'multi interp';
( run in 1.628 second using v1.01-cache-2.11-cpan-54e63673c56 )