1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
from __future__ import unicode_literals
import unittest
from cmakelang.format import __main__
from cmakelang import markup
class MockMarkupConfig(object):
def __init__(self, fence_pattern, ruler_pattern):
self.fence_pattern = fence_pattern
self.ruler_pattern = ruler_pattern
class MockRootConfig(object):
def __init__(self, fence_pattern, ruler_pattern):
self.markup = MockMarkupConfig(fence_pattern, ruler_pattern)
class TestSpecificParses(unittest.TestCase):
def assert_item_types(self, input_str, expected_types, strip_indent=6,
config=None):
"""
Run the lexer on the input string and assert that the result tokens match
the expected
"""
lines = [line[strip_indent:] for line in input_str.split('\n')]
self.assertEqual(expected_types,
[item.kind for item in markup.parse(lines, config)])
def test_paragraphs(self):
self.assert_item_types("""\
Hello world
Hello wold""", [markup.CommentType.PARAGRAPH,
markup.CommentType.SEPARATOR,
markup.CommentType.PARAGRAPH])
def test_lists(self):
self.assert_item_types("""\
This is a paragraph
* this is a
* bulleted list
* of three items
1. this is another list
2. of two items
This is a paragraph""",
[markup.CommentType.PARAGRAPH,
markup.CommentType.SEPARATOR,
markup.CommentType.BULLET_LIST,
markup.CommentType.SEPARATOR,
markup.CommentType.ENUM_LIST,
markup.CommentType.SEPARATOR,
markup.CommentType.PARAGRAPH])
def test_list_indentation(self):
lines = [line[6:] for line in """\
* this is a
* bulleted list
* of three items
* this is another list
* of two items
* this is a third list
* of two items
""".splitlines()]
items = markup.parse(lines)
self.assertEqual(items[0].kind, markup.CommentType.BULLET_LIST)
self.assertEqual(items[0].indent, 0)
self.assertEqual(items[2].kind, markup.CommentType.BULLET_LIST)
self.assertEqual(items[2].indent, 2)
self.assertEqual(items[4].kind, markup.CommentType.BULLET_LIST)
self.assertEqual(items[4].indent, 4)
def test_notes(self):
self.assert_item_types("""\
This is a comment
that should be joined but
TODO(josh): This todo should not be joined with the previous line.
NOTE(josh): Also this should not be joined with the todo.
""", [markup.CommentType.PARAGRAPH,
markup.CommentType.NOTE,
markup.CommentType.NOTE,
markup.CommentType.SEPARATOR])
def test_rulers(self):
self.assert_item_types("""\
--------------------
This is some
text that I expect
to reflow
--------------------
""", [markup.CommentType.RULER,
markup.CommentType.PARAGRAPH,
markup.CommentType.RULER,
markup.CommentType.SEPARATOR])
def test_rulers_break_bullets(self):
self.assert_item_types("""\
--------------------
* Bulleted item
* Bulttied item
--------------------
""", [markup.CommentType.RULER,
markup.CommentType.BULLET_LIST,
markup.CommentType.RULER,
markup.CommentType.SEPARATOR])
def test_fences(self):
self.assert_item_types("""\
~~~
this is some
verbatim text
that should not
be changed
~~~~~~
""", [markup.CommentType.FENCE,
markup.CommentType.VERBATIM,
markup.CommentType.FENCE,
markup.CommentType.SEPARATOR])
def test_custom_fences(self):
self.assert_item_types("""\
###
this is some
verbatim text
that should not
be changed
###
""", [markup.CommentType.PARAGRAPH,
markup.CommentType.SEPARATOR])
config = MockRootConfig(
fence_pattern=r'^\s*([#`~]{3}[#`~]*)(.*)$',
ruler_pattern=r'^\s*[^\w\s]{3}.*[^\w\s]{3}$')
self.assert_item_types(
"""\
###
this is some
verbatim text
that should not
be changed
###
""",
[markup.CommentType.FENCE,
markup.CommentType.VERBATIM,
markup.CommentType.FENCE,
markup.CommentType.SEPARATOR],
config=config)
if __name__ == '__main__':
unittest.main()
|