Skip to content

Commit cbf3d5c

Browse files
author
Steve Canny
committed
acpt: add par-insert-paragraph.feature
Acceptance test for Paragraph.insert_paragraph_before() API method.
1 parent 488a4ad commit cbf3d5c

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Feature: Insert a paragraph before or after a paragraph
2+
In order to add new content in the middle of an existing document
3+
As a developer using python-docx
4+
I need a way to insert a paragraph relative to another paragraph
5+
6+
7+
@wip
8+
Scenario: Add a new paragraph above an existing paragraph
9+
Given a document containing three paragraphs
10+
When I insert a paragraph above the second paragraph
11+
Then the document contains four paragraphs
12+
And the text of the second paragraph matches the text I set
13+
And the style of the second paragraph matches the style I set

features/steps/paragraph.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Step implementations for paragraph-related features
55
"""
66

7-
from behave import then, when
7+
from behave import given, then, when
88

99
from docx import Document
1010

@@ -14,6 +14,17 @@
1414
TEST_STYLE = 'Heading1'
1515

1616

17+
# given ===================================================
18+
19+
@given('a document containing three paragraphs')
20+
def given_a_document_containing_three_paragraphs(context):
21+
document = Document()
22+
document.add_paragraph('foo')
23+
document.add_paragraph('bar')
24+
document.add_paragraph('baz')
25+
context.document = document
26+
27+
1728
# when ====================================================
1829

1930
@when('I add a run to the paragraph')
@@ -26,6 +37,12 @@ def when_add_new_text_to_run(context):
2637
context.r.add_text(test_text)
2738

2839

40+
@when('I insert a paragraph above the second paragraph')
41+
def when_I_insert_a_paragraph_above_the_second_paragraph(context):
42+
paragraph = context.document.paragraphs[1]
43+
paragraph.insert_paragraph_before('foobar', 'Heading1')
44+
45+
2946
@when('I set the paragraph style')
3047
def when_I_set_the_paragraph_style(context):
3148
context.paragraph.add_run().add_text(test_text)
@@ -34,6 +51,11 @@ def when_I_set_the_paragraph_style(context):
3451

3552
# then =====================================================
3653

54+
@then('the document contains four paragraphs')
55+
def then_the_document_contains_four_paragraphs(context):
56+
assert len(context.document.paragraphs) == 4
57+
58+
3759
@then('the document contains the text I added')
3860
def then_document_contains_text_I_added(context):
3961
document = Document(saved_docx_path)
@@ -47,3 +69,15 @@ def then_document_contains_text_I_added(context):
4769
def then_the_paragraph_has_the_style_I_set(context):
4870
paragraph = Document(saved_docx_path).paragraphs[-1]
4971
assert paragraph.style == TEST_STYLE
72+
73+
74+
@then('the style of the second paragraph matches the style I set')
75+
def then_the_style_of_the_second_paragraph_matches_the_style_I_set(context):
76+
second_paragraph = context.document.paragraphs[1]
77+
assert second_paragraph.style == 'Heading1'
78+
79+
80+
@then('the text of the second paragraph matches the text I set')
81+
def then_the_text_of_the_second_paragraph_matches_the_text_I_set(context):
82+
second_paragraph = context.document.paragraphs[1]
83+
assert second_paragraph.text == 'foobar'

0 commit comments

Comments
 (0)