Skip to content

Commit ef5f164

Browse files
author
Steve Canny
committed
style: add Styles._get_style_id_from_style()
1 parent ad93c45 commit ef5f164

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

docx/styles/styles.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ def _get_style_id_from_style(self, style, style_type):
9999
Return the id of *style*, or |None| if it is the default style of
100100
*style_type*. Raises |ValueError| if style is not of *style_type*.
101101
"""
102-
raise NotImplementedError
102+
if style.type != style_type:
103+
raise ValueError(
104+
"assigned style is type %s, need type %s" %
105+
(style.type, style_type)
106+
)
107+
if style == self.default(style_type):
108+
return None
109+
return style.style_id
103110

104111
@staticmethod
105112
def _translate_special_case_names(name):

features/par-style-prop.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Feature: Each paragraph has a read/write style
1616
| Body Text | Body Text |
1717

1818

19-
@wip
2019
Scenario Outline: Set the style of a paragraph
2120
Given a paragraph
2221
When I assign a <style-spec> to paragraph.style

tests/styles/test_styles.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ def it_gets_a_style_id_from_a_name_to_help(self, id_name_fixture):
100100
)
101101
assert style_id is style_id_
102102

103+
def it_gets_a_style_id_from_a_style_to_help(self, id_style_fixture):
104+
styles, style_, style_type, style_id_ = id_style_fixture
105+
106+
style_id = styles._get_style_id_from_style(style_, style_type)
107+
108+
styles.default.assert_called_once_with(style_type)
109+
assert style_id is style_id_
110+
111+
def it_raises_on_style_type_mismatch(self, id_style_raises_fixture):
112+
styles, style_, style_type = id_style_raises_fixture
113+
with pytest.raises(ValueError):
114+
styles._get_style_id_from_style(style_, style_type)
115+
103116
# fixture --------------------------------------------------------
104117

105118
@pytest.fixture(params=[
@@ -220,6 +233,23 @@ def id_name_fixture(self, _getitem_, _get_style_id_from_style_, style_):
220233
_get_style_id_from_style_.return_value = style_id_
221234
return styles, style_name, style_type, style_, style_id_
222235

236+
@pytest.fixture(params=[True, False])
237+
def id_style_fixture(self, request, default_, style_):
238+
style_is_default = request.param
239+
styles = Styles(None)
240+
style_id, style_type = 'FooBar', 1
241+
default_.return_value = style_ if style_is_default else None
242+
style_.style_id, style_.type = style_id, style_type
243+
expected_value = None if style_is_default else style_id
244+
return styles, style_, style_type, expected_value
245+
246+
@pytest.fixture
247+
def id_style_raises_fixture(self, style_):
248+
styles = Styles(None)
249+
style_.type = 1
250+
style_type = 2
251+
return styles, style_, style_type
252+
223253
@pytest.fixture(params=[
224254
('w:styles', 0),
225255
('w:styles/w:style', 1),

0 commit comments

Comments
 (0)