Skip to content

Commit 210d7f1

Browse files
apteryksSteve Canny
authored andcommitted
acpt: add scenarios for cell access
* refactor tbl-item-access to remove cell access scenarios
1 parent acc4e3e commit 210d7f1

File tree

4 files changed

+78
-129
lines changed

4 files changed

+78
-129
lines changed

features/steps/table.py

Lines changed: 33 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
from docx import Document
1212
from docx.shared import Inches
13-
from docx.table import (
14-
_Cell, _Column, _ColumnCells, _Columns, _Row, _RowCells, _Rows
15-
)
13+
from docx.table import _Column, _Columns, _Row, _Rows
1614

1715
from helpers import test_docx
1816

@@ -24,11 +22,16 @@ def given_a_2x2_table(context):
2422
context.table_ = Document().add_table(rows=2, cols=2)
2523

2624

27-
@given('a column cell collection having two cells')
28-
def given_a_column_cell_collection_having_two_cells(context):
29-
docx_path = test_docx('blk-containing-table')
30-
document = Document(docx_path)
31-
context.cells = document.tables[0].columns[0].cells
25+
@given('a 3x3 table having {span_state}')
26+
def given_a_3x3_table_having_span_state(context, span_state):
27+
table_idx = {
28+
'only uniform cells': 0,
29+
'a horizontal span': 1,
30+
'a vertical span': 2,
31+
'a combined span': 3,
32+
}[span_state]
33+
document = Document(test_docx('tbl-cell-access'))
34+
context.table_ = document.tables[table_idx]
3235

3336

3437
@given('a column collection having two columns')
@@ -38,13 +41,6 @@ def given_a_column_collection_having_two_columns(context):
3841
context.columns = document.tables[0].columns
3942

4043

41-
@given('a row cell collection having two cells')
42-
def given_a_row_cell_collection_having_two_cells(context):
43-
docx_path = test_docx('blk-containing-table')
44-
document = Document(docx_path)
45-
context.cells = document.tables[0].rows[0].cells
46-
47-
4844
@given('a row collection having two rows')
4945
def given_a_row_collection_having_two_rows(context):
5046
docx_path = test_docx('blk-containing-table')
@@ -111,20 +107,6 @@ def given_a_table_having_two_rows(context):
111107
context.table_ = document.tables[0]
112108

113109

114-
@given('a table column having two cells')
115-
def given_a_table_column_having_two_cells(context):
116-
docx_path = test_docx('blk-containing-table')
117-
document = Document(docx_path)
118-
context.column = document.tables[0].columns[0]
119-
120-
121-
@given('a table row having two cells')
122-
def given_a_table_row_having_two_cells(context):
123-
docx_path = test_docx('blk-containing-table')
124-
document = Document(docx_path)
125-
context.row = document.tables[0].rows[0]
126-
127-
128110
# when =====================================================
129111

130112
@when('I add a column to the table')
@@ -166,15 +148,6 @@ def when_I_set_the_table_autofit_to_setting(context, setting):
166148

167149
# then =====================================================
168150

169-
@then('I can access a cell using its row and column indices')
170-
def then_can_access_cell_using_its_row_and_col_indices(context):
171-
table = context.table_
172-
for row_idx in range(2):
173-
for col_idx in range(2):
174-
cell = table.cell(row_idx, col_idx)
175-
assert isinstance(cell, _Cell)
176-
177-
178151
@then('I can access a collection column by index')
179152
def then_can_access_collection_column_by_index(context):
180153
columns = context.columns
@@ -191,36 +164,6 @@ def then_can_access_collection_row_by_index(context):
191164
assert isinstance(row, _Row)
192165

193166

194-
@then('I can access a column cell by index')
195-
def then_can_access_column_cell_by_index(context):
196-
cells = context.cells
197-
for idx in range(2):
198-
cell = cells[idx]
199-
assert isinstance(cell, _Cell)
200-
201-
202-
@then('I can access a row cell by index')
203-
def then_can_access_row_cell_by_index(context):
204-
cells = context.cells
205-
for idx in range(2):
206-
cell = cells[idx]
207-
assert isinstance(cell, _Cell)
208-
209-
210-
@then('I can access the cell collection of the column')
211-
def then_can_access_cell_collection_of_column(context):
212-
column = context.column
213-
cells = column.cells
214-
assert isinstance(cells, _ColumnCells)
215-
216-
217-
@then('I can access the cell collection of the row')
218-
def then_can_access_cell_collection_of_row(context):
219-
row = context.row
220-
cells = row.cells
221-
assert isinstance(cells, _RowCells)
222-
223-
224167
@then('I can access the column collection of the table')
225168
def then_can_access_column_collection_of_table(context):
226169
table = context.table_
@@ -235,37 +178,13 @@ def then_can_access_row_collection_of_table(context):
235178
assert isinstance(rows, _Rows)
236179

237180

238-
@then('I can get the length of the column cell collection')
239-
def then_can_get_length_of_column_cell_collection(context):
240-
column = context.column
241-
cells = column.cells
242-
assert len(cells) == 2
243-
244-
245-
@then('I can get the length of the row cell collection')
246-
def then_can_get_length_of_row_cell_collection(context):
247-
row = context.row
248-
cells = row.cells
249-
assert len(cells) == 2
250-
251-
252181
@then('I can get the table style name')
253182
def then_can_get_table_style_name(context):
254183
table = context.table_
255184
msg = "got '%s'" % table.style
256185
assert table.style == 'LightShading-Accent1', msg
257186

258187

259-
@then('I can iterate over the column cells')
260-
def then_can_iterate_over_the_column_cells(context):
261-
cells = context.cells
262-
actual_count = 0
263-
for cell in cells:
264-
actual_count += 1
265-
assert isinstance(cell, _Cell)
266-
assert actual_count == 2
267-
268-
269188
@then('I can iterate over the column collection')
270189
def then_can_iterate_over_column_collection(context):
271190
columns = context.columns
@@ -276,16 +195,6 @@ def then_can_iterate_over_column_collection(context):
276195
assert actual_count == 2
277196

278197

279-
@then('I can iterate over the row cells')
280-
def then_can_iterate_over_the_row_cells(context):
281-
cells = context.cells
282-
actual_count = 0
283-
for cell in cells:
284-
actual_count += 1
285-
assert isinstance(cell, _Cell)
286-
assert actual_count == 2
287-
288-
289198
@then('I can iterate over the row collection')
290199
def then_can_iterate_over_row_collection(context):
291200
rows = context.rows
@@ -296,6 +205,21 @@ def then_can_iterate_over_row_collection(context):
296205
assert actual_count == 2
297206

298207

208+
@then('table.cell({row}, {col}).text is {expected_text}')
209+
def then_table_cell_row_col_text_is_text(context, row, col, expected_text):
210+
table = context.table_
211+
row_idx, col_idx = int(row), int(col)
212+
cell_text = table.cell(row_idx, col_idx).text
213+
assert cell_text == expected_text, 'got %s' % cell_text
214+
215+
216+
@then('the column cells text is {expected_text}')
217+
def then_the_column_cells_text_is_expected_text(context, expected_text):
218+
table = context.table_
219+
cells_text = ' '.join(c.text for col in table.columns for c in col.cells)
220+
assert cells_text == expected_text, 'got %s' % cells_text
221+
222+
299223
@then('the length of the column collection is 2')
300224
def then_len_of_column_collection_is_2(context):
301225
columns = context.table_.columns
@@ -342,6 +266,13 @@ def then_the_reported_width_of_the_cell_is_width(context, width):
342266
)
343267

344268

269+
@then('the row cells text is {expected_text}')
270+
def then_the_row_cells_text_is_expected_text(context, expected_text):
271+
table = context.table_
272+
cells_text = ' '.join(c.text for row in table.rows for c in row.cells)
273+
assert cells_text == expected_text, 'got %s' % cells_text
274+
275+
345276
@then('the table style matches the name I applied')
346277
def then_table_style_matches_name_applied(context):
347278
table = context.table_
35.2 KB
Binary file not shown.

features/tbl-cell-access.feature

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Feature: Access table cells
2+
In order to access individual cells in a table
3+
As a developer using python-docx
4+
I need a way to access a cell from a table, row, or column
5+
6+
@wip
7+
Scenario Outline: Access cell sequence of a row
8+
Given a 3x3 table having <span-state>
9+
Then the row cells text is <expected-text>
10+
11+
Examples: Reported row cell contents
12+
| span-state | expected-text |
13+
| only uniform cells | 1 2 3 4 5 6 7 8 9 |
14+
| a horizontal span | 1 2 3 4 4 6 7 8 9 |
15+
| a vertical span | 1 2 3 4 5 6 7 5 9 |
16+
| a combined span | 1 2 3 4 4 6 4 4 9 |
17+
18+
19+
@wip
20+
Scenario Outline: Access cell sequence of a column
21+
Given a 3x3 table having <span-state>
22+
Then the column cells text is <expected-text>
23+
24+
Examples: Reported column cell contents
25+
| span-state | expected-text |
26+
| only uniform cells | 1 4 7 2 5 8 3 6 9 |
27+
| a horizontal span | 1 4 7 2 4 8 3 6 9 |
28+
| a vertical span | 1 4 7 2 5 5 3 6 9 |
29+
| a combined span | 1 4 4 2 4 4 3 6 9 |
30+
31+
32+
@wip
33+
Scenario Outline: Access cell by row and column index
34+
Given a 3x3 table having <span-state>
35+
Then table.cell(<row>, <col>).text is <expected-text>
36+
37+
Examples: Reported cell text
38+
| span-state | row | col | expected-text |
39+
| only uniform cells | 1 | 1 | 5 |
40+
| a horizontal span | 1 | 1 | 4 |
41+
| a vertical span | 2 | 1 | 5 |
42+
| a combined span | 2 | 1 | 4 |

features/tbl-item-access.feature

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Feature: Access table rows, columns, and cells
1+
Feature: Access table rows and columns
22
In order to query and modify individual table items
3-
As an python-docx developer
4-
I need the ability to access table rows, columns, and cells
3+
As a developer using python-docx
4+
I need the ability to access table rows and columns
55

66
Scenario: Access table row collection
77
Given a table having two rows
@@ -22,27 +22,3 @@ Feature: Access table rows, columns, and cells
2222
Given a column collection having two columns
2323
Then I can iterate over the column collection
2424
And I can access a collection column by index
25-
26-
Scenario: Access cell collection of table column
27-
Given a table column having two cells
28-
Then I can access the cell collection of the column
29-
And I can get the length of the column cell collection
30-
31-
Scenario: Access cell collection of table row
32-
Given a table row having two cells
33-
Then I can access the cell collection of the row
34-
And I can get the length of the row cell collection
35-
36-
Scenario: Access cell in column cell collection
37-
Given a column cell collection having two cells
38-
Then I can iterate over the column cells
39-
And I can access a column cell by index
40-
41-
Scenario: Access cell in row cell collection
42-
Given a row cell collection having two cells
43-
Then I can iterate over the row cells
44-
And I can access a row cell by index
45-
46-
Scenario: Access cell in table
47-
Given a table having two rows
48-
Then I can access a cell using its row and column indices

0 commit comments

Comments
 (0)