Skip to content

Commit 6ebedac

Browse files
author
Steve Canny
committed
test: refactor oxml.test_text.py to use cxml
1 parent 3b1abda commit 6ebedac

2 files changed

Lines changed: 25 additions & 105 deletions

File tree

docx/oxml/text.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ def set_sectPr(self, sectPr):
8585
@property
8686
def style(self):
8787
"""
88-
String contained in w:val attribute of <w:pPr><w:pStyle> child, or
89-
None if that element is not present.
88+
String contained in w:val attribute of ./w:pPr/w:pStyle grandchild,
89+
or |None| if not present.
9090
"""
9191
pPr = self.pPr
9292
if pPr is None:
@@ -95,10 +95,6 @@ def style(self):
9595

9696
@style.setter
9797
def style(self, style):
98-
"""
99-
Set style of this <w:p> element to *style*. If *style* is None,
100-
remove the style element.
101-
"""
10298
pPr = self.get_or_add_pPr()
10399
pPr.style = style
104100

@@ -256,8 +252,8 @@ def text(self, text):
256252
@property
257253
def underline(self):
258254
"""
259-
String contained in w:val attribute of <w:u> grandchild, or |None| if
260-
that element is not present.
255+
String contained in w:val attribute of ./w:rPr/w:u grandchild, or
256+
|None| if not present.
261257
"""
262258
rPr = self.rPr
263259
if rPr is None:

tests/oxml/test_text.py

Lines changed: 21 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -4,106 +4,30 @@
44
Test suite for the docx.oxml.text module.
55
"""
66

7-
from docx.oxml.text import CT_R, CT_Text
7+
from __future__ import absolute_import, print_function, unicode_literals
88

9-
from .unitdata.text import a_p, a_pPr, a_pStyle, a_t, an_r
9+
import pytest
1010

11-
12-
class DescribeCT_P(object):
13-
14-
def it_has_a_sequence_of_the_runs_it_contains(self):
15-
p = a_p().with_nsdecls().with_child(an_r()).with_child(an_r()).element
16-
assert len(p.r_lst) == 2
17-
for r in p.r_lst:
18-
assert isinstance(r, CT_R)
19-
20-
def it_can_add_an_r_to_itself(self):
21-
p = a_p().with_nsdecls().element
22-
# exercise -----------------
23-
r = p.add_r()
24-
# verify -------------------
25-
assert p.xml == a_p().with_nsdecls().with_child(an_r()).xml()
26-
assert isinstance(r, CT_R)
27-
28-
def it_knows_its_paragraph_style(self):
29-
pPr_bldr = a_pPr().with_child(a_pStyle().with_val('foobar'))
30-
cases = (
31-
(a_p(), None),
32-
(a_p().with_child(pPr_bldr), 'foobar'),
33-
)
34-
for builder, expected_value in cases:
35-
p = builder.with_nsdecls().element
36-
assert p.style == expected_value
37-
38-
def it_can_set_its_paragraph_style(self):
39-
pPr = a_pPr().with_child(a_pStyle().with_val('foobar'))
40-
pPr2 = a_pPr().with_child(a_pStyle().with_val('barfoo'))
41-
cases = (
42-
(1, a_p(), None, a_p().with_child(a_pPr())),
43-
(2, a_p(), 'foobar', a_p().with_child(pPr)),
44-
(3, a_p().with_child(pPr), None, a_p().with_child(a_pPr())),
45-
(4, a_p().with_child(pPr), 'barfoo', a_p().with_child(pPr2)),
46-
)
47-
for case_nmbr, before_bldr, new_style, after_bldr in cases:
48-
p = before_bldr.with_nsdecls().element
49-
p.style = new_style
50-
expected_xml = after_bldr.with_nsdecls().xml()
51-
assert p.xml == expected_xml
52-
53-
54-
class DescribeCT_PPr(object):
55-
56-
def it_knows_the_paragraph_style(self):
57-
cases = (
58-
(a_pPr(), None),
59-
(a_pPr().with_child(a_pStyle().with_val('foobar')), 'foobar'),
60-
)
61-
for builder, expected_value in cases:
62-
pPr = builder.with_nsdecls().element
63-
assert pPr.style == expected_value
64-
65-
def it_can_set_the_paragraph_style(self):
66-
cases = (
67-
(1, a_pPr(), None, a_pPr()),
68-
(2, a_pPr(), 'foobar',
69-
a_pPr().with_child(a_pStyle().with_val('foobar'))),
70-
(3, a_pPr().with_child(a_pStyle().with_val('foobar')), None,
71-
a_pPr()),
72-
(4, a_pPr().with_child(a_pStyle().with_val('foobar')), 'barfoo',
73-
a_pPr().with_child(a_pStyle().with_val('barfoo'))),
74-
)
75-
for case_nmbr, before_bldr, new_style, after_bldr in cases:
76-
pPr = before_bldr.with_nsdecls().element
77-
pPr.style = new_style
78-
expected_xml = after_bldr.with_nsdecls().xml()
79-
assert pPr.xml == expected_xml
11+
from ..unitutil.cxml import element, xml
8012

8113

8214
class DescribeCT_R(object):
8315

84-
def it_can_add_a_t_to_itself(self):
85-
text = 'foobar'
86-
r = an_r().with_nsdecls().element
87-
# exercise -----------------
88-
t = r.add_t(text)
89-
# verify -------------------
90-
assert (
91-
r.xml ==
92-
an_r().with_nsdecls().with_child(a_t().with_text(text)).xml()
93-
)
94-
assert isinstance(t, CT_Text)
95-
96-
def it_has_a_sequence_of_the_t_elms_it_contains(self):
97-
cases = (
98-
(an_r().with_nsdecls(), 0),
99-
(an_r().with_nsdecls().with_child(
100-
a_t().with_text('foo')), 1),
101-
(an_r().with_nsdecls().with_child(
102-
a_t().with_text('foo')).with_child(
103-
a_t().with_text('bar')), 2),
104-
)
105-
for r_bldr, expected_len in cases:
106-
r = r_bldr.element
107-
assert len(r.t_lst) == expected_len
108-
for t in r.t_lst:
109-
assert isinstance(t, CT_Text)
16+
def it_can_add_a_t_preserving_edge_whitespace(self, add_t_fixture):
17+
r, text, expected_xml = add_t_fixture
18+
r.add_t(text)
19+
assert r.xml == expected_xml
20+
21+
# fixtures -------------------------------------------------------
22+
23+
@pytest.fixture(params=[
24+
('w:r', 'foobar', 'w:r/w:t"foobar"'),
25+
('w:r', 'foobar ', 'w:r/w:t{xml:space=preserve}"foobar "'),
26+
('w:r/(w:rPr/w:rStyle{w:val=emphasis}, w:cr)', 'foobar',
27+
'w:r/(w:rPr/w:rStyle{w:val=emphasis}, w:cr, w:t"foobar")'),
28+
])
29+
def add_t_fixture(self, request):
30+
initial_cxml, text, expected_cxml = request.param
31+
r = element(initial_cxml)
32+
expected_xml = xml(expected_cxml)
33+
return r, text, expected_xml

0 commit comments

Comments
 (0)