1212from docx .section import Section
1313from docx .shared import Inches
1414
15- from .oxml .unitdata .section import a_pgMar , a_pgSz , a_sectPr , a_type
15+ from .oxml .unitdata .section import a_pgMar , a_sectPr
16+ from .unitutil .cxml import element , xml
1617
1718
1819class DescribeSection (object ):
@@ -33,6 +34,7 @@ def it_knows_its_page_width(self, page_width_get_fixture):
3334 def it_can_change_its_page_width (self , page_width_set_fixture ):
3435 section , new_page_width , expected_xml = page_width_set_fixture
3536 section .page_width = new_page_width
37+ assert section ._sectPr .xml == expected_xml
3638
3739 def it_knows_its_page_height (self , page_height_get_fixture ):
3840 section , expected_page_height = page_height_get_fixture
@@ -128,120 +130,100 @@ def margins_set_fixture(self, request):
128130 return section , property_name , new_value , expected_xml
129131
130132 @pytest .fixture (params = [
131- (True , ' landscape' , WD_ORIENT .LANDSCAPE ),
132- (True , ' portrait' , WD_ORIENT .PORTRAIT ),
133- (True , None , WD_ORIENT .PORTRAIT ),
134- (False , None , WD_ORIENT .PORTRAIT ),
133+ ('w:sectPr/w:pgSz{w:orient= landscape} ' , WD_ORIENT .LANDSCAPE ),
134+ ('w:sectPr/w:pgSz{w:orient= portrait} ' , WD_ORIENT .PORTRAIT ),
135+ ('w:sectPr/w:pgSz' , WD_ORIENT .PORTRAIT ),
136+ ('w:sectPr' , WD_ORIENT .PORTRAIT ),
135137 ])
136138 def orientation_get_fixture (self , request ):
137- has_pgSz_child , orient , expected_orientation = request .param
138- pgSz_bldr = self .pgSz_bldr (has_pgSz_child , orient = orient )
139- sectPr = self .sectPr_bldr (pgSz_bldr ).element
140- section = Section (sectPr )
139+ sectPr_cxml , expected_orientation = request .param
140+ section = Section (element (sectPr_cxml ))
141141 return section , expected_orientation
142142
143143 @pytest .fixture (params = [
144- (WD_ORIENT .LANDSCAPE , 'landscape' ),
145- (WD_ORIENT .PORTRAIT , None ),
146- (None , None ),
144+ (WD_ORIENT .LANDSCAPE , 'w:sectPr/w:pgSz{w:orient= landscape} ' ),
145+ (WD_ORIENT .PORTRAIT , 'w:sectPr/w:pgSz' ),
146+ (None , 'w:sectPr/w:pgSz' ),
147147 ])
148148 def orientation_set_fixture (self , request ):
149- new_orientation , expected_orient_val = request .param
150- # section ----------------------
151- sectPr = self .sectPr_bldr ().element
152- section = Section (sectPr )
153- # expected_xml -----------------
154- pgSz_bldr = self .pgSz_bldr (orient = expected_orient_val )
155- expected_xml = self .sectPr_bldr (pgSz_bldr ).xml ()
149+ new_orientation , expected_cxml = request .param
150+ section = Section (element ('w:sectPr' ))
151+ expected_xml = xml (expected_cxml )
156152 return section , new_orientation , expected_xml
157153
158154 @pytest .fixture (params = [
159- (True , 2880 , Inches (2 )),
160- (True , None , None ),
161- (False , None , None ),
155+ ('w:sectPr/w:pgSz{w:h= 2880}' , Inches (2 )),
156+ ('w:sectPr/w:pgSz' , None ),
157+ ('w:sectPr' , None ),
162158 ])
163159 def page_height_get_fixture (self , request ):
164- has_pgSz_child , h , expected_page_height = request .param
165- pgSz_bldr = self .pgSz_bldr (has_pgSz_child , h = h )
166- sectPr = self .sectPr_bldr (pgSz_bldr ).element
167- section = Section (sectPr )
160+ sectPr_cxml , expected_page_height = request .param
161+ section = Section (element (sectPr_cxml ))
168162 return section , expected_page_height
169163
170164 @pytest .fixture (params = [
171- (None , None ),
172- (Inches (2 ), 2880 ),
165+ (None , 'w:sectPr/w:pgSz' ),
166+ (Inches (2 ), 'w:sectPr/w:pgSz{w:h= 2880}' ),
173167 ])
174168 def page_height_set_fixture (self , request ):
175- new_page_height , expected_h_val = request .param
176- # section ----------------------
177- sectPr = self .sectPr_bldr ().element
178- section = Section (sectPr )
179- # expected_xml -----------------
180- pgSz_bldr = self .pgSz_bldr (h = expected_h_val )
181- expected_xml = self .sectPr_bldr (pgSz_bldr ).xml ()
169+ new_page_height , expected_cxml = request .param
170+ section = Section (element ('w:sectPr' ))
171+ expected_xml = xml (expected_cxml )
182172 return section , new_page_height , expected_xml
183173
184174 @pytest .fixture (params = [
185- (True , 1440 , Inches (1 )),
186- (True , None , None ),
187- (False , None , None ),
175+ ('w:sectPr/w:pgSz{w:w= 1440}' , Inches (1 )),
176+ ('w:sectPr/w:pgSz' , None ),
177+ ('w:sectPr' , None ),
188178 ])
189179 def page_width_get_fixture (self , request ):
190- has_pgSz_child , w , expected_page_width = request .param
191- pgSz_bldr = self .pgSz_bldr (has_pgSz_child , w = w )
192- sectPr = self .sectPr_bldr (pgSz_bldr ).element
193- section = Section (sectPr )
180+ sectPr_cxml , expected_page_width = request .param
181+ section = Section (element (sectPr_cxml ))
194182 return section , expected_page_width
195183
196184 @pytest .fixture (params = [
197- (None , None ),
198- (Inches (1 ), 1440 ),
185+ (None , 'w:sectPr/w:pgSz' ),
186+ (Inches (4 ), 'w:sectPr/w:pgSz{w:w=5760}' ),
199187 ])
200188 def page_width_set_fixture (self , request ):
201- new_page_width , expected_w_val = request .param
202- # section ----------------------
203- sectPr = self .sectPr_bldr ().element
204- section = Section (sectPr )
205- # expected_xml -----------------
206- pgSz_bldr = self .pgSz_bldr (w = expected_w_val )
207- expected_xml = self .sectPr_bldr (pgSz_bldr ).xml ()
189+ new_page_width , expected_cxml = request .param
190+ section = Section (element ('w:sectPr' ))
191+ expected_xml = xml (expected_cxml )
208192 return section , new_page_width , expected_xml
209193
210194 @pytest .fixture (params = [
211- (False , None , WD_SECTION .NEW_PAGE ),
212- (True , None , WD_SECTION .NEW_PAGE ),
213- (True , ' continuous' , WD_SECTION .CONTINUOUS ),
214- (True , ' nextPage' , WD_SECTION .NEW_PAGE ),
215- (True , ' oddPage' , WD_SECTION .ODD_PAGE ),
216- (True , ' evenPage' , WD_SECTION .EVEN_PAGE ),
217- (True , ' nextColumn' , WD_SECTION .NEW_COLUMN ),
195+ ('w:sectPr' , WD_SECTION .NEW_PAGE ),
196+ ('w:sectPr/w:type' , WD_SECTION .NEW_PAGE ),
197+ ('w:sectPr/w:type{w:val= continuous} ' , WD_SECTION .CONTINUOUS ),
198+ ('w:sectPr/w:type{w:val= nextPage} ' , WD_SECTION .NEW_PAGE ),
199+ ('w:sectPr/w:type{w:val= oddPage} ' , WD_SECTION .ODD_PAGE ),
200+ ('w:sectPr/w:type{w:val= evenPage} ' , WD_SECTION .EVEN_PAGE ),
201+ ('w:sectPr/w:type{w:val= nextColumn} ' , WD_SECTION .NEW_COLUMN ),
218202 ])
219203 def start_type_get_fixture (self , request ):
220- has_type_child , type_val , expected_start_type = request .param
221- type_bldr = self .type_bldr (has_type_child , type_val )
222- sectPr = self .sectPr_bldr (type_bldr ).element
223- section = Section (sectPr )
204+ sectPr_cxml , expected_start_type = request .param
205+ section = Section (element (sectPr_cxml ))
224206 return section , expected_start_type
225207
226208 @pytest .fixture (params = [
227- (True , 'oddPage' , WD_SECTION .EVEN_PAGE , True , 'evenPage' ),
228- (True , 'nextPage' , None , False , None ),
229- (False , None , WD_SECTION .NEW_PAGE , False , None ),
230- (True , 'continuous' , WD_SECTION .NEW_PAGE , False , None ),
231- (True , None , WD_SECTION .NEW_PAGE , False , None ),
232- (True , None , WD_SECTION .NEW_COLUMN , True , 'nextColumn' ),
209+ ('w:sectPr/w:type{w:val=oddPage}' , WD_SECTION .EVEN_PAGE ,
210+ 'w:sectPr/w:type{w:val=evenPage}' ),
211+ ('w:sectPr/w:type{w:val=nextPage}' , None ,
212+ 'w:sectPr' ),
213+ ('w:sectPr' , None ,
214+ 'w:sectPr' ),
215+ ('w:sectPr/w:type{w:val=continuous}' , WD_SECTION .NEW_PAGE ,
216+ 'w:sectPr' ),
217+ ('w:sectPr/w:type' , WD_SECTION .NEW_PAGE ,
218+ 'w:sectPr' ),
219+ ('w:sectPr/w:type' , WD_SECTION .NEW_COLUMN ,
220+ 'w:sectPr/w:type{w:val=nextColumn}' ),
233221 ])
234222 def start_type_set_fixture (self , request ):
235- (has_type_child , initial_type_val , new_type , has_type_child_after ,
236- expected_type_val ) = request .param
237- # section ----------------------
238- type_bldr = self .type_bldr (has_type_child , initial_type_val )
239- sectPr = self .sectPr_bldr (type_bldr ).element
240- section = Section (sectPr )
241- # expected_xml -----------------
242- type_bldr = self .type_bldr (has_type_child_after , expected_type_val )
243- expected_xml = self .sectPr_bldr (type_bldr ).xml ()
244- return section , new_type , expected_xml
223+ initial_cxml , new_start_type , expected_cxml = request .param
224+ section = Section (element (initial_cxml ))
225+ expected_xml = xml (expected_cxml )
226+ return section , new_start_type , expected_xml
245227
246228 # fixture components ---------------------------------------------
247229
@@ -262,29 +244,9 @@ def pgMar_bldr(self, **kwargs):
262244 set_attr_method (value )
263245 return pgMar_bldr
264246
265- def pgSz_bldr (self , has_pgSz = True , w = None , h = None , orient = None ):
266- if not has_pgSz :
267- return None
268- pgSz_bldr = a_pgSz ()
269- if w is not None :
270- pgSz_bldr .with_w (w )
271- if h is not None :
272- pgSz_bldr .with_h (h )
273- if orient is not None :
274- pgSz_bldr .with_orient (orient )
275- return pgSz_bldr
276-
277247 def sectPr_bldr (self , * child_bldrs ):
278248 sectPr_bldr = a_sectPr ().with_nsdecls ()
279249 for child_bldr in child_bldrs :
280250 if child_bldr is not None :
281251 sectPr_bldr .with_child (child_bldr )
282252 return sectPr_bldr
283-
284- def type_bldr (self , has_type_elm , val ):
285- if not has_type_elm :
286- return None
287- type_bldr = a_type ()
288- if val is not None :
289- type_bldr .with_val (val )
290- return type_bldr
0 commit comments