Skip to content

Commit cc9c487

Browse files
Benjamin Toornstrascanny
authored andcommitted
bmk: add _PartBookmarkFinder.iter_start_end_pairs()
1 parent d4059d1 commit cc9c487

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

docx/bookmark.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ def bookmark_pairs(self):
5858
class _PartBookmarkFinder(object):
5959
"""Provides access to bookmark oxml elements in a story part."""
6060

61+
def __init__(self, part):
62+
self._part = part
63+
6164
@classmethod
6265
def iter_start_end_pairs(cls, part):
6366
"""Generate each (bookmarkStart, bookmarkEnd) in *part*."""
67+
return cls(part)._iter_start_end_pairs()
68+
69+
def _iter_start_end_pairs(self):
70+
"""Generate each (bookmarkStart, bookmarkEnd) in this part."""
6471
raise NotImplementedError

tests/test_bookmark.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@
22

33
"""Test suite for the docx.bookmark module."""
44

5-
from __future__ import (
6-
absolute_import, division, print_function, unicode_literals
7-
)
5+
from __future__ import absolute_import, division, print_function, unicode_literals
86

97
import pytest
108

11-
from docx.bookmark import Bookmarks, _DocumentBookmarkFinder
12-
from docx.opc.part import Part
9+
from docx.bookmark import Bookmarks, _DocumentBookmarkFinder, _PartBookmarkFinder
10+
from docx.opc.part import Part, XmlPart
1311
from docx.parts.document import DocumentPart
1412

15-
from .unitutil.mock import call, class_mock, instance_mock, property_mock
13+
from .unitutil.mock import (
14+
ANY,
15+
call,
16+
class_mock,
17+
initializer_mock,
18+
instance_mock,
19+
method_mock,
20+
property_mock,
21+
)
1622

1723

1824
class DescribeBookmarks(object):
1925

2026
def it_knows_how_many_bookmarks_the_document_contains(
21-
self, _finder_prop_, finder_):
27+
self, _finder_prop_, finder_
28+
):
2229
_finder_prop_.return_value = finder_
2330
finder_.bookmark_pairs = tuple((1, 2) for _ in range(42))
2431
bookmarks = Bookmarks(None)
@@ -28,7 +35,8 @@ def it_knows_how_many_bookmarks_the_document_contains(
2835
assert count == 42
2936

3037
def it_provides_access_to_its_bookmark_finder_to_help(
31-
self, document_part_, _DocumentBookmarkFinder_, finder_):
38+
self, document_part_, _DocumentBookmarkFinder_, finder_
39+
):
3240
_DocumentBookmarkFinder_.return_value = finder_
3341
bookmarks = Bookmarks(document_part_)
3442

@@ -105,3 +113,29 @@ def _PartBookmarkFinder_(self, request):
105113
@pytest.fixture
106114
def document_part_(self, request):
107115
return instance_mock(request, DocumentPart)
116+
117+
118+
class Describe_PartBookmarkFinder(object):
119+
120+
def it_provides_an_iter_start_end_pairs_interface_method(
121+
self, part_, _init_, _iter_start_end_pairs_
122+
):
123+
pairs = _PartBookmarkFinder.iter_start_end_pairs(part_)
124+
125+
_init_.assert_called_once_with(ANY, part_)
126+
_iter_start_end_pairs_.assert_called_once_with()
127+
assert pairs == _iter_start_end_pairs_.return_value
128+
129+
# fixture components ---------------------------------------------
130+
131+
@pytest.fixture
132+
def _init_(self, request):
133+
return initializer_mock(request, _PartBookmarkFinder)
134+
135+
@pytest.fixture
136+
def _iter_start_end_pairs_(self, request):
137+
return method_mock(request, _PartBookmarkFinder, '_iter_start_end_pairs')
138+
139+
@pytest.fixture
140+
def part_(self, request):
141+
return instance_mock(request, XmlPart)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy