| 1 |
"""Test sphinx.util.docstrings.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from sphinx.util.docstrings import ( |
| 6 |
prepare_commentdoc, |
| 7 |
prepare_docstring, |
| 8 |
separate_metadata, |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
def test_separate_metadata() -> None: |
| 13 |
# metadata only |
| 14 |
text = ':meta foo: bar\n:meta baz:\n' |
| 15 |
docstring, metadata = separate_metadata(text) |
| 16 |
assert docstring == '' |
| 17 |
assert metadata == {'foo': 'bar', 'baz': ''} |
| 18 |
|
| 19 |
# non metadata field list item |
| 20 |
text = ':meta foo: bar\n:param baz:\n' |
| 21 |
docstring, metadata = separate_metadata(text) |
| 22 |
assert docstring == ':param baz:\n' |
| 23 |
assert metadata == {'foo': 'bar'} |
| 24 |
|
| 25 |
# field_list like text following just after paragraph is not a field_list |
| 26 |
text = 'blah blah blah\n:meta foo: bar\n:meta baz:\n' |
| 27 |
docstring, metadata = separate_metadata(text) |
| 28 |
assert docstring == text |
| 29 |
assert metadata == {} |
| 30 |
|
| 31 |
# field_list like text following after blank line is a field_list |
| 32 |
text = 'blah blah blah\n\n:meta foo: bar\n:meta baz:\n' |
| 33 |
docstring, metadata = separate_metadata(text) |
| 34 |
assert docstring == 'blah blah blah\n\n' |
| 35 |
assert metadata == {'foo': 'bar', 'baz': ''} |
| 36 |
|
| 37 |
# non field_list item breaks field_list |
| 38 |
text = ':meta foo: bar\nblah blah blah\n:meta baz:\n' |
| 39 |
docstring, metadata = separate_metadata(text) |
| 40 |
assert docstring == 'blah blah blah\n:meta baz:\n' |
| 41 |
assert metadata == {'foo': 'bar'} |
| 42 |
|
| 43 |
|
| 44 |
def test_prepare_docstring() -> None: |
| 45 |
docstring = """multiline docstring |
| 46 |
|
| 47 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit, |
| 48 |
sed do eiusmod tempor incididunt ut labore et dolore magna |
| 49 |
aliqua:: |
| 50 |
|
| 51 |
Ut enim ad minim veniam, quis nostrud exercitation |
| 52 |
ullamco laboris nisi ut aliquip ex ea commodo consequat. |
| 53 |
""" |
| 54 |
|
| 55 |
assert prepare_docstring(docstring) == [ |
| 56 |
'multiline docstring', |
| 57 |
'', |
| 58 |
'Lorem ipsum dolor sit amet, consectetur adipiscing elit,', |
| 59 |
'sed do eiusmod tempor incididunt ut labore et dolore magna', |
| 60 |
'aliqua::', |
| 61 |
'', |
| 62 |
' Ut enim ad minim veniam, quis nostrud exercitation', |
| 63 |
' ullamco laboris nisi ut aliquip ex ea commodo consequat.', |
| 64 |
'', |
| 65 |
] |
| 66 |
|
| 67 |
docstring = """ |
| 68 |
|
| 69 |
multiline docstring with leading empty lines |
| 70 |
""" |
| 71 |
assert prepare_docstring(docstring) == [ |
| 72 |
'multiline docstring with leading empty lines', |
| 73 |
'', |
| 74 |
] |
| 75 |
|
| 76 |
docstring = 'single line docstring' |
| 77 |
assert prepare_docstring(docstring) == ['single line docstring', ''] |
| 78 |
|
| 79 |
|
| 80 |
def test_prepare_commentdoc() -> None: |
| 81 |
assert prepare_commentdoc('hello world') == [] |
| 82 |
assert prepare_commentdoc('#: hello world') == ['hello world', ''] |
| 83 |
assert prepare_commentdoc('#: hello world') == [' hello world', ''] |
| 84 |
assert prepare_commentdoc('#: hello\n#: world\n') == ['hello', 'world', ''] |