77from behave import given , then , when
88
99from docx import Document
10+ from docx .oxml import parse_xml
11+ from docx .oxml .ns import nsdecls
12+ from docx .text import Paragraph
1013
1114from helpers import saved_docx_path , test_text
1215
@@ -25,6 +28,21 @@ def given_a_document_containing_three_paragraphs(context):
2528 context .document = document
2629
2730
31+ @given ('a paragraph with content and formatting' )
32+ def given_a_paragraph_with_content_and_formatting (context ):
33+ p_xml = """\
34+ <w:p %s>
35+ <w:pPr>
36+ <w:pStyle w:val="%s"/>
37+ </w:pPr>
38+ <w:r>
39+ <w:t>foobar</w:t>
40+ </w:r>
41+ </w:p>""" % (nsdecls ('w' ), TEST_STYLE )
42+ p = parse_xml (p_xml )
43+ context .paragraph = Paragraph (p )
44+
45+
2846# when ====================================================
2947
3048@when ('I add a run to the paragraph' )
@@ -33,10 +51,15 @@ def when_add_new_run_to_paragraph(context):
3351
3452
3553@when ('I add text to the run' )
36- def when_add_new_text_to_run (context ):
54+ def when_I_add_text_to_the_run (context ):
3755 context .r .add_text (test_text )
3856
3957
58+ @when ('I clear the paragraph content' )
59+ def when_I_clear_the_paragraph_content (context ):
60+ context .paragraph .clear ()
61+
62+
4063@when ('I insert a paragraph above the second paragraph' )
4164def when_I_insert_a_paragraph_above_the_second_paragraph (context ):
4265 paragraph = context .document .paragraphs [1 ]
@@ -65,6 +88,16 @@ def then_document_contains_text_I_added(context):
6588 assert r .text == test_text
6689
6790
91+ @then ('the paragraph formatting is preserved' )
92+ def then_the_paragraph_formatting_is_preserved (context ):
93+ assert context .paragraph .style == TEST_STYLE
94+
95+
96+ @then ('the paragraph has no content' )
97+ def then_the_paragraph_has_no_content (context ):
98+ assert context .paragraph .text == ''
99+
100+
68101@then ('the paragraph has the style I set' )
69102def then_the_paragraph_has_the_style_I_set (context ):
70103 paragraph = Document (saved_docx_path ).paragraphs [- 1 ]
0 commit comments