99from behave import given , then
1010
1111from docx import Document
12- from docx .table import _Row
12+ from docx .table import _Cell , _CellCollection , _Row
1313
1414from .helpers import test_docx
1515
@@ -23,8 +23,30 @@ def given_a_table_having_two_rows(context):
2323 context .table_ = document .body .tables [0 ]
2424
2525
26+ @given ('a table row having two cells' )
27+ def given_a_table_row_having_two_cells (context ):
28+ docx_path = test_docx ('blk-containing-table' )
29+ document = Document (docx_path )
30+ context .row = document .body .tables [0 ].rows [0 ]
31+
32+
2633# then =====================================================
2734
35+ @then ('I can access a collection cell by index' )
36+ def then_can_access_collection_cell_by_index (context ):
37+ cells = context .row .cells
38+ for idx in range (2 ):
39+ cell = cells [idx ]
40+ assert isinstance (cell , _Cell )
41+
42+
43+ @then ('I can access the cell collection of the row' )
44+ def then_can_access_cell_collection_of_row (context ):
45+ row = context .row
46+ cells = row .cells
47+ assert isinstance (cells , _CellCollection )
48+
49+
2850@then ('I can access the rows by index' )
2951def then_can_access_rows_by_index (context ):
3052 rows = context .table_ .rows
@@ -34,6 +56,23 @@ def then_can_access_rows_by_index(context):
3456 assert isinstance (row , _Row )
3557
3658
59+ @then ('I can get the length of the cell collection' )
60+ def then_can_get_length_of_cell_collection (context ):
61+ row = context .row
62+ cells = row .cells
63+ assert len (cells ) == 2
64+
65+
66+ @then ('I can iterate over the cell collection' )
67+ def then_can_iterate_over_cell_collection (context ):
68+ row = context .row
69+ actual_count = 0
70+ for cell in row .cells :
71+ actual_count += 1
72+ assert isinstance (cell , _Cell )
73+ assert actual_count == 2
74+
75+
3776@then ('the length of its row collection is 2' )
3877def then_len_of_row_collection_is_2 (context ):
3978 rows = context .table_ .rows
0 commit comments