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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
#!/usr/bin/perl -w
########################################################################
#
# The tests below are a subset of tests taken from XML::Writer to ensure
# XMLsimpleWriter complies with the basic functionality of that module.
#
# I'm maintaining the same structure and methodology so that I can
# borrow more tests if required. Original documentation and copyrights
# below.
#
# John McNamara 2011.
#
########################################################################
# test.pl - test script for XML::Writer module.
# Copyright (c) 1999 by Megginson Technologies.
# Copyright (c) 2003 Ed Avis <ed@membled.com>
# Copyright (c) 2004-2010 Joseph Walton <joe@kafsemo.org>
# Redistribution and use in source and compiled forms, with or without
# modification, are permitted under any circumstances. No warranty.
########################################################################
# Before 'make install' is performed this script should be runnable with
# 'make test'. After 'make install' it should work as 'perl 01_main.t'
use strict;
use Errno;
use Test::More(tests => 11);
# Catch warnings
my $warning;
$SIG{__WARN__} = sub {
($warning) = @_ unless ($warning);
};
sub wasNoWarning($)
{
my ($reason) = @_;
if (!ok(!$warning, $reason)) {
diag($warning);
}
}
# Constants for Unicode support
my $unicodeSkipMessage = 'Unicode only supported with Perl >= 5.8.1';
sub isUnicodeSupported()
{
return $] >= 5.008001;
}
require Excel::Writer::XLSX::Package::XMLwriterSimple;
TEST: {
wasNoWarning('Load without warnings');
}
use IO::File;
# The objest that will be used for testing.
my $w;
my $outputFile = IO::File->new_tmpfile or die "Unable to create temporary file: $!";
# Fetch the current contents of the scratch file as a scalar
sub getBufStr()
{
local($/);
binmode($outputFile, ':bytes') if isUnicodeSupported();
$outputFile->seek(0, 0);
return <$outputFile>;
}
# Set up the environment to run a test.
sub initEnv(@)
{
my (%args) = @_;
# Reset the scratch file
$outputFile->seek(0, 0);
$outputFile->truncate(0);
binmode($outputFile, ':raw') if $] >= 5.006;
# Overwrite OUTPUT so it goes to the scratch file
$args{'OUTPUT'} = $outputFile unless(defined($args{'OUTPUT'}));
# Set NAMESPACES, unless it's present
$args{'NAMESPACES'} = 1 unless(defined($args{'NAMESPACES'}));
undef($warning);
$w = new Excel::Writer::XLSX::Package::XMLwriterSimple($outputFile) || die "Cannot create XML writer";
}
#
# Check the results in the temporary output file.
#
# $expected - the exact output expected
#
sub checkResult($$)
{
my ($expected, $explanation) = (@_);
my $actual = getBufStr();
if ($expected eq $actual) {
ok(1, $explanation);
} else {
my @e = split(/\n/, $expected);
my @a = split(/\n/, $actual);
if (@e + @a == 2) {
is(getBufStr(), $expected, $explanation);
} else {
if (eval {require Algorithm::Diff;}) {
fail($explanation);
Algorithm::Diff::traverse_sequences( \@e, \@a, {
MATCH => sub { diag(" $e[$_[0]]\n"); },
DISCARD_A => sub { diag("-$e[$_[0]]\n"); },
DISCARD_B => sub { diag("+$a[$_[1]]\n"); }
});
} else {
fail($explanation);
diag(" got: '$actual'\n");
diag(" expected: '$expected'\n");
}
}
}
wasNoWarning('(no warnings)');
}
#
# Expect an error of some sort, and check that the message matches.
#
# $pattern - a regular expression that must match the error message
# $value - the return value from an eval{} block
#
sub expectError($$) {
my ($pattern, $value) = (@_);
if (!ok((!defined($value) and ($@ =~ $pattern)), "Error expected: $pattern"))
{
diag('Actual error:');
if ($@) {
diag($@);
} else {
diag('(no error)');
diag(getBufStr());
}
}
}
# Empty element tag.
TEST: {
initEnv();
$w->emptyTag("foo");
$w->end();
checkResult("<foo />\n", 'An empty element tag');
};
# Empty element tag with XML decl.
TEST: {
initEnv();
$w->xmlDecl();
$w->emptyTag("foo");
$w->end();
checkResult(<<"EOS", 'Empty element tag with XML declaration');
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo />
EOS
};
# Start/end tag.
TEST: {
initEnv();
$w->startTag("foo");
$w->endTag("foo");
$w->end();
checkResult("<foo></foo>\n", 'A separate start and end tag');
};
# Attributes
TEST: {
initEnv();
$w->emptyTag("foo", "x" => "1>2");
$w->end();
checkResult("<foo x=\"1>2\" />\n", 'Simple attributes');
};
# Character data
TEST: {
initEnv();
$w->startTag("foo");
$w->characters("<tag>&</tag>");
$w->endTag("foo");
$w->end();
checkResult("<foo><tag>&amp;</tag></foo>\n", 'Escaped character data');
};
__END__
|