Commit 62826b59 authored by Morten Welinder's avatar Morten Welinder

New test framework.

parent 05b20966
Makefile.in
Makefile
.deps
.libs
import-db
package GnumericTest;
use strict;
use Exporter;
use File::Basename qw(fileparse);
@GnumericTest::ISA = qw (Exporter);
@GnumericTest::EXPORT = qw(test_sheet_calc test_importer $samples);
$GnumericTest::samples = "../samples";
my $ssconvert = "../src/ssconvert";
# -----------------------------------------------------------------------------
my @tempfiles;
END {
unlink @tempfiles;
}
sub junkfile {
my ($fn) = @_;
push @tempfiles, $fn;
}
sub removejunk {
my ($fn) = @_;
unlink $fn;
if (@tempfiles && $fn eq $tempfiles[-1]) {
scalar (pop @tempfiles);
}
}
# -----------------------------------------------------------------------------
sub system_failure {
my ($program,$code) = @_;
if ($code == -1) {
die "failed to run $program: $!\n";
} elsif ($code >> 8) {
my $sig = $code >> 8;
die "$program died due to signal $sig\n";
} else {
die "$program exited with exit code $code\n";
}
}
sub read_file {
my ($fn) = @_;
local (*FIL);
open (FIL, $fn) or die "Cannot open $fn: $!\n";
local $/ = undef;
my $lines = <FIL>;
close FIL;
return $lines;
}
sub write_file {
my ($fn,$contents) = @_;
local (*FIL);
open (FIL, ">$fn.tmp") or die "Cannot create $fn.tmp: $!\n";
print FIL $contents;
close FIL;
rename "$fn.tmp", $fn;
}
sub update_file {
my ($fn,$contents) = @_;
my @stat = stat $fn;
die "Cannot stat $fn: $!\n" unless @stat > 2;
&write_file ($fn,$contents);
chmod $stat[2], $fn or
die "Cannot chmod $fn: $!\n";
}
# -----------------------------------------------------------------------------
sub test_sheet_calc {
my ($file,$range,$expected) = @_;
my $tmp = fileparse ($file);
$tmp =~ s/\.[a-zA-Z0-9]+$/.csv/;
&junkfile ($tmp);
my $code = system ("$ssconvert --recalc --export-range='$range' '$file' '$tmp' 2>&1 | sed -e 's/^/| /'");
&system_failure ($ssconvert, $code) if $code;
my $actual = &read_file ($tmp);
my $ok;
if (ref $expected) {
local $_ = $actual;
$ok = &$expected ($_);
} else {
$ok = ($actual eq $expected);
}
&removejunk ($tmp);
if ($ok) {
print STDERR "Pass\n";
} else {
die "Fail.\n$actual\n";
}
}
# -----------------------------------------------------------------------------
my $import_db = 'import-db';
# Modes:
# check: check that conversion produces right file
# create-db: save the current corresponding .gnumeric
# diff: diff conversion against saved .gnumeric
# update-SHA-1: update $0 to show current SHA-1 [validate first!]
sub test_importer {
my ($file,$sha1,$mode) = @_;
my $tmp = fileparse ($file);
($tmp =~ s/\.[a-zA-Z0-9]+$/.gnumeric/ ) or ($tmp .= '.gnumeric');
if ($mode eq 'create-db') {
-d $import_db or mkdir ($import_db, 0777) or
die "Cannot create $import_db: $!\n";
$tmp = "$import_db/$tmp";
} else {
&junkfile ($tmp);
}
my $code = system ("$ssconvert '$file' '$tmp' 2>&1 | sed -e 's/^/| /'");
&system_failure ($ssconvert, $code) if $code;
my $newsha1 = lc substr (`gzip -dc '$tmp' | sha1sum`, 0, 40);
die "SHA-1 failure\n" unless $newsha1 =~ /^[0-9a-f]{40}$/;
if ($mode eq 'check') {
if ($sha1 ne $newsha1) {
die "New SHA-1 is $newsha1; expected was $sha1\n";
}
} elsif ($mode eq 'create-db') {
if ($sha1 ne $newsha1) {
warn ("New SHA-1 is $newsha1; expected was $sha1\n");
}
# No file to remove
return;
} elsif ($mode eq 'diff') {
my $saved = "$import_db/$tmp";
die "$saved not found\n" unless -r $saved;
my $tmp1 = "$tmp-old";
&junkfile ($tmp1);
my $code1 = system ("gzip -dc '$saved' >'$tmp1'");
&system_failure ('gzip', $code1) if $code1;
my $tmp2 = "$tmp-new";
&junkfile ($tmp2);
my $code2 = system ("gzip -dc '$tmp' >'$tmp2'");
&system_failure ('gzip', $code2) if $code2;
my $code3 = system ('diff', @ARGV, $tmp1, $tmp2);
&removejunk ($tmp2);
&removejunk ($tmp1);
} elsif ($mode =~ /^update-(sha|SHA)-?1/) {
if ($sha1 ne $newsha1) {
my $script = &read_file ($0);
my $count = ($script =~ s/\b$sha1\b/$newsha1/g);
die "SHA-1 found in script $count times\n" unless $count == 1;
&update_file ($0, $script);
}
return;
} else {
die "Invalid mode \"$mode\"\n";
}
&removejunk ($tmp);
}
# -----------------------------------------------------------------------------
# Setup a consistent environment
delete $ENV{'LANG'};
foreach (keys %ENV) { delete $ENV{$_} if /^LC_/; }
$ENV{'LC_ALL'} = 'C';
1;
# First digit after is the type of test:
#
# 1xxx: Evaluation
# 10xx: Large groups of functions
# 18xx: Operators
# 19xx: Evaluation modes
#
# 5xxx: Importers
# 59xx: Minor formats
TESTS = $(wildcard t[0-9][0-9][0-9][0-9]-*.pl)
testsuite: $(TESTS)
importdb:
for t in t5[0-9][0-9][0-9]-*.pl; do @echo "*** $$t ***"; $(PERL) $$t create-db; done
$(TESTS):
@echo "*** $@ ***" && $(PERL) $@
EXTRA_DIST = GnumericTest.pm; $(TESTS)
.PHONY: testsuite importdb $(TESTS) clean
.NOTPARALLEL:
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
&test_sheet_calc ("$samples/excel/statfuns.xls", "A4", sub { /All ok/i });
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
&test_sheet_calc ("$samples/excel/mathfuns.xls", "A4", sub { /All ok/i });
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
&test_sheet_calc ("$samples/excel/finfuns.xls", "A4", sub { /All ok/i });
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
&test_sheet_calc ("$samples/excel/engfuns.xls", "A4", sub { /All ok/i });
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
&test_sheet_calc ("$samples/excel/textfuns.xls", "A4", sub { /All ok/i });
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
&test_sheet_calc ("$samples/excel/logfuns.xls", "A4", sub { /All ok/i });
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
&test_sheet_calc ("$samples/excel/datefuns.xls", "A4", sub { /All ok/i });
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
&test_sheet_calc ("$samples/excel/dbfuns.xls", "A4", sub { /All ok/i });
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
&test_sheet_calc ("$samples/excel/infofuns.xls", "A4", sub { /All ok/i });
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
&test_sheet_calc ("$samples/excel/lookfuns.xls", "A4", sub { /All ok/i });
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
&test_sheet_calc ("$samples/excel/bitwise.xls", "B3:B99", sub { /^(\s*Success)+\s*$/i });
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
&test_sheet_calc ("$samples/excel/operator.xls", "C1:C15", sub { /^(\s*Success)+\s*$/i });
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
&test_sheet_calc ("$samples/arrays.xls", "B1", sub { /TRUE/ });
#!/usr/bin/perl -w
# -----------------------------------------------------------------------------
use strict;
use lib ($0 =~ m|^(.*/)| ? $1 : ".");
use GnumericTest;
my $mode = ((shift @ARGV) || "check");
&test_importer ("$samples/sc/demo_func", "744772546766f80878879d56a329abfdd95d7c4c", $mode);
&test_importer ("$samples/sc/demo_math", "b85dcaf33edd349f20f3d2c71329ff994bc99dca", $mode);
Markdown is supported
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