cmark

My personal build of CMark ✏️

entity_tests.py (1813B)

 1 #!/usr/bin/env python3
 2 # -*- coding: utf-8 -*-
 3 
 4 import re
 5 import os
 6 import argparse
 7 import sys
 8 import platform
 9 import html
10 from cmark import CMark
11 
12 def get_entities():
13     regex = r'^{\(unsigned char\*\)"([^"]+)", \{([^}]+)\}'
14     with open(os.path.join(os.path.dirname(__file__), '..', 'src', 'entities.inc')) as f:
15         code = f.read()
16     entities = []
17     for entity, utf8 in re.findall(regex, code, re.MULTILINE):
18         utf8 = bytes(map(int, utf8.split(", ")[:-1])).decode('utf-8')
19         entities.append((entity, utf8))
20     return entities
21 
22 parser = argparse.ArgumentParser(description='Run cmark tests.')
23 parser.add_argument('--program', dest='program', nargs='?', default=None,
24         help='program to test')
25 parser.add_argument('--library-dir', dest='library_dir', nargs='?',
26         default=None, help='directory containing dynamic library')
27 args = parser.parse_args(sys.argv[1:])
28 
29 cmark = CMark(prog=args.program, library_dir=args.library_dir)
30 
31 entities = get_entities()
32 
33 passed = 0
34 errored = 0
35 failed = 0
36 
37 exceptions = {
38     'quot': '"',
39     'QUOT': '"',
40 
41     # These are broken, but I'm not too worried about them.
42     'nvlt': '<⃒',
43     'nvgt': '>⃒',
44 }
45 
46 print("Testing entities:")
47 for entity, utf8 in entities:
48     [rc, actual, err] = cmark.to_html("&{};".format(entity))
49     check = exceptions.get(entity, utf8)
50 
51     if rc != 0:
52         errored += 1
53         print(entity, '[ERRORED (return code {})]'.format(rc))
54         print(err)
55     elif check in actual:
56         # print(entity, '[PASSED]') # omit noisy success output
57         passed += 1
58     else:
59         print(entity, '[FAILED]')
60         print(repr(actual))
61         failed += 1
62 
63 print("{} passed, {} failed, {} errored".format(passed, failed, errored))
64 if failed == 0 and errored == 0:
65     exit(0)
66 else:
67     exit(1)