Skip to content

Commit 65d6d13

Browse files
author
Steve Canny
committed
parts: add _Body.paragraphs
1 parent 5541292 commit 65d6d13

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

docx/parts.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ def __init__(self, body_elm):
5151
def add_paragraph(self):
5252
p = self._body.add_p()
5353
return Paragraph(p)
54+
55+
@property
56+
def paragraphs(self):
57+
if not hasattr(self._body, 'p'):
58+
return ()
59+
return tuple([Paragraph(p) for p in self._body.p])

tests/test_parts.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
import pytest
1515

16-
from mock import Mock
16+
from mock import call, Mock
1717

18+
from .unitdata import a_body
1819
from .unitutil import class_mock, function_mock, initializer_mock
1920

2021

@@ -75,3 +76,21 @@ def it_can_add_a_paragraph_to_itself(self, Paragraph_):
7576
body_elm.add_p.assert_called_once_with()
7677
Paragraph_.assert_called_once_with(p_elm)
7778
assert p is Paragraph_.return_value
79+
80+
def it_provides_access_to_its_paragraphs_as_a_sequence(self, Paragraph_):
81+
# mockery ----------------------
82+
body_elm = Mock(name='body_elm')
83+
p1, p2 = (Mock(name='p1'), Mock(name='p2'))
84+
P1, P2 = (Mock(name='Paragraph1'), Mock(name='Paragraph2'))
85+
body_elm.p = [p1, p2]
86+
body = _Body(body_elm)
87+
Paragraph_.side_effect = [P1, P2]
88+
# exercise ---------------------
89+
paragraphs = body.paragraphs
90+
# verify -----------------------
91+
assert Paragraph_.mock_calls == [call(p1), call(p2)]
92+
assert paragraphs == (P1, P2)
93+
94+
def it_returns_an_empty_sequence_when_it_contains_no_paragraphs(self):
95+
body = _Body(a_body().element)
96+
assert body.paragraphs == ()

0 commit comments

Comments
 (0)