Debian Codegraph @

test/MarkdownTest_1.0/MarkdownTest.pl

raw
1 #!/usr/bin/perl
2
3 #
4 # MarkdownTester -- Run tests for Markdown implementations
5 #
6 # Copyright (c) 2004 John Gruber
7 # <http://daringfireball.net/projects/markdown/>
8 #
9
10 use strict;
11 use warnings;
12 use Getopt::Long;
13 use Benchmark;
14
15 our $VERSION = '1.0';
16 # Mon 13 Dec 2004
17
18 my $time_start = new Benchmark;
19 my $test_dir = "Tests";
20 my $script = "./Markdown.pl";
21 my $use_tidy = 0;
22 my ($flag_version);
23
24 GetOptions (
25 "script=s" => \$script,
26 "testdir=s" => \$test_dir,
27 "tidy" => \$use_tidy,
28 "version" => \$flag_version,
29 );
30
31 if($flag_version) {
32 my $progname = $0;
33 $progname =~ s{.*/}{};
34 die "$progname version $VERSION\n";
35 }
36
37 unless (-d $test_dir) { die "'$test_dir' is not a directory.\n"; }
38 unless (-f $script) { die "$script does not exist.\n"; }
39 unless (-x $script) { die "$script is not executable.\n"; }
40
41 my $tests_passed = 0;
42 my $tests_failed = 0;
43
44 foreach my $testfile (glob "$test_dir/*.text") {
45 my $testname = $testfile;
46 $testname =~ s{.*/(.+)\.text$}{$1}i;
47 print "$testname ... ";
48
49 # Look for a corresponding .html file for each .text file:
50 my $resultfile = $testfile;
51 $resultfile =~ s{\.text$}{\.html}i;
52 unless (-f $resultfile) {die "'$resultfile' does not exist.\n";}
53
54 # open(TEST, $testfile) || die("Can't open testfile: $!");
55 open(RESULT, $resultfile) || die("Can't open resultfile: $!");
56 undef $/;
57 # my $t_input = <TEST>;
58 my $t_result = <RESULT>;
59
60 my $t_output = `$script '$testfile'`;
61
62 # Normalize the output and expected result strings:
63 $t_result =~ s/\s+\z//; # trim trailing whitespace
64 $t_output =~ s/\s+\z//; # trim trailing whitespace
65 if ($use_tidy) {
66 # Escape the strings, pass them through to CLI tidy tool for tag-level equivalency
67 $t_result =~ s{'}{'\\''}g; # escape ' chars for shell
68 $t_output =~ s{'}{'\\''}g;
69 $t_result = `echo '$t_result' | tidy --show-body-only 1 --quiet 1 --show-warnings 0`;
70 $t_output = `echo '$t_output' | tidy --show-body-only 1 --quiet 1 --show-warnings 0`;
71 }
72
73 if ($t_output eq $t_result) {
74 print "OK\n";
75 $tests_passed++;
76 }
77 else {
78 print "FAILED\n\n";
79 $tests_failed++;
80 }
81 }
82
83 print "\n\n";
84 print "$tests_passed passed; $tests_failed failed.\n";
85
86 my $time_end = new Benchmark;
87 my $time_diff = timediff($time_end, $time_start);
88 print "Benchmark: ", timestr($time_diff), "\n";
89
90
91 __END__
92
93 =pod
94
95 =head1 NAME
96
97 B<MarkdownTest>
98
99
100 =head1 SYNOPSIS
101
102 B<MarkdownTest.pl> [ B<--options> ] [ I<file> ... ]
103
104
105 =head1 DESCRIPTION
106
107
108 =head1 OPTIONS
109
110 Use "--" to end switch parsing. For example, to open a file named "-z", use:
111
112 MarkdownTest.pl -- -z
113
114 =over 4
115
116 =item B<--script>
117
118 Specify the path to the Markdown script to test. Defaults to
119 "./Markdown.pl". Example:
120
121 ./MarkdownTest.pl --script ./PHP-Markdown/php-markdown
122
123 =item B<--testdir>
124
125 Specify the path to a directory containing test data. Defaults to "Tests".
126
127 =item B<--tidy>
128
129 Flag to turn on using the command line 'tidy' tool to normalize HTML
130 output before comparing script output to the expected test result.
131 Assumes that the 'tidy' command is available in your PATH. Defaults to
132 off.
133
134 =back
135
136
137
138 =head1 BUGS
139
140
141
142 =head1 VERSION HISTORY
143
144 1.0 Mon 13 Dec 2004
145
146
147
148 =head1 COPYRIGHT AND LICENSE
149
150 Copyright (c) 2004 John Gruber
151 <http://daringfireball.net/>
152 All rights reserved.
153
154 This is free software; you may redistribute it and/or modify it under
155 the same terms as Perl itself.
156
157 =cut