Skip to content

Commit c04bdde

Browse files
author
Steve Canny
committed
acpt: add Table.columns access scenario
Along the way, make collection scenarios parallel
1 parent baceb41 commit c04bdde

File tree

3 files changed

+119
-28
lines changed

3 files changed

+119
-28
lines changed

docx/table.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ def __init__(self, gridCol):
8686
self._gridCol = gridCol
8787

8888

89+
class _ColumnCollection(object):
90+
"""
91+
Sequence of |_Column| instances corresponding to the columns in a table.
92+
"""
93+
94+
8995
class _Row(object):
9096
"""
9197
Table row

features/steps/table.py

Lines changed: 91 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,45 @@
99
from behave import given, then
1010

1111
from 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

1416
from .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')
2052
def 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')
3668
def 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')
4492
def 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')
67120
def 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

features/tbl-item-access.feature

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,32 @@ Feature: Access table rows, columns, and cells
55

66
Scenario: Access table row collection
77
Given a table having two rows
8-
Then the length of its row collection is 2
9-
And each item in its row collection is a table row
8+
Then I can access the row collection of the table
9+
And the length of the row collection is 2
1010

11-
Scenario: Access table row by collection index
12-
Given a table having two rows
13-
Then I can access the rows by index
11+
Scenario: Access row in row collection
12+
Given a row collection having two rows
13+
Then I can iterate over the row collection
14+
And I can access a collection row by index
15+
16+
@wip
17+
Scenario: Access table column collection
18+
Given a table having two columns
19+
Then I can access the column collection of the table
20+
And the length of the column collection is 2
21+
22+
@wip
23+
Scenario: Access column in column collection
24+
Given a column collection having two columns
25+
Then I can iterate over the column collection
26+
And I can access a collection column by index
1427

1528
Scenario: Access cell collection of table row
1629
Given a table row having two cells
1730
Then I can access the cell collection of the row
1831
And I can get the length of the cell collection
19-
And I can iterate over the cell collection
32+
33+
Scenario: Access cell in cell collection
34+
Given a cell collection having two cells
35+
Then I can iterate over the cell collection
2036
And I can access a collection cell by index

0 commit comments

Comments
 (0)