Skip to content

Commit d76d47c

Browse files
author
Steve Canny
committed
style: add Styles.__iter__()
1 parent 371c94f commit d76d47c

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

docx/styles/style.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
from ..shared import ElementProxy
1212

1313

14+
def StyleFactory(style_elm):
15+
"""
16+
Return a style object of the appropriate |_BaseStyle| subclass, according
17+
to it style type.
18+
"""
19+
raise NotImplementedError
20+
21+
1422
class BaseStyle(ElementProxy):
1523
"""
1624
Base class for the various types of style object, paragraph, character,

docx/styles/styles.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
)
1010

1111
from ..shared import ElementProxy
12+
from .style import StyleFactory
1213

1314

1415
class Styles(ElementProxy):
@@ -20,5 +21,8 @@ class Styles(ElementProxy):
2021

2122
__slots__ = ()
2223

24+
def __iter__(self):
25+
return (StyleFactory(style) for style in self._element.style_lst)
26+
2327
def __len__(self):
2428
return len(self._element.style_lst)

tests/styles/test_styles.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
import pytest
1212

13+
from docx.styles.style import BaseStyle
1314
from docx.styles.styles import Styles
1415

1516
from ..unitutil.cxml import element
17+
from ..unitutil.mock import call, function_mock, instance_mock
1618

1719

1820
class DescribeStyles(object):
@@ -21,8 +23,33 @@ def it_knows_its_length(self, len_fixture):
2123
styles, expected_value = len_fixture
2224
assert len(styles) == expected_value
2325

26+
def it_can_iterate_over_its_styles(self, iter_fixture):
27+
styles, expected_count, style_, StyleFactory_, expected_calls = (
28+
iter_fixture
29+
)
30+
count = 0
31+
for style in styles:
32+
assert style is style_
33+
count += 1
34+
assert count == expected_count
35+
assert StyleFactory_.call_args_list == expected_calls
36+
2437
# fixture --------------------------------------------------------
2538

39+
@pytest.fixture(params=[
40+
('w:styles', 0),
41+
('w:styles/w:style', 1),
42+
('w:styles/(w:style,w:style)', 2),
43+
('w:styles/(w:style,w:style,w:style)', 3),
44+
])
45+
def iter_fixture(self, request, StyleFactory_, style_):
46+
styles_cxml, expected_count = request.param
47+
styles_elm = element(styles_cxml)
48+
styles = Styles(styles_elm)
49+
expected_calls = [call(style_elm) for style_elm in styles_elm]
50+
StyleFactory_.return_value = style_
51+
return styles, expected_count, style_, StyleFactory_, expected_calls
52+
2653
@pytest.fixture(params=[
2754
('w:styles', 0),
2855
('w:styles/w:style', 1),
@@ -33,3 +60,13 @@ def len_fixture(self, request):
3360
styles_cxml, expected_value = request.param
3461
styles = Styles(element(styles_cxml))
3562
return styles, expected_value
63+
64+
# fixture components ---------------------------------------------
65+
66+
@pytest.fixture
67+
def style_(self, request):
68+
return instance_mock(request, BaseStyle)
69+
70+
@pytest.fixture
71+
def StyleFactory_(self, request):
72+
return function_mock(request, 'docx.styles.styles.StyleFactory')

0 commit comments

Comments
 (0)