Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1bcdf4a

Browse files
DKWoodsSteve Canny
authored andcommitted
tabs: add TabStops.__iter__()
1 parent d0bd28e commit 1bcdf4a

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

docx/text/tabstops.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ def __init__(self, element):
2525
super(TabStops, self).__init__(element, None)
2626
self._pPr = element
2727

28+
def __iter__(self):
29+
"""
30+
Generate a TabStop object for each of the w:tab elements, in XML
31+
document order.
32+
"""
33+
tabs = self._pPr.tabs
34+
if tabs is not None:
35+
for tab in tabs.tab_lst:
36+
yield TabStop(tab)
37+
2838
def __len__(self):
2939
tabs = self._pPr.tabs
3040
if tabs is None:

tests/text/test_tabstops.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
absolute_import, division, print_function, unicode_literals
1010
)
1111

12-
from docx.text.tabstops import TabStops
12+
from docx.text.tabstops import TabStop, TabStops
1313

1414
import pytest
1515

1616
from ..unitutil.cxml import element
17+
from ..unitutil.mock import call, class_mock, instance_mock
1718

1819

1920
class DescribeTabStops(object):
@@ -22,8 +23,32 @@ def it_knows_its_length(self, len_fixture):
2223
tab_stops, expected_value = len_fixture
2324
assert len(tab_stops) == expected_value
2425

26+
def it_can_iterate_over_its_tab_stops(self, iter_fixture):
27+
tab_stops, expected_count, tab_stop_, TabStop_, expected_calls = (
28+
iter_fixture
29+
)
30+
count = 0
31+
for tab_stop in tab_stops:
32+
assert tab_stop is tab_stop_
33+
count += 1
34+
assert count == expected_count
35+
assert TabStop_.call_args_list == expected_calls
36+
2537
# fixture --------------------------------------------------------
2638

39+
@pytest.fixture(params=[
40+
('w:pPr', 0),
41+
('w:pPr/w:tabs/w:tab{w:pos=2880}', 1),
42+
('w:pPr/w:tabs/(w:tab{w:pos=2880},w:tab{w:pos=5760})', 2),
43+
])
44+
def iter_fixture(self, request, TabStop_, tab_stop_):
45+
pPr_cxml, expected_count = request.param
46+
pPr = element(pPr_cxml)
47+
tab_elms = pPr.xpath('//w:tab')
48+
tab_stops = TabStops(pPr)
49+
expected_calls = [call(tab) for tab in tab_elms]
50+
return tab_stops, expected_count, tab_stop_, TabStop_, expected_calls
51+
2752
@pytest.fixture(params=[
2853
('w:pPr', 0),
2954
('w:pPr/w:tabs/w:tab{w:pos=2880}', 1),
@@ -32,3 +57,15 @@ def len_fixture(self, request):
3257
tab_stops_cxml, expected_value = request.param
3358
tab_stops = TabStops(element(tab_stops_cxml))
3459
return tab_stops, expected_value
60+
61+
# fixture components ---------------------------------------------
62+
63+
@pytest.fixture
64+
def TabStop_(self, request, tab_stop_):
65+
return class_mock(
66+
request, 'docx.text.tabstops.TabStop', return_value=tab_stop_
67+
)
68+
69+
@pytest.fixture
70+
def tab_stop_(self, request):
71+
return instance_mock(request, TabStop)

0 commit comments

Comments
 (0)