aboutsummaryrefslogtreecommitdiff
path: root/runtests.pl
blob: 2d80f144251c8e1efde253201817368fdbd4f074 (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. # Disable ANSI colors if we're not hooked up to a terminal
  16. $ENV{ANSI_COLORS_DISABLED} ||= !-t *STDOUT;
  17. my $passed = 0;
  18. my $failed = 0;
  19. my $skipped = 0;
  20. # Markdown implementations vary on insignificant whitespace.
  21. # Some leave blanks between block elements, others don't.
  22. # This function tries to normalize the output so it can be
  23. # compared with our test. tidy takes two arguments: the
  24. # string containing the actual output, and a pathname of the
  25. # file to which the tidied output is to be saved.
  26. sub tidy
  27. {
  28. my $inpre = 0;
  29. my $out = "";
  30. my $outfh;
  31. open($outfh, '>', \$out);
  32. for (split /^/, $_[0]) {
  33. if (/<pre/) {
  34. $inpre = 1;
  35. } elsif (/<\/pre/) {
  36. $inpre = 0;
  37. }
  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. # skip blank line
  50. if (/^$/) {
  51. next;
  52. }
  53. print $outfh $_;
  54. }
  55. }
  56. close $outfh;
  57. return $out;
  58. }
  59. sub dotest
  60. {
  61. my $markdown = $_[0];
  62. my $html = $_[1];
  63. my $testname = $_[2];
  64. my $actual = "";
  65. # We use → to indicate tab and ␣ space in the spec
  66. $markdown =~ s/→/\t/g;s/␣/ /g;
  67. $html =~ s/→/\t/g;s/␣/ /g;
  68. open2(my $out, my $in, @PROG);
  69. print $in $markdown;
  70. close $in;
  71. flush $out;
  72. $actual = do { local $/; <$out>; };
  73. close $out;
  74. $html = &tidy($html);
  75. $actual = &tidy($actual);
  76. $actual =~ s/\&#39;/'/;
  77. if ($actual eq $html) {
  78. print colored("✓", "green");
  79. return 1;
  80. } else {
  81. print colored("\n$testname", "red");
  82. print "\n";
  83. print color "cyan";
  84. print "=== markdown ===============\n";
  85. print $markdown;
  86. print "=== expected ===============\n";
  87. print $html;
  88. print "=== got ====================\n";
  89. print $actual;
  90. print color "black";
  91. return 0;
  92. }
  93. }
  94. my $stage = 0;
  95. my $markdown = "";
  96. my $html = "";
  97. my $example = 0;
  98. my $linenum = 0;
  99. my $exampleline = 0;
  100. my @secnums = ();
  101. my $secheading;
  102. open(SPEC, "< $SPEC");
  103. while (<SPEC>) {
  104. $linenum++;
  105. if (/^\.$/) {
  106. $stage = ($stage + 1) % 3;
  107. if ($stage == 1) {
  108. $exampleline = $linenum;
  109. }
  110. if ($stage == 0) {
  111. $example++;
  112. if (!$PATT || $secheading =~ /$PATT/) {
  113. if (&dotest($markdown, $html,
  114. "Example $example (line $exampleline)")) {
  115. $passed++;
  116. } else {
  117. $failed++;
  118. }
  119. } else {
  120. $skipped++;
  121. }
  122. $markdown = "";
  123. $html = "";
  124. }
  125. } elsif ($stage == 0 && $_ =~ /^<!-- END TESTS -->/) {
  126. last;
  127. } elsif ($stage == 0 && $_ =~ /^(#+) +(.*)/) {
  128. my $seclevel = length($1);
  129. $secheading = $2;
  130. if ($#secnums == $seclevel - 1) {
  131. $secnums[$#secnums]++;
  132. } elsif ($#secnums > $seclevel - 1) {
  133. @secnums = @secnums[0..($seclevel - 1)];
  134. $secnums[$#secnums]++;
  135. } else {
  136. while ($#secnums < $seclevel - 1) {
  137. push(@secnums, 1);
  138. }
  139. }
  140. if (!$PATT || $secheading =~ /$PATT/) {
  141. print ("\n", join(".", @secnums) . " " . $secheading, " ");
  142. }
  143. } elsif ($stage == 1) {
  144. $markdown .= $_;
  145. } elsif ($stage == 2) {
  146. $html .= $_;
  147. }
  148. }
  149. print "\n";
  150. print STDERR colored("$passed tests passed, $failed failed, $skipped skipped.", "bold");
  151. print STDERR "\n";
  152. exit $failed;