summaryrefslogtreecommitdiffstats
path: root/libkcal/tests/runtestcase.pl
blob: ce825479235bb8e865678161a9551b7ed439c61a (plain)
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
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/perl

#    This file is part of libkcal.
#
#    Copyright (c) 2003 Cornelius Schumacher <[email protected]>
#    Copyright (C) 2005 Reinhold Kainhofer <[email protected]>
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Library General Public
#    License as published by the Free Software Foundation; either
#    version 2 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Library General Public License for more details.
#
#    You should have received a copy of the GNU Library General Public License
#    along with this library; see the file COPYING.LIB.  If not, write to
#    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#    Boston, MA 02110-1301, USA.

# This little script runs a test program on a given (calendar) file and 
# compares the output to a reference file. All discrepancies are shown 
# to the user. Usage:
#      runtestcase.pl appname identifier testfile.ics
# 'identifier' is used as a suffix to allow multiple tests on the same input 
# file during a test run.
#
# The application/script appname is required to take two arguments:
#      appname inputfile outputfile
# where inputfile is the file to be used as input data, and the output of the
# program will go to outputfile (=testfile.ics.identifier.out if called through 
# runtestcase.pl). That outputfile is then compared to the reference file
# testfile.ics.ref.


if ( @ARGV != 3 ) {
  print STDERR "Missing arg! Arguments: testapp identifier filename \n";
  exit 1;
}

$app = $ARGV[0];
$id = $ARGV[1];
$file = $ARGV[2];

$MAXERRLINES=25;

$file =~ /^(.*)\.[^\.]*$/;

my $outfile = $file;
$outfile =~ /\/([^\/]*)$/;
$outfile = "$file.$id.out";

$cmd = "./$app $file $outfile 2> /dev/null";

#print "CMD $cmd\n";

if ( system( $cmd ) != 0 ) {
  print STDERR "Error running $app\n";
  exit 1;
}

chectdefile( $file, $outfile );

exit 0;

sub chectdefile()
{
  my $file = shift;
  my $outfile = shift;

  my $logentry = "Checking '$outfile':\n";

  my @ref;
  if ( !open( REF, "$file.$id.ref" ) ) {
    print STDERR "Unable to open $file.$id.ref\n";
    exit 1;
  }
  while( <REF> ) {
    push @ref, $_ if($_ !~ m/^\s*$/);  #skip blank lines in the ref
  }
  close REF;

  if ( !open( READ, $outfile ) ) {
    print STDERR "Unable to open $outfile\n";
    exit 1;
  }

  $error = 0;
  $i = 0;
  $line = 0;
  my $errorlines = 0;
  while( <READ> ) {
    next if ($_ =~ m/^\s*$/); #skip blank lines in the output
    $out = $_;
    $ref = @ref[$i++];
    $line++;

    $out =~ s/\s*$//; #remove trailing whitespace
    $ref =~ s/\s*$//; #remove trailing whitespace
    # DTSTAMP, LAST-MODIFIED and CREATED might be different to the reference...
    if ( $out =~ /^DTSTAMP:[0-9ZT]+\r?$/ && $ref =~ /^DTSTAMP:[0-9ZT]+\r?$/ ) {
      next;
    }

    if ( $out =~ /^LAST-MODIFIED:[0-9ZT]+\r?$/ && $ref =~ /^LAST-MODIFIED:[0-9ZT]+\r?$/ ) {
      next;
    }

    if ( $out =~ /^CREATED:[0-9ZT]+\r?$/ && $ref =~ /^CREATED:[0-9ZT]+\r?$/ ) {
      next;
    }

    if ( $out ne $ref ) {
      if ( $errorlines == 0 ) {
        print $logentry;
      }
      $errorlines++;
      $error++;
      if ( $errorlines < $MAXERRLINES ) {
        print "  Line $line: Expected      : $ref";
        print "  Line $line: Actual output : $out";
      } elsif ( $errorlines == $MAXERRLINES ) {
        print "  <Remaining error suppressed>\n";
      }
    }
  }

  close READ;

  if ( $error > 0 ) {
    if ( -e "$file.$id.fixme" ) {
      if ( !open( FIXME, "$file.$id.fixme" ) ) {
        print STDERR "Unable to open $file.fixme\n";
        exit 1;
      }
      my $firstline = <FIXME>;
      $firstline =~ /^(\d+) known errors/;
      my $expected = $1;
      if ( $expected == $error ) {
        print "\n  EXPECTED FAIL: $error errors found.\n";
        print "    Fixme:\n";
        while( <FIXME> ) {
          print "      ";
          print;
        }
      } else {
        print "\n  UNEXPECTED FAIL: $error errors found, $expected expected.\n";
        exit 1;
      }
    } else {
      print "\n  FAILED: $error errors found.\n";
      if ( $error > 5 ) {
        system( "diff -u $file.$id.ref $outfile" );
      }
      system( "touch FAILED" );
      exit 1;
    }
  } else {
    unlink($outfile);
#    print "  OK\n";
  }
}