1515from docx .text import Paragraph
1616
1717from .oxml .unitdata .parts import a_body
18- from .oxml .unitdata .table import a_tbl
18+ from .oxml .unitdata .table import (
19+ a_gridCol , a_tbl , a_tblGrid , a_tblPr , a_tc , a_tr
20+ )
1921from .oxml .unitdata .text import a_p , a_sectPr
2022from .unitutil import class_mock , function_mock , initializer_mock
2123
@@ -80,12 +82,18 @@ def serialize_part_xml_(self, request):
8082
8183class Describe_Body (object ):
8284
83- def it_can_add_a_paragraph_to_itself (self , add_paragraph_fixture ):
85+ def it_can_add_a_paragraph (self , add_paragraph_fixture ):
8486 body , expected_xml = add_paragraph_fixture
8587 p = body .add_paragraph ()
8688 assert body ._body .xml == expected_xml
8789 assert isinstance (p , Paragraph )
8890
91+ def it_can_add_a_table (self , add_table_fixture ):
92+ body , expected_xml = add_table_fixture
93+ table = body .add_table (rows = 1 , cols = 1 )
94+ assert body ._body .xml == expected_xml
95+ assert isinstance (table , Table )
96+
8997 def it_can_clear_itself_of_all_content_it_holds (
9098 self , clear_content_fixture ):
9199 body , expected_xml = clear_content_fixture
@@ -112,28 +120,34 @@ def it_provides_access_to_the_tables_it_contains(
112120 # fixtures -------------------------------------------------------
113121
114122 @pytest .fixture (params = [
115- (False , False ), (True , False ), (False , True ), (True , True )
123+ (0 , False ), (1 , False ), (0 , True ), (1 , True )
116124 ])
117125 def add_paragraph_fixture (self , request ):
118- has_p , has_sectPr = request .param
126+ p_count , has_sectPr = request .param
119127 # body element -----------------
120- body_bldr = a_body ().with_nsdecls ()
121- if has_p :
122- body_bldr .with_child (a_p ())
123- if has_sectPr :
124- body_bldr .with_child (a_sectPr ())
128+ body_bldr = self ._body_bldr (p_count = p_count , sectPr = has_sectPr )
125129 body_elm = body_bldr .element
126130 body = _Body (body_elm )
127131 # expected XML -----------------
128- body_bldr = a_body ().with_nsdecls ()
129- if has_p :
130- body_bldr .with_child (a_p ())
131- body_bldr .with_child (a_p ())
132- if has_sectPr :
133- body_bldr .with_child (a_sectPr ())
132+ p_count += 1
133+ body_bldr = self ._body_bldr (p_count = p_count , sectPr = has_sectPr )
134134 expected_xml = body_bldr .xml ()
135135 return body , expected_xml
136136
137+ @pytest .fixture (params = [(0 , False ), (0 , True ), (1 , False ), (1 , True )])
138+ def add_table_fixture (self , request ):
139+ p_count , has_sectPr = request .param
140+ body_bldr = self ._body_bldr (p_count = p_count , sectPr = has_sectPr )
141+ body = _Body (body_bldr .element )
142+
143+ tbl_bldr = self ._tbl_bldr ()
144+ body_bldr = self ._body_bldr (
145+ p_count = p_count , tbl_bldr = tbl_bldr , sectPr = has_sectPr
146+ )
147+ expected_xml = body_bldr .xml ()
148+
149+ return body , expected_xml
150+
137151 @pytest .fixture
138152 def body_with_paragraphs (self ):
139153 body_elm = (
@@ -170,3 +184,39 @@ def clear_content_fixture(self, request):
170184 body_bldr .with_child (a_sectPr ())
171185 expected_xml = body_bldr .xml ()
172186 return body , expected_xml
187+
188+ def _body_bldr (self , p_count = 0 , tbl_bldr = None , sectPr = False ):
189+ body_bldr = a_body ().with_nsdecls ()
190+ for i in range (p_count ):
191+ body_bldr .with_child (a_p ())
192+ if tbl_bldr is not None :
193+ body_bldr .with_child (tbl_bldr )
194+ if sectPr :
195+ body_bldr .with_child (a_sectPr ())
196+ return body_bldr
197+
198+ def _tbl_bldr (self , rows = 1 , cols = 1 ):
199+ tblPr_bldr = a_tblPr ()
200+
201+ tblGrid_bldr = a_tblGrid ()
202+ for i in range (cols ):
203+ tblGrid_bldr .with_child (a_gridCol ())
204+
205+ tbl_bldr = a_tbl ()
206+ tbl_bldr .with_child (tblPr_bldr )
207+ tbl_bldr .with_child (tblGrid_bldr )
208+ for i in range (rows ):
209+ tr_bldr = self ._tr_bldr (cols )
210+ tbl_bldr .with_child (tr_bldr )
211+
212+ return tbl_bldr
213+
214+ def _tc_bldr (self ):
215+ return a_tc ().with_child (a_p ())
216+
217+ def _tr_bldr (self , cols ):
218+ tr_bldr = a_tr ()
219+ for i in range (cols ):
220+ tc_bldr = self ._tc_bldr ()
221+ tr_bldr .with_child (tc_bldr )
222+ return tr_bldr
0 commit comments