aboutsummaryrefslogtreecommitdiff
path: root/runtests.pl
blob: ae1195e43faeebb04ff40754e59ce3bc017a9b16 (plain)
  1. #!/usr/bin/env perl
  2. use warnings;
  3. use strict;
  4. use Term::ANSIColor;
  5. use IO::Handle;
  6. use IPC::Open2;
  7. my $usage="runtests.pl SPEC PROGRAM\nSet ANSI_COLORS_DISABLED=1 if you redirect to a file.\nSet PATT='...' to restrict tests to sections matching a regex.\n";
  8. my $SPEC = shift @ARGV;
  9. my @PROG = @ARGV;
  10. my $PATT=$ENV{'PATT'};
  11. if (!(@PROG && defined $SPEC)) {
  12. print STDERR $usage;
  13. exit 1;
  14. }
  15. my $passed = 0;
  16. my $failed = 0;
  17. my $skipped = 0;
  18. # Markdown implementations vary on insignificant whitespace.
  19. # Some leave blanks between block elements, others don't.
  20. # This function tries to normalize the output so it can be
  21. # compared with our test. tidy takes two arguments: the
  22. # string containing the actual output, and a pathname of the
  23. # file to which the tidied output is to be saved.
  24. sub tidy
  25. {
  26. my $inpre = 0;
  27. my $out = "";
  28. my $outfh;
  29. open($outfh, '>', \$out);
  30. for (split /^/, $_[0]) {
  31. if (/<pre/) {
  32. $inpre = 1;
  33. } elsif (/<\/pre/) {
  34. $inpre = 0;
  35. }
  36. # remove \r to allow mixing linux/windows newlines
  37. s/\r//;
  38. if ($inpre) {
  39. print $outfh $_;
  40. } else {
  41. # remove leading spaces
  42. s/^ *//;
  43. # remove trailing spaces
  44. s/ *$//;
  45. # collapse consecutive spaces
  46. s/ */ /;
  47. # collapse space before /> in tag
  48. s/ *\/>/\/>/;
  49. s/>\n$/>/;
  50. # skip blank line
  51. if (/^$/) {
  52. next;
  53. }
  54. print $outfh $_;
  55. }
  56. }
  57. close $outfh;
  58. return $out;
  59. }
  60. sub dotest
  61. {
  62. my $markdown = $_[0];
  63. my $html = $_[1];
  64. my $testname = $_[2];
  65. my $actual = "";
  66. # We use → to indicate tab and ␣ space in the spec
  67. $markdown =~ s/→/\t/g;s/␣/ /g;
  68. $html =~ s/→/\t/g;s/␣/ /g;
  69. my $pid = open2(my $out, my $in, @PROG);
  70. print $in $markdown;
  71. close $in;
  72. flush $out;
  73. $actual = do { local $/; <$out>; };
  74. close $out;
  75. waitpid($pid, 0);
  76. $html = &tidy($html);
  77. $actual = &tidy($actual);
  78. $actual =~ s/\&#39;/'/g;
  79. if ($actual eq $html) {
  80. print colored("✓", "green");
  81. return 1;
  82. } else {
  83. print colored("\n$testname", "red");
  84. print "\n";
  85. print color "cyan";
  86. print "=== markdown ===============\n";
  87. print $markdown;
  88. print "=== expected ===============\n";
  89. print $html;
  90. print "\n";
  91. print "=== got ====================\n";
  92. print $actual;
  93. print "\n";
  94. print color "black";
  95. return 0;
  96. }
  97. }
  98. my $stage = 0;
  99. my $markdown = "";
  100. my $html = "";
  101. my $example = 0;
  102. my $linenum = 0;
  103. my $exampleline = 0;
  104. my @secnums = ();
  105. my $secheading;
  106. open(SPEC, "< $SPEC");
  107. while (<SPEC>) {
  108. $linenum++;
  109. if (/^\.$/) {
  110. $stage = ($stage + 1) % 3;
  111. if ($stage == 1) {
  112. $exampleline = $linenum;
  113. }
  114. if ($stage == 0) {
  115. $example++;
  116. if (!$PATT || $secheading =~ /$PATT/) {
  117. if (&dotest($markdown, $html,
  118. "Example $example (line $exampleline)")) {
  119. $passed++;
  120. } else {
  121. $failed++;
  122. }
  123. } else {
  124. $skipped++;
  125. }
  126. $markdown = "";
  127. $html = "";
  128. }
  129. } elsif ($stage == 0 && $_ =~ /^<!-- END TESTS -->/) {
  130. last;
  131. } elsif ($stage == 0 && $_ =~ /^(#+) +(.*)/) {
  132. my $seclevel = length($1);
  133. $secheading = $2;
  134. if ($#secnums == $seclevel - 1) {
  135. $secnums[$#secnums]++;
  136. } elsif ($#secnums > $seclevel - 1) {
  137. @secnums = @secnums[0..($seclevel - 1)];
  138. $secnums[$#secnums]++;
  139. } else {
  140. while ($#secnums < $seclevel - 1) {
  141. push(@secnums, 1);
  142. }
  143. }
  144. if (!$PATT || $secheading =~ /$PATT/) {
  145. print ("\n", join(".", @secnums) . " " . $secheading, " ");
  146. }
  147. } elsif ($stage == 1) {
  148. $markdown .= $_;
  149. } elsif ($stage == 2) {
  150. $html .= $_;
  151. }
  152. }
  153. print "\n";
  154. print STDERR colored("$passed tests passed, $failed failed, $skipped skipped.", "bold");
  155. print STDERR "\n";
  156. exit $failed;