|
9 | 9 | from behave import given, then, when |
10 | 10 |
|
11 | 11 | from docx import Document |
| 12 | +from docx.shared import Inches |
12 | 13 | from docx.table import ( |
13 | 14 | _Cell, _Column, _ColumnCells, _Columns, _Row, _RowCells, _Rows |
14 | 15 | ) |
@@ -56,6 +57,15 @@ def given_a_table(context): |
56 | 57 | context.table_ = Document().add_table(rows=2, cols=2) |
57 | 58 |
|
58 | 59 |
|
| 60 | +@given('a table cell having a width of {width}') |
| 61 | +def given_a_table_cell_having_a_width_of_width(context, width): |
| 62 | + table_idx = {'no explicit setting': 0, '1 inch': 1, '2 inches': 2}[width] |
| 63 | + document = Document(test_docx('tbl-props')) |
| 64 | + table = document.tables[table_idx] |
| 65 | + cell = table.cell(0, 0) |
| 66 | + context.cell = cell |
| 67 | + |
| 68 | + |
59 | 69 | @given('a table column having a width of {width_desc}') |
60 | 70 | def given_a_table_having_a_width_of_width_desc(context, width_desc): |
61 | 71 | col_idx = { |
@@ -135,6 +145,12 @@ def when_apply_style_to_table(context): |
135 | 145 | table.style = 'LightShading-Accent1' |
136 | 146 |
|
137 | 147 |
|
| 148 | +@when('I set the cell width to {width}') |
| 149 | +def when_I_set_the_cell_width_to_width(context, width): |
| 150 | + new_value = {'1 inch': Inches(1)}[width] |
| 151 | + context.cell.width = new_value |
| 152 | + |
| 153 | + |
138 | 154 | @when('I set the column width to {width_emu}') |
139 | 155 | def when_I_set_the_column_width_to_width_emu(context, width_emu): |
140 | 156 | new_value = None if width_emu == 'None' else int(width_emu) |
@@ -317,6 +333,15 @@ def then_the_reported_column_width_is_width_emu(context, width_emu): |
317 | 333 | ) |
318 | 334 |
|
319 | 335 |
|
| 336 | +@then('the reported width of the cell is {width}') |
| 337 | +def then_the_reported_width_of_the_cell_is_width(context, width): |
| 338 | + expected_width = {'None': None, '1 inch': Inches(1)}[width] |
| 339 | + actual_width = context.cell.width |
| 340 | + assert actual_width == expected_width, ( |
| 341 | + 'expected %s, got %s' % (expected_width, actual_width) |
| 342 | + ) |
| 343 | + |
| 344 | + |
320 | 345 | @then('the table style matches the name I applied') |
321 | 346 | def then_table_style_matches_name_applied(context): |
322 | 347 | table = context.table_ |
|
0 commit comments