Skip to content

Commit ad93c45

Browse files
author
Steve Canny
committed
style: add Styles._get_style_id_from_name()
1 parent 180de12 commit ad93c45

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

docx/styles/styles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def _get_style_id_from_name(self, style_name, style_type):
9292
*style_type*. Raises |ValueError| if the named style is not found in
9393
the document or does not match *style_type*.
9494
"""
95-
raise NotImplementedError
95+
return self._get_style_id_from_style(self[style_name], style_type)
9696

9797
def _get_style_id_from_style(self, style, style_type):
9898
"""

tests/styles/test_styles.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ def it_gets_a_style_by_id_to_help(self, _get_by_id_fixture):
8989
assert StyleFactory_.call_args_list == StyleFactory_calls
9090
assert style is style_
9191

92+
def it_gets_a_style_id_from_a_name_to_help(self, id_name_fixture):
93+
styles, style_name, style_type, style_, style_id_ = id_name_fixture
94+
95+
style_id = styles._get_style_id_from_name(style_name, style_type)
96+
97+
styles.__getitem__.assert_called_once_with(style_name)
98+
styles._get_style_id_from_style.assert_called_once_with(
99+
style_, style_type
100+
)
101+
assert style_id is style_id_
102+
92103
# fixture --------------------------------------------------------
93104

94105
@pytest.fixture(params=[
@@ -201,6 +212,14 @@ def get_raises_fixture(self, request):
201212
styles = Styles(element(styles_cxml))
202213
return styles, 'bar'
203214

215+
@pytest.fixture
216+
def id_name_fixture(self, _getitem_, _get_style_id_from_style_, style_):
217+
styles = Styles(None)
218+
style_name, style_type, style_id_ = 'Foo Bar', 1, 'FooBar'
219+
_getitem_.return_value = style_
220+
_get_style_id_from_style_.return_value = style_id_
221+
return styles, style_name, style_type, style_, style_id_
222+
204223
@pytest.fixture(params=[
205224
('w:styles', 0),
206225
('w:styles/w:style', 1),
@@ -236,6 +255,10 @@ def default_(self, request):
236255
def _get_by_id_(self, request):
237256
return method_mock(request, Styles, '_get_by_id')
238257

258+
@pytest.fixture
259+
def _getitem_(self, request):
260+
return method_mock(request, Styles, '__getitem__')
261+
239262
@pytest.fixture
240263
def _get_style_id_from_name_(self, request):
241264
return method_mock(request, Styles, '_get_style_id_from_name')

0 commit comments

Comments
 (0)