Skip to content

Commit a80f0e3

Browse files
Victor VolleSteve Canny
authored andcommitted
acpt: add par-add-run.feature
Retroactively added an acceptance test for Paragraph.add_run(). Made some stylistic updates to naming in steps along the way.
1 parent 16afd9b commit a80f0e3

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

features/par-add-run.feature

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Feature: Add a run with optional text and style
2+
In order to add distinctively formatted text to a paragraph
3+
As a python-docx programmer
4+
I want a way to add a styled run of text in a single step
5+
6+
Scenario: Add a run specifying its text
7+
Given a paragraph
8+
When I add a run specifying its text
9+
Then the run contains the text I specified
10+
11+
@wip
12+
Scenario: Add a run specifying its style
13+
Given a paragraph
14+
When I add a run specifying the character style Emphasis
15+
Then the style of the run is Emphasis

features/steps/block.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def given_a_document_containing_a_table(context):
2323
@given('a paragraph')
2424
def given_a_paragraph(context):
2525
context.document = Document()
26-
context.p = context.document.add_paragraph()
26+
context.paragraph = context.document.add_paragraph()
2727

2828

2929
# when ====================================================

features/steps/paragraph.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,32 @@
1010

1111
from helpers import saved_docx_path, test_text
1212

13-
test_style = 'Heading1'
13+
14+
TEST_STYLE = 'Heading1'
1415

1516

1617
# when ====================================================
1718

1819
@when('I add a run to the paragraph')
19-
def step_when_add_new_run_to_paragraph(context):
20+
def when_add_new_run_to_paragraph(context):
2021
context.r = context.p.add_run()
2122

2223

2324
@when('I add text to the run')
24-
def step_when_add_new_text_to_run(context):
25+
def when_add_new_text_to_run(context):
2526
context.r.add_text(test_text)
2627

2728

2829
@when('I set the paragraph style')
29-
def step_when_set_paragraph_style(context):
30-
context.p.add_run().add_text(test_text)
31-
context.p.style = test_style
30+
def when_I_set_the_paragraph_style(context):
31+
context.paragraph.add_run().add_text(test_text)
32+
context.paragraph.style = TEST_STYLE
3233

3334

3435
# then =====================================================
3536

3637
@then('the document contains the text I added')
37-
def step_then_document_contains_text_I_added(context):
38+
def then_document_contains_text_I_added(context):
3839
document = Document(saved_docx_path)
3940
paragraphs = document.paragraphs
4041
p = paragraphs[-1]
@@ -43,8 +44,6 @@ def step_then_document_contains_text_I_added(context):
4344

4445

4546
@then('the paragraph has the style I set')
46-
def step_then_paragraph_has_the_style_I_set(context):
47-
document = Document(saved_docx_path)
48-
paragraphs = document.paragraphs
49-
p = paragraphs[-1]
50-
assert p.style == test_style
47+
def then_the_paragraph_has_the_style_I_set(context):
48+
paragraph = Document(saved_docx_path).paragraphs[-1]
49+
assert paragraph.style == TEST_STYLE

features/steps/text.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from docx.enum.text import WD_BREAK
1313
from docx.oxml.shared import qn
1414

15-
from .helpers import test_docx
15+
from .helpers import test_docx, test_text
1616

1717

1818
# given ===================================================
@@ -59,6 +59,16 @@ def when_add_page_break(context):
5959
run.add_break(WD_BREAK.PAGE)
6060

6161

62+
@when('I add a run specifying its text')
63+
def when_I_add_a_run_specifying_its_text(context):
64+
context.run = context.paragraph.add_run(test_text)
65+
66+
67+
@when('I add a run specifying the character style Emphasis')
68+
def when_I_add_a_run_specifying_the_character_style_Emphasis(context):
69+
context.run = context.paragraph.add_run(test_text, 'Emphasis')
70+
71+
6272
@when('I assign {value_str} to its {bool_prop_name} property')
6373
def when_assign_true_to_bool_run_prop(context, value_str, bool_prop_name):
6474
value = {'True': True, 'False': False, 'None': None}[value_str]
@@ -122,6 +132,11 @@ def then_run_appears_without_bool_prop(context, boolean_prop_name):
122132
assert getattr(run, boolean_prop_name) is False
123133

124134

135+
@then('the run contains the text I specified')
136+
def then_the_run_contains_the_text_I_specified(context):
137+
assert context.run.text == test_text
138+
139+
125140
@then('the style of the run is {char_style}')
126141
def then_the_style_of_the_run_is_char_style(context, char_style):
127142
expected_value = {

0 commit comments

Comments
 (0)