Skip to content

Commit cbc2d60

Browse files
author
Steve Canny
committed
tbl: reimplement _Column.cells
1 parent f615877 commit cbc2d60

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

docx/table.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ def cell(self, row_idx, col_idx):
6161
row = self.rows[row_idx]
6262
return row.cells[col_idx]
6363

64+
def column_cells(self, column_idx):
65+
"""
66+
Sequence of cells in the column at *column_idx* in this table.
67+
"""
68+
raise NotImplementedError
69+
6470
@lazyproperty
6571
def columns(self):
6672
"""
@@ -237,6 +243,20 @@ def cells(self):
237243
"""
238244
return _ColumnCells(self._tbl, self._gridCol, self)
239245

246+
@property
247+
def cells_new(self):
248+
"""
249+
Sequence of |_Cell| instances corresponding to cells in this column.
250+
"""
251+
return tuple(self.table.column_cells(self._index))
252+
253+
@property
254+
def table(self):
255+
"""
256+
Reference to the |Table| object this column belongs to.
257+
"""
258+
raise NotImplementedError
259+
240260
@property
241261
def width(self):
242262
"""
@@ -249,6 +269,13 @@ def width(self):
249269
def width(self, value):
250270
self._gridCol.w = value
251271

272+
@property
273+
def _index(self):
274+
"""
275+
Index of this column in its table, starting from zero.
276+
"""
277+
raise NotImplementedError
278+
252279

253280
class _ColumnCells(Parented):
254281
"""

features/steps/table.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ def then_table_cell_row_col_text_is_text(context, row, col, expected_text):
216216
@then('the column cells text is {expected_text}')
217217
def then_the_column_cells_text_is_expected_text(context, expected_text):
218218
table = context.table_
219-
cells_text = ' '.join(c.text for col in table.columns for c in col.cells)
219+
cells_text = ' '.join(
220+
c.text for col in table.columns for c in col.cells_new
221+
)
220222
assert cells_text == expected_text, 'got %s' % cells_text
221223

222224

tests/test_table.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,11 @@ def width_set_fixture(self, request):
374374

375375
class Describe_Column(object):
376376

377-
def it_provides_access_to_the_column_cells(self):
378-
column = _Column(None, None, None)
379-
cells = column.cells
380-
assert isinstance(cells, _ColumnCells)
377+
def it_provides_access_to_its_cells(self, cells_fixture):
378+
column, column_idx, expected_cells = cells_fixture
379+
cells = column.cells_new
380+
column.table.column_cells.assert_called_once_with(column_idx)
381+
assert cells == expected_cells
381382

382383
def it_knows_its_width_in_EMU(self, width_get_fixture):
383384
column, expected_width = width_get_fixture
@@ -391,6 +392,14 @@ def it_can_change_its_width(self, width_set_fixture):
391392

392393
# fixtures -------------------------------------------------------
393394

395+
@pytest.fixture
396+
def cells_fixture(self, _index_, table_prop_, table_):
397+
column = _Column(None, None, None)
398+
_index_.return_value = column_idx = 4
399+
expected_cells = (3, 2, 1)
400+
table_.column_cells.return_value = list(expected_cells)
401+
return column, column_idx, expected_cells
402+
394403
@pytest.fixture(params=[
395404
('w:gridCol{w:w=4242}', 2693670),
396405
('w:gridCol{w:w=1440}', 914400),
@@ -416,6 +425,20 @@ def width_set_fixture(self, request):
416425
expected_xml = xml(expected_cxml)
417426
return column, new_value, expected_xml
418427

428+
# fixture components ---------------------------------------------
429+
430+
@pytest.fixture
431+
def _index_(self, request):
432+
return property_mock(request, _Column, '_index')
433+
434+
@pytest.fixture
435+
def table_(self, request):
436+
return instance_mock(request, Table)
437+
438+
@pytest.fixture
439+
def table_prop_(self, request, table_):
440+
return property_mock(request, _Column, 'table', return_value=table_)
441+
419442

420443
class Describe_ColumnCells(object):
421444

0 commit comments

Comments
 (0)