Skip to content

Commit 3de03c8

Browse files
author
Steve Canny
committed
tbl: reimplement _Row.cells
Temporarily named _Row.cells_new
1 parent 725df27 commit 3de03c8

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

docx/table.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ def columns(self):
6767
"""
6868
return _Columns(self._tbl, self)
6969

70+
def row_cells(self, row_idx):
71+
"""
72+
Sequence of cells in the row at *row_idx* in this table.
73+
"""
74+
raise NotImplementedError
75+
7076
@lazyproperty
7177
def rows(self):
7278
"""
@@ -297,6 +303,27 @@ def cells(self):
297303
"""
298304
return _RowCells(self._tr, self)
299305

306+
@property
307+
def cells_new(self):
308+
"""
309+
Sequence of |_Cell| instances corresponding to cells in this row.
310+
"""
311+
return tuple(self.table.row_cells(self._index))
312+
313+
@property
314+
def table(self):
315+
"""
316+
Reference to the |Table| object this row belongs to.
317+
"""
318+
raise NotImplementedError
319+
320+
@property
321+
def _index(self):
322+
"""
323+
Index of this row in its table, starting from zero.
324+
"""
325+
raise NotImplementedError
326+
300327

301328
class _RowCells(Parented):
302329
"""

features/steps/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def then_the_reported_width_of_the_cell_is_width(context, width):
269269
@then('the row cells text is {expected_text}')
270270
def then_the_row_cells_text_is_expected_text(context, expected_text):
271271
table = context.table_
272-
cells_text = ' '.join(c.text for row in table.rows for c in row.cells)
272+
cells_text = ' '.join(c.text for row in table.rows for c in row.cells_new)
273273
assert cells_text == expected_text, 'got %s' % cells_text
274274

275275

tests/test_table.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .oxml.unitdata.table import a_gridCol, a_tbl, a_tblGrid, a_tc, a_tr
1818
from .oxml.unitdata.text import a_p
1919
from .unitutil.cxml import element, xml
20+
from .unitutil.mock import instance_mock, property_mock
2021

2122

2223
class DescribeTable(object):
@@ -429,11 +430,41 @@ def columns_fixture(self):
429430

430431
class Describe_Row(object):
431432

433+
def it_provides_access_to_its_cells(self, cells_fixture):
434+
row, row_idx, expected_cells = cells_fixture
435+
cells = row.cells_new
436+
row.table.row_cells.assert_called_once_with(row_idx)
437+
assert cells == expected_cells
438+
432439
def it_provides_access_to_the_row_cells(self):
433440
row = _Row(element('w:tr'), None)
434441
cells = row.cells
435442
assert isinstance(cells, _RowCells)
436443

444+
# fixtures -------------------------------------------------------
445+
446+
@pytest.fixture
447+
def cells_fixture(self, _index_, table_prop_, table_):
448+
row = _Row(None, None)
449+
_index_.return_value = row_idx = 6
450+
expected_cells = (1, 2, 3)
451+
table_.row_cells.return_value = list(expected_cells)
452+
return row, row_idx, expected_cells
453+
454+
# fixture components ---------------------------------------------
455+
456+
@pytest.fixture
457+
def _index_(self, request):
458+
return property_mock(request, _Row, '_index')
459+
460+
@pytest.fixture
461+
def table_(self, request):
462+
return instance_mock(request, Table)
463+
464+
@pytest.fixture
465+
def table_prop_(self, request, table_):
466+
return property_mock(request, _Row, 'table', return_value=table_)
467+
437468

438469
class Describe_RowCells(object):
439470

0 commit comments

Comments
 (0)