aboutsummaryrefslogtreecommitdiff
path: root/runtests.pl
blob: e53938d170f497a30166ebb954e931f17429d680 (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. if ($inpre) {
  37. print $outfh $_;
  38. } else {
  39. # remove leading spaces
  40. s/^ *//;
  41. # remove trailing spaces
  42. s/ *$//;
  43. # collapse consecutive spaces
  44. s/ */ /;
  45. # collapse space before /> in tag
  46. s/ *\/>/\/>/;
  47. s/>\n$/>/;
  48. # skip blank line
  49. if (/^$/) {
  50. next;
  51. }
  52. print $outfh $_;
  53. }
  54. }
  55. close $outfh;
  56. return $out;
  57. }
  58. sub dotest
  59. {
  60. my $markdown = $_[0];
  61. my $html = $_[1];
  62. my $testname = $_[2];
  63. my $actual = "";
  64. # We use → to indicate tab and ␣ space in the spec
  65. $markdown =~ s/→/\t/g;s/␣/ /g;
  66. $html =~ s/→/\t/g;s/␣/ /g;
  67. open2(my $out, my $in, @PROG);
  68. print $in $markdown;
  69. close $in;
  70. flush $out;
  71. $actual = do { local $/; <$out>; };
  72. close $out;
  73. $html = &tidy($html);
  74. $actual = &tidy($actual);
  75. $actual =~ s/\&#39;/'/;
  76. if ($actual eq $html) {
  77. print colored("✓", "green");
  78. return 1;
  79. } else {
  80. print colored("\n$testname", "red");
  81. print "\n";
  82. print color "cyan";
  83. print "=== markdown ===============\n";
  84. print $markdown;
  85. print "=== expected ===============\n";
  86. print $html;
  87. print "\n";
  88. print "=== got ====================\n";
  89. print $actual;
  90. print "\n";
  91. print color "black";
  92. return 0;
  93. }
  94. }
  95. my $stage = 0;
  96. my $markdown = "";
  97. my $html = "";
  98. my $example = 0;
  99. my $linenum = 0;
  100. my $exampleline = 0;
  101. my @secnums = ();
  102. my $secheading;
  103. open(SPEC, "< $SPEC");
  104. while (<SPEC>) {
  105. $linenum++;
  106. if (/^\.$/) {
  107. $stage = ($stage + 1) % 3;
  108. if ($stage == 1) {
  109. $exampleline = $linenum;
  110. }
  111. if ($stage == 0) {
  112. $example++;
  113. if (!$PATT || $secheading =~ /$PATT/) {
  114. if (&dotest($markdown, $html,
  115. "Example $example (line $exampleline)")) {
  116. $passed++;
  117. } else {
  118. $failed++;
  119. }
  120. } else {
  121. $skipped++;
  122. }
  123. $markdown = "";
  124. $html = "";
  125. }
  126. } elsif ($stage == 0 && $_ =~ /^<!-- END TESTS -->/) {
  127. last;
  128. } elsif ($stage == 0 && $_ =~ /^(#+) +(.*)/) {
  129. my $seclevel = length($1);
  130. $secheading = $2;
  131. if ($#secnums == $seclevel - 1) {
  132. $secnums[$#secnums]++;
  133. } elsif ($#secnums > $seclevel - 1) {
  134. @secnums = @secnums[0..($seclevel - 1)];
  135. $secnums[$#secnums]++;
  136. } else {
  137. while ($#secnums < $seclevel - 1) {
  138. push(@secnums, 1);
  139. }
  140. }
  141. if (!$PATT || $secheading =~ /$PATT/) {
  142. print ("\n", join(".", @secnums) . " " . $secheading, " ");
  143. }
  144. } elsif ($stage == 1) {
  145. $markdown .= $_;
  146. } elsif ($stage == 2) {
  147. $html .= $_;
  148. }
  149. }
  150. print "\n";
  151. print STDERR colored("$passed tests passed, $failed failed, $skipped skipped.", "bold");
  152. print STDERR "\n";
  153. exit $failed;