|
1 | | -# -*- coding: utf-8 -*- |
2 | | -import codecs |
3 | | -import os |
4 | | -import re |
5 | | - |
6 | | -from tests.normalize import normalize_html |
7 | | - |
8 | | -TEST_ROOT = os.path.dirname(__file__) |
9 | | -EXAMPLE_PATTERN = re.compile( |
10 | | - r"^`{32} example\b.*?\n([\s\S]*?)^\.\n([\s\S]*?)^`{32}$|^#{1,6} *(.*)$", |
11 | | - flags=re.M, |
12 | | -) |
13 | | - |
14 | | - |
15 | | -def parse_examples(text): |
16 | | - data = EXAMPLE_PATTERN.findall(text) |
17 | | - |
18 | | - section = None |
19 | | - count = 0 |
20 | | - for md, html, title in data: |
21 | | - if title: |
22 | | - count = 0 |
23 | | - section = title.lower().split("(")[0].replace(" ", "_") |
24 | | - |
25 | | - if md and html: |
26 | | - count += 1 |
27 | | - name = "%s_%03d" % (section, count) |
28 | | - md = md.replace("→", "\t") |
29 | | - html = html.replace("→", "\t") |
30 | | - yield name, md, html |
31 | | - |
32 | | - |
33 | | -class SpecTestSuite: |
34 | | - @classmethod |
35 | | - def load_spec(cls, spec_name): |
36 | | - def attach_case(n, md, html): |
37 | | - def method(self): |
38 | | - self.assert_case(md, html) |
39 | | - |
40 | | - name = "test_{}".format(n) |
41 | | - method.__name__ = name |
42 | | - method.__doc__ = "Run spec {} - {}".format(spec_name, n) |
43 | | - setattr(cls, name, method) |
44 | | - |
45 | | - spec_file = os.path.join(TEST_ROOT, "spec/{}.txt".format(spec_name)) |
46 | | - with codecs.open(spec_file, encoding="utf-8") as f: |
47 | | - for name, md, html in parse_examples(f.read()): |
48 | | - if not cls.ignore_case(name): |
49 | | - attach_case(name, md, html) |
50 | | - |
51 | | - @classmethod |
52 | | - def ignore_case(cls, n): |
53 | | - return False |
54 | | - |
55 | | - def assert_case(self, text, html): |
56 | | - result = self.markdown(text) |
57 | | - assert normalize_html(result) == normalize_html(html), repr(result) |
58 | | - |
59 | | - # Extra cases that are not included |
60 | | - def test_mixed_tab_space_in_list_item(self): |
61 | | - text = "* foo\n\t* foo.bar" |
62 | | - html = "<ul><li>foo<ul><li>foo.bar</li></ul></li></ul>" |
63 | | - self.assert_case(text, html) |
0 commit comments