99from behave import given , then
1010
1111from docx import Document
12- from docx .table import _Cell , _CellCollection , _Row
12+ from docx .table import (
13+ _Cell , _CellCollection , _Column , _ColumnCollection , _Row , _RowCollection
14+ )
1315
1416from .helpers import test_docx
1517
1618
1719# given ===================================================
1820
21+ @given ('a cell collection having two cells' )
22+ def given_a_cell_collection_having_two_cells (context ):
23+ docx_path = test_docx ('blk-containing-table' )
24+ document = Document (docx_path )
25+ context .cells = document .body .tables [0 ].rows [0 ].cells
26+
27+
28+ @given ('a column collection having two columns' )
29+ def given_a_column_collection_having_two_columns (context ):
30+ docx_path = test_docx ('blk-containing-table' )
31+ document = Document (docx_path )
32+ context .columns = document .body .tables [0 ].columns
33+
34+
35+ @given ('a row collection having two rows' )
36+ def given_a_row_collection_having_two_rows (context ):
37+ docx_path = test_docx ('blk-containing-table' )
38+ document = Document (docx_path )
39+ context .rows = document .body .tables [0 ].rows
40+
41+
42+ @given ('a table having two columns' )
43+ def given_a_table_having_two_columns (context ):
44+ docx_path = test_docx ('blk-containing-table' )
45+ document = Document (docx_path )
46+ # context.table is used internally by behave, underscore added
47+ # to distinguish this one
48+ context .table_ = document .body .tables [0 ]
49+
50+
1951@given ('a table having two rows' )
2052def given_a_table_having_two_rows (context ):
2153 docx_path = test_docx ('blk-containing-table' )
@@ -34,26 +66,47 @@ def given_a_table_row_having_two_cells(context):
3466
3567@then ('I can access a collection cell by index' )
3668def then_can_access_collection_cell_by_index (context ):
37- cells = context .row . cells
69+ cells = context .cells
3870 for idx in range (2 ):
3971 cell = cells [idx ]
4072 assert isinstance (cell , _Cell )
4173
4274
75+ @then ('I can access a collection column by index' )
76+ def then_can_access_collection_column_by_index (context ):
77+ columns = context .columns
78+ for idx in range (2 ):
79+ column = columns [idx ]
80+ assert isinstance (column , _Column )
81+
82+
83+ @then ('I can access a collection row by index' )
84+ def then_can_access_collection_row_by_index (context ):
85+ rows = context .rows
86+ for idx in range (2 ):
87+ row = rows [idx ]
88+ assert isinstance (row , _Row )
89+
90+
4391@then ('I can access the cell collection of the row' )
4492def then_can_access_cell_collection_of_row (context ):
4593 row = context .row
4694 cells = row .cells
4795 assert isinstance (cells , _CellCollection )
4896
4997
50- @then ('I can access the rows by index' )
51- def then_can_access_rows_by_index (context ):
52- rows = context .table_ .rows
53- row_count = len (rows )
54- for idx in range (row_count ):
55- row = rows [idx ]
56- assert isinstance (row , _Row )
98+ @then ('I can access the column collection of the table' )
99+ def then_can_access_column_collection_of_table (context ):
100+ table = context .table_
101+ columns = table .columns
102+ assert isinstance (columns , _ColumnCollection )
103+
104+
105+ @then ('I can access the row collection of the table' )
106+ def then_can_access_row_collection_of_table (context ):
107+ table = context .table_
108+ rows = table .rows
109+ assert isinstance (rows , _RowCollection )
57110
58111
59112@then ('I can get the length of the cell collection' )
@@ -65,25 +118,41 @@ def then_can_get_length_of_cell_collection(context):
65118
66119@then ('I can iterate over the cell collection' )
67120def then_can_iterate_over_cell_collection (context ):
68- row = context .row
121+ cells = context .cells
69122 actual_count = 0
70- for cell in row . cells :
123+ for cell in cells :
71124 actual_count += 1
72125 assert isinstance (cell , _Cell )
73126 assert actual_count == 2
74127
75128
76- @then ('the length of its row collection is 2' )
77- def then_len_of_row_collection_is_2 (context ):
78- rows = context .table_ .rows
79- assert len (rows ) == 2
129+ @then ('I can iterate over the column collection' )
130+ def then_can_iterate_over_column_collection (context ):
131+ columns = context .columns
132+ actual_count = 0
133+ for column in columns :
134+ actual_count += 1
135+ assert isinstance (column , _Column )
136+ assert actual_count == 2
137+
80138
139+ @then ('I can iterate over the row collection' )
140+ def then_can_iterate_over_row_collection (context ):
141+ rows = context .rows
142+ actual_count = 0
143+ for row in rows :
144+ actual_count += 1
145+ assert isinstance (row , _Row )
146+ assert actual_count == 2
81147
82- @then ('each item in its row collection is a table row' )
83- def then_each_item_in_row_collection_is_a_table_row (context ):
148+
149+ @then ('the length of the column collection is 2' )
150+ def then_len_of_column_collection_is_2 (context ):
151+ columns = context .table_ .columns
152+ assert len (columns ) == 2
153+
154+
155+ @then ('the length of the row collection is 2' )
156+ def then_len_of_row_collection_is_2 (context ):
84157 rows = context .table_ .rows
85- count = 0
86- for item in rows :
87- count += 1
88- assert isinstance (item , _Row )
89- assert count == 2
158+ assert len (rows ) == 2
0 commit comments