Skip to content

Commit 5d2afe4

Browse files
author
Steve Canny
committed
test: refactor DescribeParagraph
* segregate fixtures out of text methods * eliminate mocking in it_can_add_a_run_to_itself()
1 parent 86ae5d2 commit 5d2afe4

File tree

2 files changed

+61
-34
lines changed

2 files changed

+61
-34
lines changed

docx/text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class Paragraph(object):
1313
"""
1414
Proxy object wrapping ``<w:p>`` element.
1515
"""
16-
def __init__(self, p_elm):
16+
def __init__(self, p):
1717
super(Paragraph, self).__init__()
18-
self._p = p_elm
18+
self._p = p
1919

2020
def add_run(self):
2121
"""

tests/test_text.py

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,36 @@
44
Test suite for the docx.text module
55
"""
66

7-
from __future__ import absolute_import, print_function, unicode_literals
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
810

911
from docx.enum.text import WD_BREAK
10-
from docx.oxml.text import CT_P
12+
from docx.oxml.text import CT_P, CT_R
1113
from docx.text import Paragraph, Run
1214

1315
import pytest
1416

15-
from mock import call, create_autospec, Mock
17+
from mock import call, Mock
1618

1719
from .oxml.unitdata.text import a_b, a_br, a_t, a_p, an_i, an_r, an_rPr
18-
from .unitutil import class_mock
20+
from .unitutil import class_mock, instance_mock
1921

2022

2123
class DescribeParagraph(object):
2224

23-
@pytest.fixture
24-
def Run_(self, request):
25-
return class_mock(request, 'docx.text.Run')
26-
27-
def it_has_a_sequence_of_the_runs_it_contains(self, Run_):
28-
p_elm = Mock(name='p_elm')
29-
r1, r2 = (Mock(name='r1'), Mock(name='r2'))
30-
R1, R2 = (Mock(name='Run1'), Mock(name='Run2'))
31-
p_elm.r_lst = [r1, r2]
32-
p = Paragraph(p_elm)
33-
Run_.side_effect = [R1, R2]
34-
# exercise ---------------------
35-
runs = p.runs
36-
# verify -----------------------
37-
assert Run_.mock_calls == [call(r1), call(r2)]
38-
assert runs == [R1, R2]
39-
40-
def it_can_add_a_run_to_itself(self, Run_):
41-
# mockery ----------------------
42-
p_elm = create_autospec(CT_P)
43-
p_elm.add_r.return_value = r_elm = Mock(name='r_elm')
44-
p = Paragraph(p_elm)
45-
# exercise ---------------------
46-
r = p.add_run()
47-
# verify -----------------------
48-
p_elm.add_r.assert_called_once_with()
49-
Run_.assert_called_once_with(r_elm)
50-
assert r is Run_.return_value
25+
def it_has_a_sequence_of_the_runs_it_contains(self, runs_fixture):
26+
paragraph, Run_, r_, r_2_, run_, run_2_ = runs_fixture
27+
runs = paragraph.runs
28+
assert Run_.mock_calls == [call(r_), call(r_2_)]
29+
assert runs == [run_, run_2_]
30+
31+
def it_can_add_a_run_to_itself(self, add_run_fixture):
32+
paragraph, expected_xml = add_run_fixture
33+
run = paragraph.add_run()
34+
assert paragraph._p.xml == expected_xml
35+
assert isinstance(run, Run)
36+
assert run._r is paragraph._p.r_lst[0]
5137

5238
def it_knows_its_paragraph_style(self):
5339
cases = (
@@ -75,6 +61,47 @@ def it_knows_the_text_it_contains(self, text_prop_fixture):
7561

7662
# fixtures -------------------------------------------------------
7763

64+
@pytest.fixture
65+
def add_run_fixture(self, paragraph):
66+
expected_xml = a_p().with_nsdecls().with_child(an_r()).xml()
67+
return paragraph, expected_xml
68+
69+
@pytest.fixture
70+
def p_(self, request, r_, r_2_):
71+
return instance_mock(request, CT_P, r_lst=(r_, r_2_))
72+
73+
@pytest.fixture
74+
def paragraph(self, request):
75+
p = a_p().with_nsdecls().element
76+
return Paragraph(p)
77+
78+
@pytest.fixture
79+
def Run_(self, request, runs_):
80+
run_, run_2_ = runs_
81+
return class_mock(
82+
request, 'docx.text.Run', side_effect=[run_, run_2_]
83+
)
84+
85+
@pytest.fixture
86+
def r_(self, request):
87+
return instance_mock(request, CT_R)
88+
89+
@pytest.fixture
90+
def r_2_(self, request):
91+
return instance_mock(request, CT_R)
92+
93+
@pytest.fixture
94+
def runs_(self, request):
95+
run_ = instance_mock(request, Run, name='run_')
96+
run_2_ = instance_mock(request, Run, name='run_2_')
97+
return run_, run_2_
98+
99+
@pytest.fixture
100+
def runs_fixture(self, p_, Run_, r_, r_2_, runs_):
101+
paragraph = Paragraph(p_)
102+
run_, run_2_ = runs_
103+
return paragraph, Run_, r_, r_2_, run_, run_2_
104+
78105
@pytest.fixture
79106
def text_prop_fixture(self):
80107
p = (

0 commit comments

Comments
 (0)