44Step implementations for paragraph-related features
55"""
66
7- from behave import then , when
7+ from behave import given , then , when
88
99from docx import Document
1010
1414TEST_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' )
3047def 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' )
3860def 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):
4769def 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