App-MtAws
view release on metacpan or search on metacpan
t/integration/config_read_config.t view on Meta::CPAN
ok ! defined eval { read_as_config("$badname"); 1 }, "should deny invalid names";
is get_exception && get_exception->{code}, 'invalid_config_line';
ok ! defined eval { read_as_config(" $badname"); 1 }, "should deny invalid names";
is get_exception && get_exception->{code}, 'invalid_config_line';
ok ! defined eval { read_as_config("$badname=myvalue"); 1 }, "should deny invalid names";
is get_exception && get_exception->{code}, 'invalid_config_line';
}
for my $goodname ("somename", 'some-name', 'some--name', 'SomeName', 'Some-Name', '1', '1name', '1-name', 'name-123') {
is_deeply(read_as_config("$goodname=myvalue"), { $goodname => 'myvalue'}, "should accet correct names");
is_deeply(read_as_config("$goodname"), { $goodname => 1}, "should accet correct names");
}
for my $badline ("x!=1", "ÑеÑÑ=1", "test!1=ÑеÑÑ") {
my $utfbadline = encode("UTF-8", $badline);
for my $append_lines (0..3) {
my $wholefile = join("\n", (map { $_ } 1..$append_lines), $utfbadline);
ok ! defined eval { read_as_config($wholefile); 1 };
ok get_exception;
is get_exception->{code}, 'invalid_config_line', "should have valid exception code";
is get_exception->{lineno}, $append_lines + 1, "should report correct lineno";
is get_exception->{line}, hex_dump_string($utfbadline), "should report correct line";
is get_exception->{config}, hex_dump_string($file), "should report filename";
is exception_message(get_exception),
"Cannot parse line in config file: ".hex_dump_string($utfbadline)." at ".hex_dump_string($file)." line ".($append_lines + 1);
}
}
for my $utfstring ("ÑеÑÑ", "вÑ") {
is_deeply(
read_as_config(encode("UTF-8", "mykey1=$utfstring")),
{ 'mykey1' => $utfstring },
"should read utf string"
);
}
for my $comment (" ", "\t", " ", " \t", "#", " #", "# comment", "###", " # comment", "\t#a=b", "\t\t#a=b", "\t#\ta=b") {
my $vars = { 'mykey1' => 'myvalue', 'mykey2' => 'myvalue2'};
is_deeply(read_as_config("mykey1=myvalue\n$comment\nmykey2=myvalue2"), $vars, "should allow comments in the beginning of line");
}
for my $notcomment ("#", " #", "# comment", "###", " # comment", "\t#a=b", "\t\t#a=b", "\t#\ta=b") {
my $vars = { 'mykey1' => 'myvalue'.$notcomment, 'mykey2' => 'myvalue2'};
is_deeply(read_as_config("mykey1=myvalue$notcomment\nmykey2=myvalue2"), $vars, "should not allow comments in the midle of line");
}
for my $value ("a", "=b", "a=b", "c=d", "e==f", "===x") {
my $vars = { 'mykey1' => 'myvalue'.$value, 'mykey2' => 'myvalue2'};
is_deeply(read_as_config("mykey1=myvalue$value\nmykey2=myvalue2"), $vars, "should allow equal sign in values");
my $vars2 = { 'mykey1' => $value, 'mykey2' => 'myvalue2'};
is_deeply(read_as_config("mykey1=$value\nmykey2=myvalue2"), $vars2, "should allow equal sign in values");
}
{
unlink $file if -e $file;
ok ! -e $file, "assert we deleted file";
my $C = App::MtAws::ConfigEngine->new();
ok !defined eval { $C->read_config($file); 1 };
ok get_exception;
is get_exception->{code}, 'config_file_is_not_a_file';
is get_exception->{config}, hex_dump_string($file);
is exception_message(get_exception), "Config file is not a file: ".hex_dump_string($file);
}
{
unlink $file;
rmtree($file) if -d $file;
mkpath($file);
ok -d $file, "assert file is directory";
my $C = App::MtAws::ConfigEngine->new();
ok !defined eval { $C->read_config($file); 1;};
ok get_exception;
is get_exception->{code}, 'config_file_is_not_a_file';
is get_exception->{config}, hex_dump_string($file);
is exception_message(get_exception), "Config file is not a file: ".hex_dump_string($file);
}
sub read_as_config
{
my ($bytes) = @_;
open F, ">", $file;
binmode F;
print F $bytes;
close F;
my $C = App::MtAws::ConfigEngine->new();
my $r = $C->read_config($file);
return undef unless defined $r;
return ({map { decode("UTF-8", $_) } %$r}); # UTF-8 decode hash
}
1;
( run in 0.454 second using v1.01-cache-2.11-cpan-ceb78f64989 )