Skip to content

Commit bdbd6fe

Browse files
author
Benjamin Toornstra
committed
paragraph.end_bookmark tests
1 parent f08c43a commit bdbd6fe

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

docx/text/paragraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def alignment(self, value):
5757
@lazyproperty
5858
def _bookmarks(self):
5959
"""Global |Bookmarks| object for overall document."""
60-
raise NotImplementedError
60+
return self.part.bookmarks
6161

6262
def clear(self):
6363
"""

tests/text/test_paragraph.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929

3030

3131
class DescribeParagraph(object):
32+
def it_can_end_a_bookmark(self, end_bookmark_fixture):
33+
blockContainer, name, bookmarkStart, expected_xml = end_bookmark_fixture
34+
35+
blkcntnr = Paragraph(blockContainer, None)
36+
bookmark_ = _Bookmark((bookmarkStart, None))
37+
38+
bookmark = blkcntnr.end_bookmark(bookmark_)
39+
40+
assert blkcntnr._element.xml == expected_xml
41+
assert bookmark.name == name
42+
assert bookmark is bookmark_
43+
3244
def it_can_start_a_bookmark(
3345
self,
3446
start_bookmark_fixture,
@@ -208,6 +220,30 @@ def clear_fixture(self, request):
208220
expected_xml = xml(expected_cxml)
209221
return paragraph, expected_xml
210222

223+
@pytest.fixture(
224+
params=[
225+
# ---at start of paragraph---
226+
(
227+
"w:p/(w:bookmarkStart{w:name=bmk-1, w:id=24})",
228+
"w:bookmarkStart{w:name=bmk-1, w:id=24}",
229+
"w:p/(w:bookmarkStart{w:name=bmk-1, w:id=24},w:bookmarkEnd{w:id=24})",
230+
),
231+
(
232+
# ---run in between bookmarks---
233+
'w:p/(w:bookmarkStart{w:name=bmk-1, w:id=24}, w:r/w:t"foobar")',
234+
"w:bookmarkStart{w:name=bmk-1, w:id=24}",
235+
'w:p/(w:bookmarkStart{w:name=bmk-1, w:id=24},w:r/w:t"foobar", w:bookmarkEnd{w:id=24})',
236+
),
237+
]
238+
)
239+
def end_bookmark_fixture(self, request):
240+
cxml, bookmark_cxml, expected_cxml = request.param
241+
blockContainer = element(cxml)
242+
bookmarkStart = element(bookmark_cxml)
243+
expected_xml = xml(expected_cxml)
244+
name = "bmk-1"
245+
return blockContainer, name, bookmarkStart, expected_xml
246+
211247
@pytest.fixture(params=[(None, None), ("Foo", None), (None, "Bar"), ("Foo", "Bar")])
212248
def insert_before_fixture(self, request, _insert_paragraph_before_, add_run_):
213249
text, style = request.param

0 commit comments

Comments
 (0)