Commit 359406bfb2d78bca2bcccfd88e47a466525b4b01

Martin Mitas 2024-01-16T14:25:46

Test: Add support for per-example command line options. (We also removed direct call support into the library. It was inherited from cmark as the testsuite was originally taken from there, but it actually was never updated to work with MD4C.)

diff --git a/test/cmark.py b/test/cmark.py
deleted file mode 100755
index 1110860..0000000
--- a/test/cmark.py
+++ /dev/null
@@ -1,40 +0,0 @@
-#!/usr/bin/env python3
-# -*- coding: utf-8 -*-
-
-from ctypes import CDLL, c_char_p, c_long
-from subprocess import *
-import platform
-import os
-
-def pipe_through_prog(prog, text):
-    p1 = Popen(prog.split(), stdout=PIPE, stdin=PIPE, stderr=PIPE)
-    [result, err] = p1.communicate(input=text.encode('utf-8'))
-    return [p1.returncode, result.decode('utf-8'), err]
-
-def use_library(lib, text):
-    textbytes = text.encode('utf-8')
-    textlen = len(textbytes)
-    return [0, lib(textbytes, textlen, 0).decode('utf-8'), '']
-
-class CMark:
-    def __init__(self, prog=None, library_dir=None):
-        self.prog = prog
-        if prog:
-            self.to_html = lambda x: pipe_through_prog(prog, x)
-        else:
-            sysname = platform.system()
-            if sysname == 'Darwin':
-                libname = "libcmark.dylib"
-            elif sysname == 'Windows':
-                libname = "cmark.dll"
-            else:
-                libname = "libcmark.so"
-            if library_dir:
-                libpath = os.path.join(library_dir, libname)
-            else:
-                libpath = os.path.join("build", "src", libname)
-            cmark = CDLL(libpath)
-            markdown = cmark.cmark_markdown_to_html
-            markdown.restype = c_char_p
-            markdown.argtypes = [c_char_p, c_long]
-            self.to_html = lambda x: use_library(markdown, x)
diff --git a/test/pathological_tests.py b/test/pathological_tests.py
index 76cb9df..78d77b9 100755
--- a/test/pathological_tests.py
+++ b/test/pathological_tests.py
@@ -5,19 +5,15 @@ import re
 import argparse
 import sys
 import platform
-from cmark import CMark
+from prog import Prog
 from timeit import default_timer as timer
 
 if __name__ == "__main__":
-    parser = argparse.ArgumentParser(description='Run cmark tests.')
+    parser = argparse.ArgumentParser(description='Run Markdown tests.')
     parser.add_argument('-p', '--program', dest='program', nargs='?', default=None,
             help='program to test')
-    parser.add_argument('--library-dir', dest='library_dir', nargs='?',
-            default=None, help='directory containing dynamic library')
     args = parser.parse_args(sys.argv[1:])
 
-cmark = CMark(prog=args.program, library_dir=args.library_dir)
-
 # list of pairs consisting of input and a regex that must match the output.
 pathological = {
     # note - some pythons have limit of 65535 for {num-matches} in re.
@@ -105,9 +101,15 @@ failed = 0
 
 #print("Testing pathological cases:")
 for description in pathological:
-    (inp, regex) = pathological[description]
+    if len(pathological[description]) == 2:
+        (inp, regex) = pathological[description]
+        prog = Prog(cmdline=args.program)
+    else:
+        (inp, regex, default_options) = pathological[description]
+        prog = Prog(cmdline=args.program, default_options=default_options)
+
     start = timer()
-    [rc, actual, err] = cmark.to_html(inp)
+    [rc, actual, err] = prog.to_html(inp)
     end = timer()
     if rc != 0:
         errored += 1
diff --git a/test/prog.py b/test/prog.py
new file mode 100644
index 0000000..fd28df9
--- /dev/null
+++ b/test/prog.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+from subprocess import *
+import platform
+import os
+
+def pipe_through_prog(argv, text):
+    p1 = Popen(argv, stdout=PIPE, stdin=PIPE, stderr=PIPE)
+    [result, err] = p1.communicate(input=text.encode('utf-8'))
+    return [p1.returncode, result.decode('utf-8'), err]
+
+class Prog:
+    def __init__(self, cmdline="md2html", default_options=[]):
+        self.cmdline = cmdline.split()
+        if len(self.cmdline) <= 1:
+            # cmdline provided no command line options. Use default ones.
+            if isinstance(default_options, str):
+                self.cmdline += default_options.split()
+            else:
+                self.cmdline += default_options
+        self.to_html = lambda x: pipe_through_prog(self.cmdline, x)
diff --git a/test/spec_tests.py b/test/spec_tests.py
index c739e5f..50f1252 100755
--- a/test/spec_tests.py
+++ b/test/spec_tests.py
@@ -6,19 +6,17 @@ from difflib import unified_diff
 import argparse
 import re
 import json
-from cmark import CMark
+from prog import Prog
 from normalize import normalize_html
 
 if __name__ == "__main__":
-    parser = argparse.ArgumentParser(description='Run cmark tests.')
+    parser = argparse.ArgumentParser(description='Run Markdown tests.')
     parser.add_argument('-p', '--program', dest='program', nargs='?', default=None,
             help='program to test')
     parser.add_argument('-s', '--spec', dest='spec', nargs='?', default='spec.txt',
             help='path to spec')
     parser.add_argument('-P', '--pattern', dest='pattern', nargs='?',
             default=None, help='limit to sections matching regex pattern')
-    parser.add_argument('--library-dir', dest='library_dir', nargs='?',
-            default=None, help='directory containing dynamic library')
     parser.add_argument('--no-normalize', dest='normalize',
             action='store_const', const=False, default=True,
             help='do not normalize HTML')
@@ -39,7 +37,8 @@ def print_test_header(headertext, example_number, start_line, end_line):
     out("Example %d (lines %d-%d) %s\n" % (example_number,start_line,end_line,headertext))
 
 def do_test(test, normalize, result_counts):
-    [retcode, actual_html, err] = cmark.to_html(test['markdown'])
+    prog = Prog(cmdline=args.program, default_options=test['cmdline_options'])
+    [retcode, actual_html, err] = prog.to_html(test['markdown'])
     if retcode == 0:
         expected_html = test['html']
         unicode_error = None
@@ -81,6 +80,7 @@ def get_tests(specfile):
     example_number = 0
     markdown_lines = []
     html_lines = []
+    cmdline_lines = []
     state = 0  # 0 regular text, 1 markdown example, 2 html output
     headertext = ''
     tests = []
@@ -91,16 +91,16 @@ def get_tests(specfile):
         for line in specf:
             line_number = line_number + 1
             l = line.strip()
-            #if l == "`" * 32 + " example":
             if re.match("`{32} example( [a-z]{1,})?", l):
                 state = 1
-            elif state == 2 and l == "`" * 32:
+            elif state >= 2 and l == "`" * 32:
                 state = 0
                 example_number = example_number + 1
                 end_line = line_number
                 tests.append({
                     "markdown":''.join(markdown_lines).replace('→',"\t"),
                     "html":''.join(html_lines).replace('→',"\t"),
+                    "cmdline_options":''.join(cmdline_lines),
                     "example": example_number,
                     "start_line": start_line,
                     "end_line": end_line,
@@ -108,14 +108,17 @@ def get_tests(specfile):
                 start_line = 0
                 markdown_lines = []
                 html_lines = []
+                cmdline_lines = []
             elif l == ".":
-                state = 2
+                state += 1
             elif state == 1:
                 if start_line == 0:
                     start_line = line_number - 1
                 markdown_lines.append(line)
             elif state == 2:
                 html_lines.append(line)
+            elif state == 3:
+                cmdline_lines.append(line)
             elif state == 0 and re.match(header_re, line):
                 headertext = header_re.sub('', line).strip()
     return tests
@@ -136,7 +139,6 @@ if __name__ == "__main__":
         exit(0)
     else:
         skipped = len(all_tests) - len(tests)
-        cmark = CMark(prog=args.program, library_dir=args.library_dir)
         result_counts = {'pass': 0, 'fail': 0, 'error': 0, 'skip': skipped}
         for test in tests:
             do_test(test, args.normalize, result_counts)