File: date_manip.t

package info (click to toggle)
grepmail 5.3033-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,424 kB
  • ctags: 535
  • sloc: perl: 9,438; makefile: 6
file content (153 lines) | stat: -rwxr-xr-x 3,626 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/perl

use strict;

use Test::More;
use lib 't';
use Test::Utils;
use File::Spec::Functions qw( :ALL );

my %tests = (
'grepmail -d "before 1st Tuesday in July 1998" t/mailboxes/mailarc-1.txt'
  => ['date_manip','none'],
'grepmail -d "after armageddon" pattern t/mailboxes/mailarc-1.txt'
  => ['none','invalid_date_1'],
"grepmail -d \"after armageddon\" -E $single_quote\$email =~ /pattern/$single_quote t/mailboxes/mailarc-1.txt"
  => ['none','invalid_date_1'],
"grepmail -d \"1st Tuesday in July 1998\" . t/mailboxes/mailarc-1.txt"
  => ['sep_7_1998','none'],
);

my %expected_errors = (
'grepmail -d "after armageddon" pattern t/mailboxes/mailarc-1.txt'
  => 1,
"grepmail -d \"after armageddon\" -E $single_quote\$email =~ /pattern/$single_quote t/mailboxes/mailarc-1.txt"
  => 1,
);

mkdir 't/temp', 0700;

plan tests => scalar (keys %tests) * 2 + 1;

my %skip = SetSkip(\%tests);

SKIP:
{
  print "Checking Date::Manip::Date_TimeZone()\n";

  skip("Date::Manip not installed",1) unless Module_Installed('Date::Manip');

  # Date::Manip prior to 5.39 nukes the PATH. Save and restore it to avoid
  # problems.
  my $path = $ENV{PATH};
  require Date::Manip;
  $ENV{PATH} = $path;

  if (eval 'Date::Manip::Date_TimeZone()')
  {
    ok(1);
  }
  else
  {
    print <<EOF;

WARNING: Your time zone is not recognized by Date::Manip. It is likely
that many test cases related to dates will fail. See the README for more
information on how to resolve this problem.

EOF
    ok(0);
  }
}

# So that the tests will work consistently across timezones
$ENV{'TZ'} = 'EST';

foreach my $test (sort keys %tests) 
{
  print "Running test:\n  $test\n";

  SKIP:
  {
    skip("$skip{$test}",2) if exists $skip{$test};

    TestIt($test, $tests{$test}, $expected_errors{$test});
  }
}

# ---------------------------------------------------------------------------

sub TestIt
{
  my $test = shift;
  my ($stdout_file,$stderr_file) = @{ shift @_ };
  my $error_expected = shift;

  my $testname = [splitdir($0)]->[-1];
  $testname =~ s#\.t##;

  {
    my @standard_inc = split /###/, `perl -e '\$" = "###";print "\@INC"'`;
    my @extra_inc;
    foreach my $inc (@INC)
    {
      push @extra_inc, "$single_quote$inc$single_quote"
        unless grep { /^$inc$/ } @standard_inc;
    }

    local $" = ' -I';
    if (@extra_inc)
    {
      $test =~ s#\bgrepmail\s#$^X -I@extra_inc blib/script/grepmail -C t/temp/cache #g;
    }
    else
    {
      $test =~ s#\bgrepmail\s#$^X blib/script/grepmail -C t/temp/cache #g;
    }
  }

  my $test_stdout = catfile('t','temp',"${testname}_$stdout_file.stdout");
  my $test_stderr = catfile('t','temp',"${testname}_$stderr_file.stderr");

  system "$test 1>$test_stdout 2>$test_stderr";

  if (!$? && defined $error_expected)
  {
    ok(0,"Did not encounter an error executing the test when one was expected.\n\n");
    return;
  }

  if ($? && !defined $error_expected)
  {
    ok(0, "Encountered an error executing the test when one was not expected.\n" .
      "See $test_stdout and $test_stderr.\n\n");
    return;
  }


  my $real_stdout = catfile('t','results',$stdout_file);
  my $real_stderr = catfile('t','results',$stderr_file);

  Do_Diff($test_stdout,$real_stdout);
  Do_Diff($test_stderr,$real_stderr);
}

# ---------------------------------------------------------------------------

sub SetSkip
{
  my %tests = %{ shift @_ };

  my %skip;

  foreach my $test (keys %tests)
  {
    $skip{$test} = 'Date::Manip not installed'
      unless Module_Installed('Date::Manip');
  }

  return %skip;
}

# ---------------------------------------------------------------------------