X Tutup
Skip to content

Commit f933976

Browse files
author
Steve Canny
committed
acpt: add scenarios for Style.add_style()
1 parent faabd7b commit f933976

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

features/steps/styles.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313

1414
from helpers import test_docx
1515

16+
bool_vals = {
17+
'True': True,
18+
'False': False
19+
}
20+
21+
style_types = {
22+
'WD_STYLE_TYPE.CHARACTER': WD_STYLE_TYPE.CHARACTER,
23+
'WD_STYLE_TYPE.PARAGRAPH': WD_STYLE_TYPE.PARAGRAPH,
24+
'WD_STYLE_TYPE.LIST': WD_STYLE_TYPE.LIST,
25+
'WD_STYLE_TYPE.TABLE': WD_STYLE_TYPE.TABLE,
26+
}
27+
1628

1729
# given ===================================================
1830

@@ -22,6 +34,14 @@ def given_a_document_having_a_styles_part(context):
2234
context.document = Document(docx_path)
2335

2436

37+
@given('a document having known styles')
38+
def given_a_document_having_known_styles(context):
39+
docx_path = test_docx('sty-known-styles')
40+
document = Document(docx_path)
41+
context.document = document
42+
context.style_count = len(document.styles)
43+
44+
2545
@given('a document having no styles part')
2646
def given_a_document_having_no_styles_part(context):
2747
docx_path = test_docx('sty-having-no-styles-part')
@@ -47,6 +67,14 @@ def when_I_assign_a_new_value_to_style_style_id(context):
4767
context.style.style_id = 'Foo42'
4868

4969

70+
@when('I call add_style(\'{name}\', {type_str}, builtin={builtin_str})')
71+
def when_I_call_add_style(context, name, type_str, builtin_str):
72+
styles = context.document.styles
73+
type = style_types[type_str]
74+
builtin = bool_vals[builtin_str]
75+
styles.add_style(name, type, builtin=builtin)
76+
77+
5078
# then =====================================================
5179

5280
@then('I can access a style by its UI name')
@@ -82,6 +110,13 @@ def then_len_styles_is_style_count(context, style_count_str):
82110
assert len(context.document.styles) == int(style_count_str)
83111

84112

113+
@then('style.builtin is {builtin_str}')
114+
def then_style_builtin_is_builtin(context, builtin_str):
115+
style = context.style
116+
builtin = bool_vals[builtin_str]
117+
assert style.builtin == builtin
118+
119+
85120
@then('style.name is the {which} name')
86121
def then_style_name_is_the_which_name(context, which):
87122
expected_name = {
@@ -106,3 +141,25 @@ def then_style_style_id_is_the_which_style_id(context, which):
106141
def then_style_type_is_the_known_type(context):
107142
style = context.style
108143
assert style.type == WD_STYLE_TYPE.PARAGRAPH
144+
145+
146+
@then('style.type is {type_str}')
147+
def then_style_type_is_type(context, type_str):
148+
style = context.style
149+
style_type = style_types[type_str]
150+
assert style.type == style_type
151+
152+
153+
@then('styles[\'{name}\'] is a style')
154+
def then_styles_name_is_a_style(context, name):
155+
styles = context.document.styles
156+
style = context.style = styles[name]
157+
assert isinstance(style, BaseStyle)
158+
159+
160+
@then('the document has one additional style')
161+
def then_the_document_has_one_additional_style(context):
162+
document = context.document
163+
style_count = len(document.styles)
164+
expected_style_count = context.style_count + 1
165+
assert style_count == expected_style_count
21.2 KB
Binary file not shown.

features/sty-add-style.feature

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Feature: Add a style
2+
In order to customize the available styles in a document
3+
As a developer using python-docx
4+
I need a way to add a new style
5+
6+
7+
@wip
8+
Scenario Outline: Add a style
9+
Given a document having known styles
10+
When I call add_style('<name>', <type>, builtin=<builtin>)
11+
Then the document has one additional style
12+
And styles['<name>'] is a style
13+
And style.type is <type>
14+
And style.builtin is <builtin>
15+
16+
Examples: New style varieties
17+
| name | type | builtin |
18+
| Heading 1 | WD_STYLE_TYPE.PARAGRAPH | True |
19+
| Inline Code | WD_STYLE_TYPE.CHARACTER | False |
20+
| List Bullet | WD_STYLE_TYPE.LIST | True |
21+
| Shipments | WD_STYLE_TYPE.TABLE | False |

features/sty-style-props.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Feature: Get and set style properties
2-
In order to adjust styles to suit my needs
2+
In order to adjust a style to suit my needs
33
As a developer using python-docx
44
I need a set of read/write style properties
55

0 commit comments

Comments
 (0)
X Tutup