|
9 | 9 | from xml2mw.transform import to_mw
|
10 | 10 |
|
11 | 11 |
|
12 |
| -class TransformToMW(TestCase): |
13 |
| - """TestCases for transformation to media wiki markup. |
14 |
| -
|
15 |
| - Note, that `to_mw()` returns a generator, so we need to |
16 |
| - consume it with e.g. `list()` or `'\n'.join()` to generate |
17 |
| - an actual result. |
| 12 | +class TransformEdgeCases(TestCase): |
| 13 | + """Some general tests which do not fit into any |
| 14 | + other category. |
18 | 15 | """
|
19 | 16 |
|
20 | 17 | def test_empty_page(self):
|
21 | 18 | """A completely empty body should yield an empty generator."""
|
22 | 19 | page_body = ''
|
23 |
| - result = '\n'.join(to_mw(page_body)) |
24 | 20 | expected = ''
|
| 21 | + result = '\n'.join(to_mw(page_body)) |
25 | 22 | self.assertEqual(result, expected)
|
26 | 23 |
|
| 24 | + |
| 25 | +class TransformHeadings(TestCase): |
| 26 | + """TestCases for transformation of headings, which can only |
| 27 | + be at the beginning of lines. |
| 28 | +
|
| 29 | + Note, that `to_mw()` returns a generator, so we need to |
| 30 | + consume it with e.g. `list()` or `'\n'.join()` to generate |
| 31 | + an actual result. |
| 32 | + """ |
| 33 | + |
27 | 34 | def test_various_headings(self):
|
28 | 35 | """Test transformation of heading markup."""
|
29 | 36 | page_body = 'h1. H1\nh2. H2\nh3. H3\nh4. H4\n'
|
30 |
| - result = '\n'.join(to_mw(page_body)) |
31 | 37 | expected = '= H1 =\n== H2 ==\n=== H3 ===\n==== H4 ====\n'
|
| 38 | + result = '\n'.join(to_mw(page_body)) |
32 | 39 | self.assertEqual(result, expected)
|
33 | 40 |
|
34 |
| - def test_inline_markup(self): |
35 |
| - """The markup which is currently implemented must be at the |
| 41 | + def test_no_inline_headings(self): |
| 42 | + """Most markup which is currently implemented must be at the |
36 | 43 | beginning of lines, i.e. a `h1. ` in the middle of a line
|
37 | 44 | should not be interpreted as heading markup.
|
38 | 45 | """
|
39 |
| - page_body = 'h1. A Heading\n\nA line with h1. in it.\n' |
| 46 | + page_body = 'h1. A Heading\n\nA line with h1. in it.\nA line with h3. in it.\n' |
| 47 | + expected = '= A Heading =\n\nA line with h1. in it.\nA line with h3. in it.\n' |
| 48 | + result = '\n'.join(to_mw(page_body)) |
| 49 | + self.assertEqual(result, expected) |
| 50 | + |
| 51 | + |
| 52 | +class TransformLists(TestCase): |
| 53 | + """Test cases for transformation of unordered and ordered lists. |
| 54 | +
|
| 55 | + Note, that most of the list syntax is identical for confluence |
| 56 | + and mediawiki markup, so for many of these tests, `to_mw()` |
| 57 | + should actually be a no-op. |
| 58 | + Also note that because `to_mw()` returns a generator, we need |
| 59 | + to consume it with e.g. `list()` or `'\n'.join()` to generate |
| 60 | + an actual result. |
| 61 | + """ |
| 62 | + |
| 63 | + def test_unordered_list_a(self): |
| 64 | + """There are two versions of unordered list items in |
| 65 | + confluence markup, this tests the `*` version. |
| 66 | + """ |
| 67 | + page_body = 'Some text.\n* first item.\n* second item.\n' |
| 68 | + expected = 'Some text.\n* first item.\n* second item.\n' |
| 69 | + result = '\n'.join(to_mw(page_body)) |
| 70 | + self.assertEqual(result, expected) |
| 71 | + |
| 72 | + def test_unordered_list_b(self): |
| 73 | + """There are two versions of unordered list items in |
| 74 | + confluence markup, this tests the `-` version. |
| 75 | + """ |
| 76 | + page_body = 'Some text.\n- first item.\n- second item.\n' |
| 77 | + expected = 'Some text.\n* first item.\n* second item.\n' |
| 78 | + result = '\n'.join(to_mw(page_body)) |
| 79 | + self.assertEqual(result, expected) |
| 80 | + |
| 81 | + def test_nested_unordered_lists_a(self): |
| 82 | + """Test nested unordered lists of type `*`, but not mixed ones.""" |
| 83 | + page_body = 'Some text.\n* first level, first item\n** second level, first item\n** second level, second item\n* first level, second item\n' |
| 84 | + expected = 'Some text.\n* first level, first item\n** second level, first item\n** second level, second item\n* first level, second item\n' |
| 85 | + result = '\n'.join(to_mw(page_body)) |
| 86 | + self.assertEqual(result, expected) |
| 87 | + |
| 88 | + def test_nested_unordered_lists_b(self): |
| 89 | + """Test nested unordered lists of type `-`, but not mixed ones.""" |
| 90 | + page_body = 'Some text.\n- first level, first item\n-- second level, first item\n-- second level, second item\n- first level, second item\n' |
| 91 | + expected = 'Some text.\n* first level, first item\n** second level, first item\n** second level, second item\n* first level, second item\n' |
| 92 | + result = '\n'.join(to_mw(page_body)) |
| 93 | + self.assertEqual(result, expected) |
| 94 | + |
| 95 | + def test_ordered_list(self): |
| 96 | + """Test if ordered list items are transformed correctly.""" |
| 97 | + page_body = 'Some text.\n# first item\n# second item\n' |
| 98 | + expected = 'Some text.\n# first item\n# second item\n' |
| 99 | + result = '\n'.join(to_mw(page_body)) |
| 100 | + self.assertEqual(result, expected) |
| 101 | + |
| 102 | + def test_nested_ordered_lists(self): |
| 103 | + """Test nested ordered lists, but not mixed ones.""" |
| 104 | + page_body = 'Some text.\n# first, first\n## second, first\n## second, second\n# first, second\n' |
| 105 | + expected = 'Some text.\n# first, first\n## second, first\n## second, second\n# first, second\n' |
| 106 | + result = '\n'.join(to_mw(page_body)) |
| 107 | + self.assertEqual(result, expected) |
| 108 | + |
| 109 | + def test_mixed_lists(self): |
| 110 | + """Ordered and unordered lists can be mixed.""" |
| 111 | + page_body = '# first\n#* mixed\n#* list\n# for us\n' |
| 112 | + expected = '# first\n#* mixed\n#* list\n# for us\n' |
40 | 113 | result = '\n'.join(to_mw(page_body))
|
41 |
| - expected = '= A Heading =\n\nA line with h1. in it.\n' |
42 | 114 | self.assertEqual(result, expected)
|
| 115 | + |
| 116 | + def test_no_inline_ordered_lists(self): |
| 117 | + """Ordered lists must start at the beginning of a line.""" |
| 118 | + page_body = 'Some text with # a pseudo list\nSome more ## fake lists\n' |
| 119 | + expected = 'Some text with # a pseudo list\nSome more ## fake lists\n' |
| 120 | + result = '\n'.join(to_mw(page_body)) |
| 121 | + self.assertEqual(result, expected) |
| 122 | + |
| 123 | + def test_no_inline_unordered_lists(self): |
| 124 | + """Unordered lists must start at the beginning of a line.""" |
| 125 | + # Note: we don't test for unordered lists with `*`, because these |
| 126 | + # would indicate emphasis when not used at the beginning of the line. |
| 127 | + page_body = 'Some text with - a pseudo-list\nSome more -- fake lists.\n' |
| 128 | + # Should be a no-op. |
| 129 | + result = '\n'.join(to_mw(page_body)) |
| 130 | + self.assertEqual(result, page_body) |
| 131 | + |
| 132 | + |
| 133 | +class TransformEmphasis(TestCase): |
| 134 | + """TestCases for transformations of emphasis, which can occur anywhere |
| 135 | + in a line. |
| 136 | +
|
| 137 | + Note, that `to_mw()` returns a generator, so we need to |
| 138 | + consume it with e.g. `list()` or `'\n'.join()` to generate |
| 139 | + an actual result. |
| 140 | + """ |
| 141 | + |
| 142 | + def test_light_emphasis(self): |
| 143 | + """This type of emphasis is normally rendered as cursive text.""" |
| 144 | + known_items = [ |
| 145 | + ("text with _inline emphasis_ and more text", "text with ''inline emphasis'' and more text"), |
| 146 | + ("_emphasis at the beginning_ and more text", "''emphasis at the beginning'' and more text"), |
| 147 | + ("emphasis with following _non-white-space_, and text", "emphasis with following ''non-white-space'', and text"), |
| 148 | + ] |
| 149 | + for page_body, expected in known_items: |
| 150 | + result = '\n'.join(to_mw(page_body)) |
| 151 | + self.assertEqual(result, expected) |
| 152 | + |
| 153 | + def test_strong_emphasis(self): |
| 154 | + """This type of emphasis is normally rendered as bold text.""" |
| 155 | + known_items = [ |
| 156 | + ("text with *inline emphasis* and more text", "text with '''inline emphasis''' and more text"), |
| 157 | + ("*emphasis at the beginning* and more text", "'''emphasis at the beginning''' and more text"), |
| 158 | + ("emphasis with following *non-white-space*, and text", "emphasis with following '''non-white-space''', and text"), |
| 159 | + ] |
| 160 | + for page_body, expected in known_items: |
| 161 | + result = '\n'.join(to_mw(page_body)) |
| 162 | + self.assertEqual(result, expected) |
| 163 | + |
| 164 | + def test_very_strong_emphasis(self): |
| 165 | + """This type of emphasis is normally rendered as both bold and |
| 166 | + cursive text. |
| 167 | + """ |
| 168 | + known_items = [ |
| 169 | + ("text with *_inline emphasis_* and more text", "text with '''''inline emphasis''''' and more text"), |
| 170 | + ("*_emphasis at the beginning_* and more text", "'''''emphasis at the beginning''''' and more text"), |
| 171 | + ("emphasis with following *_non-white-space_*, and text", "emphasis with following '''''non-white-space''''', and text"), |
| 172 | + ("text with _*inline emphasis*_ and more text", "text with '''''inline emphasis''''' and more text"), |
| 173 | + ("_*emphasis at the beginning*_ and more text", "'''''emphasis at the beginning''''' and more text"), |
| 174 | + ("emphasis with following _*non-white-space*_, and text", "emphasis with following '''''non-white-space''''', and text"), |
| 175 | + ] |
| 176 | + for page_body, expected in known_items: |
| 177 | + result = '\n'.join(to_mw(page_body)) |
| 178 | + self.assertEqual(result, expected) |
0 commit comments