Skip to content

Commit 51c79c6

Browse files
author
Steve Canny
committed
test: refactor tests.test_table to use cxml
1 parent 3c06e1c commit 51c79c6

3 files changed

Lines changed: 97 additions & 128 deletions

File tree

docx/oxml/table.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,23 @@ class CT_TblPr(BaseOxmlElement):
7777
"""
7878
tblStyle = ZeroOrOne('w:tblStyle')
7979

80-
def add_tblStyle(self, style_name):
80+
@property
81+
def style(self):
8182
"""
82-
Return a new <w:tblStyle> element having its style set to
83-
*style_name*.
83+
Return the value of the ``val`` attribute of the ``<w:tblStyle>``
84+
child or |None| if not present.
8485
"""
85-
return self._add_tblStyle(val=style_name)
86+
tblStyle = self.tblStyle
87+
if tblStyle is None:
88+
return None
89+
return tblStyle.val
90+
91+
@style.setter
92+
def style(self, value):
93+
self._remove_tblStyle()
94+
if value is None:
95+
return
96+
self._add_tblStyle(val=value)
8697

8798

8899
class CT_Tc(BaseOxmlElement):

docx/table.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,12 @@ def style(self):
6767
'LightShading-Accent1'. Name is derived by removing spaces from the
6868
table style name displayed in the Word UI.
6969
"""
70-
tblStyle = self._tblPr.tblStyle
71-
if tblStyle is None:
72-
return None
73-
return tblStyle.val
70+
return self._tblPr.style
7471

7572
@style.setter
76-
def style(self, style_name):
77-
tblStyle = self._tblPr.tblStyle
78-
if tblStyle is None:
79-
self._tblPr.add_tblStyle(style_name)
80-
else:
81-
tblStyle.val = style_name
73+
def style(self, value):
74+
print('style value is %s' % value)
75+
self._tblPr.style = value
8276

8377
@property
8478
def _tblPr(self):

tests/test_table.py

Lines changed: 78 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
)
1414
from docx.text import Paragraph
1515

16-
from .oxml.unitdata.table import (
17-
a_gridCol, a_tbl, a_tblGrid, a_tblPr, a_tblStyle, a_tc, a_tcPr, a_tr
18-
)
19-
from .oxml.unitdata.text import a_p, a_t, an_r
16+
from .oxml.unitdata.table import a_gridCol, a_tbl, a_tblGrid, a_tc, a_tr
17+
from .oxml.unitdata.text import a_p
18+
from .unitutil.cxml import element, xml
2019

2120

2221
class DescribeTable(object):
@@ -52,8 +51,8 @@ def it_can_add_a_column(self, add_column_fixture):
5251
assert isinstance(column, _Column)
5352
assert column._gridCol is table._tbl.tblGrid.gridCol_lst[1]
5453

55-
def it_knows_its_table_style(self, table_style_fixture):
56-
table, style = table_style_fixture
54+
def it_knows_its_table_style(self, table_style_get_fixture):
55+
table, style = table_style_get_fixture
5756
assert table.style == style
5857

5958
def it_can_apply_a_table_style_by_name(self, table_style_set_fixture):
@@ -77,94 +76,85 @@ def add_row_fixture(self):
7776
expected_xml = _tbl_bldr(rows=2, cols=2).xml()
7877
return table, expected_xml
7978

79+
@pytest.fixture(params=[
80+
('w:tbl/w:tblPr', None),
81+
('w:tbl/w:tblPr/w:tblStyle{w:val=foobar}', 'foobar'),
82+
])
83+
def table_style_get_fixture(self, request):
84+
tbl_cxml, expected_style = request.param
85+
table = Table(element(tbl_cxml))
86+
return table, expected_style
87+
88+
@pytest.fixture(params=[
89+
('w:tbl/w:tblPr', 'foobar',
90+
'w:tbl/w:tblPr/w:tblStyle{w:val=foobar}'),
91+
('w:tbl/w:tblPr/w:tblStyle{w:val=foobar}', 'barfoo',
92+
'w:tbl/w:tblPr/w:tblStyle{w:val=barfoo}'),
93+
('w:tbl/w:tblPr/w:tblStyle{w:val=foobar}', None,
94+
'w:tbl/w:tblPr'),
95+
('w:tbl/w:tblPr', None,
96+
'w:tbl/w:tblPr'),
97+
])
98+
def table_style_set_fixture(self, request):
99+
tbl_cxml, new_style, expected_cxml = request.param
100+
table = Table(element(tbl_cxml))
101+
expected_xml = xml(expected_cxml)
102+
return table, new_style, expected_xml
103+
104+
# fixture components ---------------------------------------------
105+
80106
@pytest.fixture
81107
def table(self):
82108
tbl = _tbl_bldr(rows=2, cols=2).element
83109
table = Table(tbl)
84110
return table
85111

86-
@pytest.fixture
87-
def table_style_fixture(self):
88-
style = 'foobar'
89-
tbl = (
90-
a_tbl().with_nsdecls().with_child(
91-
a_tblPr().with_child(
92-
a_tblStyle().with_val(style)))
93-
).element
94-
table = Table(tbl)
95-
return table, style
96-
97-
@pytest.fixture
98-
def table_style_set_fixture(self):
99-
# table ------------------------
100-
tbl = a_tbl().with_nsdecls().with_child(a_tblPr()).element
101-
table = Table(tbl)
102-
# style_name -------------------
103-
style_name = 'foobar'
104-
# expected_xml -----------------
105-
expected_xml = (
106-
a_tbl().with_nsdecls().with_child(
107-
a_tblPr().with_child(
108-
a_tblStyle().with_val(style_name)))
109-
).xml()
110-
return table, style_name, expected_xml
111-
112112

113113
class Describe_Cell(object):
114114

115115
def it_provides_access_to_the_paragraphs_it_contains(
116-
self, cell_with_paragraphs):
117-
cell = cell_with_paragraphs
116+
self, paragraphs_fixture):
117+
cell = paragraphs_fixture
118118
paragraphs = cell.paragraphs
119119
assert len(paragraphs) == 2
120-
for p in paragraphs:
121-
assert isinstance(p, Paragraph)
120+
count = 0
121+
for idx, paragraph in enumerate(paragraphs):
122+
assert isinstance(paragraph, Paragraph)
123+
assert paragraph is paragraphs[idx]
124+
count += 1
125+
assert count == 2
122126

123127
def it_can_replace_its_content_with_a_string_of_text(
124-
self, cell_text_fixture):
125-
cell, text, expected_xml = cell_text_fixture
128+
self, text_set_fixture):
129+
cell, text, expected_xml = text_set_fixture
126130
cell.text = text
127131
assert cell._tc.xml == expected_xml
128132

129133
# fixtures -------------------------------------------------------
130134

131-
@pytest.fixture
132-
def cell_text_fixture(self):
133-
# cell -------------------------
134-
tc = (
135-
a_tc().with_nsdecls().with_child(
136-
a_tcPr()).with_child(
137-
a_p()).with_child(
138-
a_tbl()).with_child(
139-
a_p())
140-
).element
141-
cell = _Cell(tc)
142-
# text -------------------------
143-
text = 'foobar'
144-
# expected_xml -----------------
145-
expected_xml = (
146-
a_tc().with_nsdecls().with_child(
147-
a_tcPr()).with_child(
148-
a_p().with_child(
149-
an_r().with_child(
150-
a_t().with_text(text))))
151-
).xml()
152-
return cell, text, expected_xml
135+
@pytest.fixture(params=[
136+
('w:tc/w:p', 'foobar',
137+
'w:tc/w:p/w:r/w:t"foobar"'),
138+
('w:tc/w:p', 'fo\tob\rar\n',
139+
'w:tc/w:p/w:r/(w:t"fo",w:tab,w:t"ob",w:cr,w:t"ar",w:cr)'),
140+
('w:tc/(w:tcPr, w:p, w:tbl, w:p)', 'foobar',
141+
'w:tc/(w:tcPr, w:p/w:r/w:t"foobar")'),
142+
])
143+
def text_set_fixture(self, request):
144+
tc_cxml, new_text, expected_cxml = request.param
145+
cell = _Cell(element(tc_cxml))
146+
expected_xml = xml(expected_cxml)
147+
return cell, new_text, expected_xml
153148

154149
@pytest.fixture
155-
def cell_with_paragraphs(self):
156-
tc = (
157-
a_tc().with_nsdecls()
158-
.with_child(a_p())
159-
.with_child(a_p())
160-
.element
161-
)
162-
return _Cell(tc)
150+
def paragraphs_fixture(self):
151+
return _Cell(element('w:tc/(w:p, w:p)'))
163152

164153

165154
class Describe_Column(object):
166155

167-
def it_provides_access_to_the_column_cells(self, column):
156+
def it_provides_access_to_the_column_cells(self):
157+
column = _Column(None, None)
168158
cells = column.cells
169159
assert isinstance(cells, _ColumnCells)
170160

@@ -181,43 +171,29 @@ def it_can_change_its_width(self, width_set_fixture):
181171
# fixtures -------------------------------------------------------
182172

183173
@pytest.fixture(params=[
184-
(4242, 2693670),
185-
(1440, 914400),
186-
('2.54cm', 914400),
187-
('54mm', 1944000),
188-
('12.5pt', 158750),
189-
(None, None),
174+
('w:gridCol{w:w=4242}', 2693670),
175+
('w:gridCol{w:w=1440}', 914400),
176+
('w:gridCol{w:w=2.54cm}', 914400),
177+
('w:gridCol{w:w=54mm}', 1944000),
178+
('w:gridCol{w:w=12.5pt}', 158750),
179+
('w:gridCol', None),
190180
])
191181
def width_get_fixture(self, request):
192-
w, expected_width = request.param
193-
gridCol = self.gridCol_bldr(w).element
194-
column = _Column(gridCol, None)
182+
gridCol_cxml, expected_width = request.param
183+
column = _Column(element(gridCol_cxml), None)
195184
return column, expected_width
196185

197186
@pytest.fixture(params=[
198-
(4242, None, None),
199-
(None, None, None),
200-
(4242, 914400, 1440),
201-
(None, 914400, 1440),
187+
('w:gridCol', 914400, 'w:gridCol{w:w=1440}'),
188+
('w:gridCol{w:w=4242}', 457200, 'w:gridCol{w:w=720}'),
189+
('w:gridCol{w:w=4242}', None, 'w:gridCol'),
190+
('w:gridCol', None, 'w:gridCol'),
202191
])
203192
def width_set_fixture(self, request):
204-
initial_w, value, expected_w = request.param
205-
gridCol = self.gridCol_bldr(initial_w).element
206-
column = _Column(gridCol, None)
207-
expected_xml = self.gridCol_bldr(expected_w).xml()
208-
return column, value, expected_xml
209-
210-
# fixture components ---------------------------------------------
211-
212-
@pytest.fixture
213-
def column(self):
214-
return _Column(None, None)
215-
216-
def gridCol_bldr(self, w=None):
217-
gridCol_bldr = a_gridCol().with_nsdecls()
218-
if w is not None:
219-
gridCol_bldr.with_w(w)
220-
return gridCol_bldr
193+
gridCol_cxml, new_value, expected_cxml = request.param
194+
column = _Column(element(gridCol_cxml), None)
195+
expected_xml = xml(expected_cxml)
196+
return column, new_value, expected_xml
221197

222198

223199
class Describe_ColumnCells(object):
@@ -301,19 +277,11 @@ def columns_fixture(self):
301277

302278
class Describe_Row(object):
303279

304-
def it_provides_access_to_the_row_cells(self, cells_access_fixture):
305-
row = cells_access_fixture
280+
def it_provides_access_to_the_row_cells(self):
281+
row = _Row(element('w:tr'))
306282
cells = row.cells
307283
assert isinstance(cells, _RowCells)
308284

309-
# fixtures -------------------------------------------------------
310-
311-
@pytest.fixture
312-
def cells_access_fixture(self):
313-
tr = a_tr().with_nsdecls().element
314-
row = _Row(tr)
315-
return row
316-
317285

318286
class Describe_RowCells(object):
319287

@@ -348,12 +316,8 @@ def it_raises_on_indexed_access_out_of_range(self, cell_count_fixture):
348316

349317
@pytest.fixture
350318
def cell_count_fixture(self):
319+
cells = _RowCells(element('w:tr/(w:tc, w:tc)'))
351320
cell_count = 2
352-
tr_bldr = a_tr().with_nsdecls()
353-
for idx in range(cell_count):
354-
tr_bldr.with_child(a_tc())
355-
tr = tr_bldr.element
356-
cells = _RowCells(tr)
357321
return cells, cell_count
358322

359323

0 commit comments

Comments
 (0)