Skip to content

Commit 3d1534a

Browse files
author
Steve Canny
committed
acpt: add scenarios for _Cell.merge()
1 parent a8a49e1 commit 3d1534a

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

features/steps/table.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
Step implementations for table-related features
55
"""
66

7-
from __future__ import absolute_import, print_function, unicode_literals
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
810

911
from behave import given, then, when
1012

@@ -127,6 +129,17 @@ def when_apply_style_to_table(context):
127129
table.style = 'LightShading-Accent1'
128130

129131

132+
@when('I merge from cell {origin} to cell {other}')
133+
def when_I_merge_from_cell_origin_to_cell_other(context, origin, other):
134+
def cell(table, idx):
135+
row, col = idx // 3, idx % 3
136+
return table.cell(row, col)
137+
a_idx, b_idx = int(origin) - 1, int(other) - 1
138+
table = context.table_
139+
a, b = cell(table, a_idx), cell(table, b_idx)
140+
a.merge(b)
141+
142+
130143
@when('I set the cell width to {width}')
131144
def when_I_set_the_cell_width_to_width(context, width):
132145
new_value = {'1 inch': Inches(1)}[width]
@@ -266,8 +279,9 @@ def then_the_reported_width_of_the_cell_is_width(context, width):
266279
)
267280

268281

269-
@then('the row cells text is {expected_text}')
270-
def then_the_row_cells_text_is_expected_text(context, expected_text):
282+
@then('the row cells text is {encoded_text}')
283+
def then_the_row_cells_text_is_expected_text(context, encoded_text):
284+
expected_text = encoded_text.replace('\\', '\n')
271285
table = context.table_
272286
cells_text = ' '.join(c.text for row in table.rows for c in row.cells)
273287
assert cells_text == expected_text, 'got %s' % cells_text
@@ -292,3 +306,13 @@ def then_table_has_count_rows(context, count):
292306
row_count = int(count)
293307
rows = context.table_.rows
294308
assert len(rows) == row_count
309+
310+
311+
@then('the width of cell {n_str} is {inches_str} inches')
312+
def then_the_width_of_cell_n_is_x_inches(context, n_str, inches_str):
313+
def _cell(table, idx):
314+
row, col = idx // 3, idx % 3
315+
return table.cell(row, col)
316+
idx, inches = int(n_str) - 1, float(inches_str)
317+
cell = _cell(context.table_, idx)
318+
assert cell.width == Inches(inches), 'got %s' % cell.width.inches

features/tbl-merge-cells.feature

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Feature: Merge table cells
2+
In order to form a table cell spanning multiple rows and/or columns
3+
As a developer using python-docx
4+
I need a way to merge a range of cells
5+
6+
@wip
7+
Scenario Outline: Merge cells
8+
Given a 3x3 table having only uniform cells
9+
When I merge from cell <origin> to cell <other>
10+
Then the row cells text is <expected-text>
11+
12+
Examples: Reported row cell contents
13+
| origin | other | expected-text |
14+
| 1 | 2 | 1\2 1\2 3 4 5 6 7 8 9 |
15+
| 2 | 5 | 1 2\5 3 4 2\5 6 7 8 9 |
16+
| 5 | 9 | 1 2 3 4 5\6\8\9 5\6\8\9 7 5\6\8\9 5\6\8\9 |
17+
18+
19+
@wip
20+
Scenario Outline: Merge horizontal span with other cell
21+
Given a 3x3 table having a horizontal span
22+
When I merge from cell <origin> to cell <other>
23+
Then the row cells text is <expected-text>
24+
25+
Examples: Reported row cell contents
26+
| origin | other | expected-text |
27+
| 4 | 8 | 1 2 3 4\7\8 4\7\8 6 4\7\8 4\7\8 9 |
28+
| 4 | 6 | 1 2 3 4\6 4\6 4\6 7 8 9 |
29+
| 2 | 4 | 1\2\4 1\2\4 3 1\2\4 1\2\4 6 7 8 9 |
30+
31+
32+
@wip
33+
Scenario Outline: Merge vertical span with other cell
34+
Given a 3x3 table having a vertical span
35+
When I merge from cell <origin> to cell <other>
36+
Then the row cells text is <expected-text>
37+
38+
Examples: Reported row cell contents
39+
| origin | other | expected-text |
40+
| 5 | 9 | 1 2 3 4 5\6\9 5\6\9 7 5\6\9 5\6\9 |
41+
| 2 | 5 | 1 2\5 3 4 2\5 6 7 2\5 9 |
42+
| 7 | 5 | 1 2 3 4\5\7 4\5\7 6 4\5\7 4\5\7 9 |
43+
44+
45+
@wip
46+
Scenario Outline: Horizontal span adds cell widths
47+
Given a 3x3 table having <span-state>
48+
When I merge from cell <origin> to cell <other>
49+
Then the width of cell <merged> is <width> inches
50+
51+
Examples: Reported row cell contents
52+
| span-state | origin | other | merged | width |
53+
| only uniform cells | 1 | 2 | 1 | 2.0 |
54+
| only uniform cells | 1 | 5 | 1 | 2.0 |
55+
| a horizontal span | 4 | 6 | 4 | 3.0 |
56+
| a vertical span | 5 | 2 | 2 | 1.0 |
57+
| a vertical span | 5 | 7 | 5 | 2.0 |

0 commit comments

Comments
 (0)