Commit d98dcf03 authored by sikeda's avatar sikeda
Browse files

[svn] Retrieving some recent additions on unittests from trunk.

git-svn-id: https://subversion.renater.fr/sympa/branches/sympa-6.2-branch@10730 05aa8bb8-cd2b-0410-b1d7-8918dfa770ce
parent 98d22a5f
......@@ -27,7 +27,10 @@ check_SCRIPTS = t/Language.t \
t/LockedFile.t \
t/Regexps.t \
t/compile_modules.t \
t/compile_executables.t
t/compile_executables.t \
t/tools_data.t \
t/tools_file.t \
t/tools_time.t
check_DATA = t/locale/cs/LC_MESSAGES/sympa.mo \
t/locale/cs/LC_MESSAGES/web_help.mo \
t/locale/zh_TW/LC_MESSAGES/sympa.mo
......
#!/usr/bin/perl
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id: tools_data.t 8606 2013-02-06 08:44:02Z rousse $
use strict;
use warnings;
use Test::More;
use tools; #Sympa::Tools::Data;
my @array_from_string_tests = (
[ 'foo,bar,baz' => [ qw/foo bar baz/ ] ],
[ ' foo, bar, baz' => [ qw/foo bar baz/ ] ],
[ 'foo ,bar ,baz ' => [ qw/foo bar baz/ ] ],
[ ' foo , bar , baz ' => [ qw/foo bar baz/ ] ],
);
my @string_2_hash_tests = (
[ 'var1="val1";var2="val2";' => { var1 => "val1", var2 => "val2" } ],
[ ';var1="val1";var2="val2"' => { var1 => "val1", var2 => "val2" } ]
);
my @hash_2_string_tests = (
[ { var1 => "val1", var2 => "val2" }
=> qr/^(;var1="val1";var2="val2"|;var2="val2";var1="val1")$/x ]
);
my @smart_lessthan_ok_tests = (
[ [ "", "1" ] ],
[ [ "1", "2" ] ],
[ [ " 1 ", " 2 " ] ],
);
my @smart_lessthan_nok_tests = (
[ [ "", "" ] ],
[ [ "1", "" ] ],
[ [ "1", "1" ] ],
[ [ "2", "1" ] ],
[ [ " 2 ", " 1 " ] ],
);
my @diff_on_arrays_tests = (
[
[ [], [] ] => {
intersection => [],
union => [],
added => [],
deleted => []
}
],
[
[ [ 'a' ], [ 'a' ] ] => {
intersection => [ 'a' ],
union => [ 'a' ],
added => [ ],
deleted => [ ]
}
],
[
[ [ 'a' ], [ 'b' ] ] => {
intersection => [],
union => [ 'a', 'b' ],
added => [ 'b' ],
deleted => [ 'a' ]
}
]
);
my @remove_empty_entries_tests = (
[ '' => [ 0, '' ] ],
[ 'a' => [ 1, 'a' ] ],
[ [ ] => [ 0, [ ] ] ],
[ [ 'a' ] => [ 1, [ 'a' ] ] ],
[ [ 'a', '' ] => [ 1, [ 'a', undef ] ] ],
[ [ 'a', '', 'b' ] => [ 1, [ 'a', undef, 'b' ] ] ],
[ { } => [ 0, { } ] ],
[ { a => 'a' } => [ 1, { a => 'a' } ] ],
[ { a => 'a', b => '' } => [ 1, { a => 'a', b => undef } ] ],
[ { a => 'a', b => 'b' } => [ 1, { a => 'a', b => 'b' } ] ],
);
my @recursive_transformation_tests = (
[ '' => '' ],
[ 'a' => 'a' ],
[ [ ] => [ ] ],
[ [ 'a' ] => [ 'aa' ] ],
[ [ 'a', '' ] => [ 'aa', '' ] ],
[ [ 'a', '', 'b' ] => [ 'aa', '', 'bb' ] ],
[ { } => { } ],
[ { a => 'a' } => { a => 'aa' } ],
[ { a => 'a', b => '' } => { a => 'aa', b => '' } ],
[ { a => 'a', b => 'b' } => { a => 'aa', b => 'bb' } ],
[ [ 'a', [ 'a' ], { a => 'a' } ] => [ 'aa', [ 'aa' ], { a => 'aa' } ] ],
);
plan tests =>
@array_from_string_tests +
@string_2_hash_tests +
@hash_2_string_tests +
@smart_lessthan_ok_tests +
@smart_lessthan_nok_tests +
@diff_on_arrays_tests +
@recursive_transformation_tests ;
foreach my $test (@array_from_string_tests) {
is_deeply(
tools::get_array_from_splitted_string($test->[0]),
$test->[1],
"get_array_from_splitted_string $test->[0]"
);
}
foreach my $test (@string_2_hash_tests) {
is_deeply(
{ tools::string_2_hash($test->[0]) },
$test->[1],
"string_2_hash $test->[0]"
);
}
foreach my $test (@hash_2_string_tests) {
like(
tools::hash_2_string($test->[0]),
$test->[1],
"hash_2_string"
);
}
foreach my $test (@smart_lessthan_ok_tests) {
ok(
tools::smart_lessthan(@{$test->[0]}),
"smart_lessthan $test->[0]->[0], $test->[0]->[1]"
);
}
foreach my $test (@smart_lessthan_nok_tests) {
ok(
!tools::smart_lessthan(@{$test->[0]}),
"smart_lessthan $test->[0]->[0], $test->[0]->[1]"
);
}
foreach my $test (@diff_on_arrays_tests) {
# normalize result to enforce constant ordering despite hash randomization
my $result = tools::diff_on_arrays(@{$test->[0]});
foreach my $key (qw/union intersection added deleted/) {
$result->{$key} = [ sort @{$result->{$key}} ];
}
my $expected = $test->[1];
is_deeply(
$result,
$expected,
"diff_in_arrays"
);
}
my $transformation = sub { return $_[0] . $_[0] };
foreach my $test (@recursive_transformation_tests) {
tools::recursive_transformation($test->[0], $transformation);
is_deeply(
$test->[0],
$test->[1],
"recursive_transformation"
);
}
#!/usr/bin/perl
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id: tools_file.t 8606 2013-02-06 08:44:02Z rousse $
use strict;
use warnings;
use Test::More;
use English qw(-no_match_vars);
use File::Temp;
use File::stat;
use Fcntl qw(:mode);
use tools; #Sympa::Tools::File;
plan tests => 21;
my $user = getpwuid($UID);
my $group = getgrgid($GID);
my $file = File::Temp->new();
ok(
tools::set_file_rights(file => $file),
'file, nothing else: ok'
);
ok(
!tools::set_file_rights(file => $file, user => 'none'),
'file, invalid user: ko'
);
ok(
!tools::set_file_rights(file => $file, group => 'none'),
'file, invalid group: ko'
);
ok(
tools::set_file_rights(file => $file, mode => 999),
'file, invalid mode: ok (fixme)'
);
ok(
!tools::set_file_rights(file => $file, user => $user, group => 'none'),
'file, valid user, invalid group: ko'
);
ok(
!tools::set_file_rights(file => $file, user => 'none', group => $group),
'file, invalid user, valid group: ko'
);
ok(
tools::set_file_rights(file => $file, user => $user, group => $group),
'file, valid user, valid group: ok'
);
ok(
tools::set_file_rights(file => $file, user => $user, group => $group, mode => 0666),
'file, valid user, valid group, valid mode: ok'
);
is(get_perms($file), "0666", "expected mode");
my $dir;
$dir = File::Temp->newdir();
tools::del_dir($dir);
ok(!-d $dir, 'del_dir with empty dir');
$dir = File::Temp->newdir();
tools::remove_dir($dir);
ok(!-d $dir, 'remove_dir with empty dir');
$dir = File::Temp->newdir();
touch($dir .'/foo');
tools::del_dir($dir);
ok(!-d $dir, 'del_dir with non empty dir');
$dir = File::Temp->newdir();
touch($dir .'/foo');
tools::remove_dir($dir);
ok(!-d $dir, 'remove_dir with non empty dir');
$dir = File::Temp->newdir();
tools::mk_parent_dir($dir . '/foo/bar/baz');
ok(-d "$dir/foo", 'mk_parent_dir first element');
ok(-d "$dir/foo/bar", 'mk_parent_dir second element');
$dir = File::Temp->newdir();
tools::mkdir_all($dir . '/foo/bar/baz');
ok(!-d "$dir/foo", 'mkdir_all first element, no mode');
ok(!-d "$dir/foo/bar", 'mkdir_all second element, no mode');
$dir = File::Temp->newdir();
tools::mkdir_all($dir . '/foo/bar/baz', 0777);
ok(-d "$dir/foo", 'mkdir_all first element');
ok(-d "$dir/foo/bar", 'mkdir_all second element');
is(get_perms("$dir/foo"), "0777", "first element, expected mode");
is(get_perms("$dir/foo/bar"), "0777", "second element, expected mode");
sub touch {
my ($file) = @_;
open (my $fh, '>', $file) or die "Can't create file: $ERRNO";
close $fh;
}
sub get_perms {
my ($file) = @_;
return sprintf("%04o", stat($file)->mode() & 07777);
}
#!/usr/bin/perl
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id: tools_time.t 8606 2013-02-06 08:44:02Z rousse $
use strict;
use warnings;
use POSIX qw(setlocale LC_ALL LC_CTYPE);
use Test::More;
use tools; #Sympa::Tools::Time;
setlocale(LC_ALL, 'C');
my @epoch2yyyymmjj_hhmmss_tests = (
[ 1350544367, '2012-10-18 09:12:47' ],
[ 1250544367, '2009-08-17 23:26:07' ],
);
my @adate_tests = (
[ 1350544367, '18 Thu Oct 2012 09 h 12 min 47 s' ],
[ 1250544367, '17 Mon Aug 2009 23 h 26 min 07 s' ],
);
my @get_midnight_time_tests = (
[ 1350544367, 1350511200 ],
[ 1250544367, 1250460000 ],
);
my @date_conv_tests = (
[ [ 1350544367 , undef ] => 1350544367 ],
[ [ '2012y10m18d09h12min47sec', undef ] => 1350511967 ],
[ [ '2012y10m18d09h12min', undef ] => 1350511920 ],
[ [ '2012y10m18d09h', undef ] => 1350511200 ],
[ [ '2012y10m18d', undef ] => 1350511200 ],
[ [ '2012y10m', undef ] => 1349042400 ],
[ [ '2012y', undef ] => 1325372400 ],
);
my @duration_conv_tests = (
[ [ 0 , 0 ] => 0 ],
[ [ '1sec' , 0 ] => 1 ],
[ [ '1min' , 0 ] => 60 ],
[ [ '1h' , 0 ] => 3600 ],
[ [ '1d' , 0 ] => 86400 ],
[ [ '1w' , 0 ] => 604800 ],
[ [ '1m' , 0 ] => 2678400 ],
[ [ '2m' , 0 ] => 5097600 ],
[ [ '1y' , 0 ] => 31536000 ],
[ [ '1y1m1w1d1h1min1sec', 0 ] => 34909261 ],
);
my @epoch_conv_tests = (
);
plan tests =>
##@epoch2yyyymmjj_hhmmss_tests + # Recently unavailable
@adate_tests +
@get_midnight_time_tests +
@date_conv_tests +
@duration_conv_tests +
@epoch_conv_tests ;
#foreach my $test (@epoch2yyyymmjj_hhmmss_tests) {
# is(
# tools::epoch2yyyymmjj_hhmmss($test->[0]),
# $test->[1],
# "epoch2yyyymmjj_hhmmss $test->[0]"
# );
#}
foreach my $test (@adate_tests) {
is(
tools::adate($test->[0]),
$test->[1],
"adate $test->[0]"
);
}
foreach my $test (@get_midnight_time_tests) {
is(
tools::get_midnight_time($test->[0]),
$test->[1],
"get_midnight_time $test->[0]"
);
}
foreach my $test (@date_conv_tests) {
is(
tools::date_conv(@{$test->[0]}),
$test->[1],
"date_conv $test->[0]"
);
}
foreach my $test (@duration_conv_tests) {
is(
tools::duration_conv(@{$test->[0]}),
$test->[1],
"duration_conv $test->[0]"
);
}
foreach my $test (@epoch_conv_tests) {
is(
tools::epoch_conv(@{$test->[0]}),
$test->[1],
"epoch_conv $test->[0]"
);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment