3 #=======================================================================
5 # File ID: 8c064bc0-1463-11de-b31f-000475e441b9
7 # Test suite for sortxml(1).
10 # ©opyleft 2009– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
19 use Test
::More
qw{no_plan
};
27 our $CMD_BASENAME = "sortxml";
28 our $CMD = "../$CMD_BASENAME";
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = '0.0.0';
45 my %descriptions = ();
47 Getopt
::Long
::Configure
('bundling');
50 'all|a' => \
$Opt{'all'},
51 'help|h' => \
$Opt{'help'},
52 'quiet|q+' => \
$Opt{'quiet'},
53 'todo|t' => \
$Opt{'todo'},
54 'verbose|v+' => \
$Opt{'verbose'},
55 'version' => \
$Opt{'version'},
57 ) || die("$progname: Option error. Use -h for help.\n");
59 $Opt{'verbose'} -= $Opt{'quiet'};
60 $Opt{'help'} && usage
(0);
61 if ($Opt{'version'}) {
72 diag
(sprintf('========== Executing %s v%s ==========',
73 $progname, $VERSION));
75 if ($Opt{'todo'} && !$Opt{'all'}) {
81 testcmd("$CMD command", # {{{
94 diag
('Testing -h (--help) option...');
95 likecmd
("$CMD -h", # {{{
99 'Option -h prints help screen',
103 diag
('Testing -v (--verbose) option...');
104 likecmd
("$CMD -hv", # {{{
105 '/^\n\S+ \d+\.\d+\.\d+/s',
108 'Option -v with -h returns version number and help screen',
112 diag
('Testing --version option...');
113 likecmd
("$CMD --version", # {{{
114 '/^\S+ \d+\.\d+\.\d+/',
117 'Option --version returns version number',
121 diag
("Test sorting...");
122 testcmd
("$CMD -s b sortxml-files/a.xml", # {{{
124 <?xml version="1.0" encoding="UTF-8"?>
127 <!ELEMENT b (c , d?)>
128 <!ELEMENT c (#PCDATA)>
129 <!ELEMENT d (#PCDATA)>
165 "Sorting XML document",
169 testcmd
("$CMD -s b sortxml-files/oneliners.xml", # {{{
171 <?xml version="1.0" encoding="UTF-8"?>
174 <!ELEMENT b (c , d?)>
175 <!ELEMENT c (#PCDATA)>
176 <!ELEMENT d (#PCDATA)>
179 <b> <c>abc</c> <d>dsfv</d> </b>
182 <b> <c>bbb</c> <d>gurgle</d> </b>
183 <b> <c>ggg</c> <d>fgh</d> </b>
184 <b> <c>ggg</c> <d>pdfg</d> </b>
190 "XML uses oneliners",
194 diag
("Test reverse sorting...");
195 testcmd
("$CMD -s b -r sortxml-files/a.xml", # {{{
197 <?xml version="1.0" encoding="UTF-8"?>
200 <!ELEMENT b (c , d?)>
201 <!ELEMENT c (#PCDATA)>
202 <!ELEMENT d (#PCDATA)>
238 "Reverse sort XML document",
242 testcmd
("$CMD -s b -r sortxml-files/oneliners.xml", # {{{
244 <?xml version="1.0" encoding="UTF-8"?>
247 <!ELEMENT b (c , d?)>
248 <!ELEMENT c (#PCDATA)>
249 <!ELEMENT d (#PCDATA)>
253 <b> <c>ggg</c> <d>pdfg</d> </b>
254 <b> <c>ggg</c> <d>fgh</d> </b>
255 <b> <c>bbb</c> <d>gurgle</d> </b>
258 <b> <c>abc</c> <d>dsfv</d> </b>
263 "Reverse sort onelined XML",
267 diag
('Testing -u (--unique) option...');
268 testcmd
("$CMD -s b -u sortxml-files/a.xml", # {{{
270 <?xml version="1.0" encoding="UTF-8"?>
273 <!ELEMENT b (c , d?)>
274 <!ELEMENT c (#PCDATA)>
275 <!ELEMENT d (#PCDATA)>
307 "Duplicated <b> removed",
315 if ($Opt{'all'} || $Opt{'todo'}) {
316 diag
('Running TODO tests...'); # {{{
321 # Insert TODO tests here.
327 diag
('Testing finished.');
334 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
335 defined($descriptions{$Desc}) &&
336 BAIL_OUT
("testcmd(): '$Desc' description is used twice");
337 $descriptions{$Desc} = 1;
339 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
340 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
341 my $TMP_STDERR = "$CMD_BASENAME-stderr.tmp";
344 if (defined($Exp_stderr)) {
345 $stderr_cmd = " 2>$TMP_STDERR";
347 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
349 if (defined($Exp_stderr)) {
350 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
353 diag
("Warning: stderr not defined for '$Txt'");
355 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
363 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
364 defined($descriptions{$Desc}) &&
365 BAIL_OUT
("likecmd(): '$Desc' description is used twice");
366 $descriptions{$Desc} = 1;
368 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
369 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
370 my $TMP_STDERR = "$CMD_BASENAME-stderr.tmp";
373 if (defined($Exp_stderr)) {
374 $stderr_cmd = " 2>$TMP_STDERR";
376 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
378 if (defined($Exp_stderr)) {
379 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
382 diag
("Warning: stderr not defined for '$Txt'");
384 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
391 # Return file content as a string {{{
395 open(my $fp, '<', $File) or return undef;
404 # Print program version {{{
405 print("$progname $VERSION\n");
411 # Send the help message to stdout {{{
414 if ($Opt{'verbose'}) {
420 Usage: $progname [options]
422 Contains tests for the $CMD_BASENAME(1) program.
427 Run all tests, also TODOs.
431 Be more quiet. Can be repeated to increase silence.
433 Run only the TODO tests.
435 Increase level of verbosity. Can be repeated.
437 Print version information.
445 # Print a status message to stderr based on verbosity level {{{
446 my ($verbose_level, $Txt) = @_;
448 $verbose_level > $Opt{'verbose'} && return;
449 print(STDERR
"$progname: $Txt\n");
456 # This program is free software; you can redistribute it and/or modify
457 # it under the terms of the GNU General Public License as published by
458 # the Free Software Foundation; either version 2 of the License, or (at
459 # your option) any later version.
461 # This program is distributed in the hope that it will be useful, but
462 # WITHOUT ANY WARRANTY; without even the implied warranty of
463 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
464 # See the GNU General Public License for more details.
466 # You should have received a copy of the GNU General Public License
467 # along with this program.
468 # If not, see L<http://www.gnu.org/licenses/>.
470 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :