Skip to content

Commit 3700d2d

Browse files
author
Steve Canny
committed
acpt: rewrite table style scenarios
1 parent 1c9264e commit 3700d2d

File tree

3 files changed

+57
-35
lines changed

3 files changed

+57
-35
lines changed

features/steps/table.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,6 @@ def given_a_table_having_alignment_alignment(context, alignment):
8989
context.table_ = document.tables[table_idx]
9090

9191

92-
@given('a table having an applied style')
93-
def given_a_table_having_an_applied_style(context):
94-
docx_path = test_docx('tbl-having-applied-style')
95-
document = Document(docx_path)
96-
context.table_ = document.tables[0]
97-
98-
9992
@given('a table having an autofit layout of {autofit}')
10093
def given_a_table_having_an_autofit_layout_of_autofit(context, autofit):
10194
tbl_idx = {
@@ -107,6 +100,18 @@ def given_a_table_having_an_autofit_layout_of_autofit(context, autofit):
107100
context.table_ = document.tables[tbl_idx]
108101

109102

103+
@given('a table having {style} style')
104+
def given_a_table_having_style(context, style):
105+
table_idx = {
106+
'no explicit': 0,
107+
'Table Grid': 1,
108+
'Light Shading - Accent 1': 2,
109+
}[style]
110+
document = Document(test_docx('tbl-having-applied-style'))
111+
context.document = document
112+
context.table_ = document.tables[table_idx]
113+
114+
110115
@given('a table having two columns')
111116
def given_a_table_having_two_columns(context):
112117
docx_path = test_docx('blk-containing-table')
@@ -137,12 +142,6 @@ def when_add_row_to_table(context):
137142
context.row = table.add_row()
138143

139144

140-
@when('I apply a style to the table')
141-
def when_apply_style_to_table(context):
142-
table = context.table_
143-
table.style = 'LightShading-Accent1'
144-
145-
146145
@when('I assign {value_str} to table.alignment')
147146
def when_I_assign_value_to_table_alignment(context, value_str):
148147
value = {
@@ -155,6 +154,18 @@ def when_I_assign_value_to_table_alignment(context, value_str):
155154
table.alignment = value
156155

157156

157+
@when('I assign {value} to table.style')
158+
def when_apply_value_to_table_style(context, value):
159+
table, styles = context.table_, context.document.styles
160+
if value == 'None':
161+
new_value = None
162+
elif value.startswith('styles['):
163+
new_value = styles[value.split('\'')[1]]
164+
else:
165+
new_value = styles[value]
166+
table.style = new_value
167+
168+
158169
@when('I merge from cell {origin} to cell {other}')
159170
def when_I_merge_from_cell_origin_to_cell_other(context, origin, other):
160171
def cell(table, idx):
@@ -217,13 +228,6 @@ def then_can_access_row_collection_of_table(context):
217228
assert isinstance(rows, _Rows)
218229

219230

220-
@then('I can get the table style name')
221-
def then_can_get_table_style_name(context):
222-
table = context.table_
223-
msg = "got '%s'" % table.style
224-
assert table.style == 'LightShading-Accent1', msg
225-
226-
227231
@then('I can iterate over the column collection')
228232
def then_can_iterate_over_column_collection(context):
229233
columns = context.columns
@@ -264,6 +268,13 @@ def then_table_cell_row_col_text_is_text(context, row, col, expected_text):
264268
assert cell_text == expected_text, 'got %s' % cell_text
265269

266270

271+
@then('table.style is styles[\'{style_name}\']')
272+
def then_table_style_is_styles_style_name(context, style_name):
273+
table, styles = context.table_, context.document.styles
274+
expected_style = styles[style_name]
275+
assert table.style == expected_style, "got '%s'" % table.style
276+
277+
267278
@then('the column cells text is {expected_text}')
268279
def then_the_column_cells_text_is_expected_text(context, expected_text):
269280
table = context.table_
@@ -325,13 +336,6 @@ def then_the_row_cells_text_is_expected_text(context, encoded_text):
325336
assert cells_text == expected_text, 'got %s' % cells_text
326337

327338

328-
@then('the table style matches the name I applied')
329-
def then_table_style_matches_name_applied(context):
330-
table = context.table_
331-
tmpl = "table.style doesn't match, got '%s'"
332-
assert table.style == 'LightShading-Accent1', tmpl % table.style
333-
334-
335339
@then('the table has {count} columns')
336340
def then_table_has_count_columns(context, count):
337341
column_count = int(count)
Binary file not shown.

features/tbl-style.feature

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
Feature: Query and apply a table style
22
In order to maintain consistent formatting of tables
33
As a developer using python-docx
4-
I need the ability to query and apply a table style
4+
I need the ability to get and set the style of a table
55

6-
Scenario: Access table style
7-
Given a table having an applied style
8-
Then I can get the table style name
96

10-
Scenario: Apply table style
11-
Given a table
12-
When I apply a style to the table
13-
Then the table style matches the name I applied
7+
@wip
8+
Scenario Outline: Get the style of a table
9+
Given a table having <style> style
10+
Then table.style is styles['<value>']
11+
12+
Examples: Table styles
13+
| style | value |
14+
| no explicit | Normal Table |
15+
| Table Grid | Table Grid |
16+
| Light Shading - Accent 1 | Light Shading Accent 1 |
17+
18+
19+
@wip
20+
Scenario Outline: Apply a table style
21+
Given a table having <style> style
22+
When I assign <value> to table.style
23+
Then table.style is styles['<style-name>']
24+
25+
Examples: Character style transitions
26+
| style | value | style-name |
27+
| no explicit | Table Grid | Table Grid |
28+
| no explicit | styles['Table Grid'] | Table Grid |
29+
| Table Grid | Normal Table | Normal Table |
30+
| Table Grid | styles['Normal Table'] | Normal Table |
31+
| Table Grid | None | Normal Table |

0 commit comments

Comments
 (0)