44Test suite for the docx.parts module
55"""
66
7+ from __future__ import absolute_import , print_function , unicode_literals
8+
79from docx .parts import _Body , _Document
810
911import pytest
1315from docx .text import Paragraph
1416
1517from .oxml .unitdata .parts import a_body
16- from .oxml .unitdata .text import a_p
18+ from .oxml .unitdata .text import a_p , a_sectPr
1719from .unitutil import class_mock , function_mock , initializer_mock
1820
1921
@@ -77,21 +79,11 @@ def serialize_part_xml_(self, request):
7779
7880class Describe_Body (object ):
7981
80- def it_can_add_a_paragraph_to_itself (self , Paragraph_ ):
81- # mockery ----------------------
82- body_elm = Mock (name = 'body_elm' )
83- body_elm .add_p .return_value = p_elm = Mock (name = 'p_elm' )
84- body = _Body (body_elm )
85- # exercise ---------------------
82+ def it_can_add_a_paragraph_to_itself (self , add_paragraph_fixture ):
83+ body , expected_xml = add_paragraph_fixture
8684 p = body .add_paragraph ()
87- # verify -----------------------
88- body_elm .add_p .assert_called_once_with ()
89- Paragraph_ .assert_called_once_with (p_elm )
90- assert p is Paragraph_ .return_value
91-
92- def it_returns_an_empty_sequence_when_it_contains_no_paragraphs (self ):
93- body = _Body (a_body ().with_nsdecls ().element )
94- assert body .paragraphs == []
85+ assert body ._body .xml == expected_xml
86+ assert isinstance (p , Paragraph )
9587
9688 def it_can_clear_itself_of_all_content_it_holds (self ):
9789 # mockery ----------------------
@@ -113,6 +105,29 @@ def it_provides_access_to_the_paragraphs_it_contains(
113105
114106 # fixtures -------------------------------------------------------
115107
108+ @pytest .fixture (params = [
109+ (False , False ), (True , False ), (False , True ), (True , True )
110+ ])
111+ def add_paragraph_fixture (self , request ):
112+ has_p , has_sectPr = request .param
113+ # body element -----------------
114+ body_bldr = a_body ().with_nsdecls ()
115+ if has_p :
116+ body_bldr .with_child (a_p ())
117+ if has_sectPr :
118+ body_bldr .with_child (a_sectPr ())
119+ body_elm = body_bldr .element
120+ body = _Body (body_elm )
121+ # expected XML -----------------
122+ body_bldr = a_body ().with_nsdecls ()
123+ if has_p :
124+ body_bldr .with_child (a_p ())
125+ body_bldr .with_child (a_p ())
126+ if has_sectPr :
127+ body_bldr .with_child (a_sectPr ())
128+ expected_xml = body_bldr .xml ()
129+ return body , expected_xml
130+
116131 @pytest .fixture
117132 def body_with_paragraphs (self ):
118133 body_elm = (
@@ -122,7 +137,3 @@ def body_with_paragraphs(self):
122137 .element
123138 )
124139 return _Body (body_elm )
125-
126- @pytest .fixture
127- def Paragraph_ (self , request ):
128- return class_mock (request , 'docx.parts.Paragraph' )
0 commit comments