MarkdownTest.pl 3.4 KB

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