Debian Codegraph @

test/MarkdownTest_1.0.3/MarkdownTest.pl

raw
1 #!/usr/bin/perl
2
3 #
4 # MarkdownTester -- Run tests for Markdown implementations
5 #
6 # Copyright (c) 2004-2005 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.2';
16 # Sat 24 Dec 2005
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 TEST:
45 foreach my $testfile (glob "$test_dir/*.text") {
46 my $testname = $testfile;
47 $testname =~ s{.*/(.+)\.text$}{$1}i;
48 print "$testname ... ";
49
50 # Look for a corresponding .html file for each .text file:
51 my $resultfile = $testfile;
52 $resultfile =~ s{\.text$}{\.html}i;
53 unless (-f $resultfile) {
54 print "'$resultfile' does not exist.\n\n";
55 next TEST;
56 }
57
58 # open(TEST, $testfile) || die("Can't open testfile: $!");
59 open(RESULT, $resultfile) || die("Can't open resultfile: $!");
60 undef $/;
61 # my $t_input = <TEST>;
62 my $t_result = <RESULT>;
63
64 my $t_output = `'$script' '$testfile'`;
65
66 # Normalize the output and expected result strings:
67 $t_result =~ s/\s+\z//; # trim trailing whitespace
68 $t_output =~ s/\s+\z//; # trim trailing whitespace
69 if ($use_tidy) {
70 # Escape the strings, pass them through to CLI tidy tool for tag-level equivalency
71 $t_result =~ s{'}{'\\''}g; # escape ' chars for shell
72 $t_output =~ s{'}{'\\''}g;
73 $t_result = `echo '$t_result' | tidy --show-body-only 1 --quiet 1 --show-warnings 0`;
74 $t_output = `echo '$t_output' | tidy --show-body-only 1 --quiet 1 --show-warnings 0`;
75 }
76
77 if ($t_output eq $t_result) {
78 print "OK\n";
79 $tests_passed++;
80 }
81 else {
82 print "FAILED\n\n";
83 # This part added by JM to print diffs
84 open(OUT, '>tmp1') or die $!;
85 print OUT $t_output or die $!;
86 open(RES, '>tmp2') or die $!;
87 print RES $t_result or die $!;
88 print `diff tmp1 tmp2`;
89 close RES;
90 close OUT;
91 print "\n";
92 `rm tmp?`;
93 # End of added part
94 $tests_failed++;
95 }
96 }
97
98 print "\n\n";
99 print "$tests_passed passed; $tests_failed failed.\n";
100
101 my $time_end = new Benchmark;
102 my $time_diff = timediff($time_end, $time_start);
103 print "Benchmark: ", timestr($time_diff), "\n";
104
105
106 __END__
107
108 =pod
109
110 =head1 NAME
111
112 B<MarkdownTest>
113
114
115 =head1 SYNOPSIS
116
117 B<MarkdownTest.pl> [ B<--options> ] [ I<file> ... ]
118
119
120 =head1 DESCRIPTION
121
122
123 =head1 OPTIONS
124
125 Use "--" to end switch parsing. For example, to open a file named "-z", use:
126
127 MarkdownTest.pl -- -z
128
129 =over 4
130
131 =item B<--script>
132
133 Specify the path to the Markdown script to test. Defaults to
134 "./Markdown.pl". Example:
135
136 ./MarkdownTest.pl --script ./PHP-Markdown/php-markdown
137
138 =item B<--testdir>
139
140 Specify the path to a directory containing test data. Defaults to "Tests".
141
142 =item B<--tidy>
143
144 Flag to turn on using the command line 'tidy' tool to normalize HTML
145 output before comparing script output to the expected test result.
146 Assumes that the 'tidy' command is available in your PATH. Defaults to
147 off.
148
149 =back
150
151
152
153 =head1 BUGS
154
155
156
157 =head1 VERSION HISTORY
158
159 1.0 Mon 13 Dec 2004-2005
160
161 1.0.1 Mon 19 Sep 2005
162
163 + Better handling of case when foo.text exists, but foo.html doesn't.
164 It now prints a message and moves on, rather than dying.
165
166
167 =head1 COPYRIGHT AND LICENSE
168
169 Copyright (c) 2004-2005 John Gruber
170 <http://daringfireball.net/>
171 All rights reserved.
172
173 This is free software; you may redistribute it and/or modify it under
174 the same terms as Perl itself.
175
176 =cut