1010
1111from docx .enum .section import WD_SECTION
1212from docx .section import Section
13+ from docx .shared import Inches
1314
14- from .oxml .unitdata .section import a_sectPr , a_type
15+ from .oxml .unitdata .section import a_pgSz , a_sectPr , a_type
1516
1617
1718class DescribeSection (object ):
@@ -25,41 +26,81 @@ def it_can_change_its_start_type(self, start_type_set_fixture):
2526 section .start_type = new_start_type
2627 assert section ._sectPr .xml == expected_xml
2728
29+ def it_knows_its_page_width (self , page_width_get_fixture ):
30+ section , expected_page_width = page_width_get_fixture
31+ assert section .page_width == expected_page_width
32+
2833 # fixtures -------------------------------------------------------
2934
3035 @pytest .fixture (params = [
31- (None , WD_SECTION .NEW_PAGE ),
32- ('continuous' , WD_SECTION .CONTINUOUS ),
33- ('nextPage' , WD_SECTION .NEW_PAGE ),
34- ('oddPage' , WD_SECTION .ODD_PAGE ),
35- ('evenPage' , WD_SECTION .EVEN_PAGE ),
36- ('nextColumn' , WD_SECTION .NEW_COLUMN ),
36+ (True , 1440 , Inches (1 )),
37+ (True , None , None ),
38+ (False , None , None ),
39+ ])
40+ def page_width_get_fixture (self , request ):
41+ has_pgSz_child , w , expected_page_width = request .param
42+ pgSz_bldr = self .pgSz_bldr (has_pgSz_child , w )
43+ sectPr = self .sectPr_bldr (pgSz_bldr ).element
44+ section = Section (sectPr )
45+ return section , expected_page_width
46+
47+ @pytest .fixture (params = [
48+ (False , None , WD_SECTION .NEW_PAGE ),
49+ (True , None , WD_SECTION .NEW_PAGE ),
50+ (True , 'continuous' , WD_SECTION .CONTINUOUS ),
51+ (True , 'nextPage' , WD_SECTION .NEW_PAGE ),
52+ (True , 'oddPage' , WD_SECTION .ODD_PAGE ),
53+ (True , 'evenPage' , WD_SECTION .EVEN_PAGE ),
54+ (True , 'nextColumn' , WD_SECTION .NEW_COLUMN ),
3755 ])
3856 def start_type_get_fixture (self , request ):
39- type_val , expected_start_type = request .param
40- sectPr = self .sectPr_bldr (type_val ).element
57+ has_type_child , type_val , expected_start_type = request .param
58+ type_bldr = self .type_bldr (has_type_child , type_val )
59+ sectPr = self .sectPr_bldr (type_bldr ).element
4160 section = Section (sectPr )
4261 return section , expected_start_type
4362
4463 @pytest .fixture (params = [
45- ('oddPage' , WD_SECTION .EVEN_PAGE , 'evenPage' ),
46- ('nextPage' , None , None ),
47- ('continuous' , WD_SECTION .NEW_PAGE , None ),
48- (None , WD_SECTION .NEW_COLUMN , 'nextColumn' ),
64+ (True , 'oddPage' , WD_SECTION .EVEN_PAGE , True , 'evenPage' ),
65+ (True , 'nextPage' , None , False , None ),
66+ (False , None , WD_SECTION .NEW_PAGE , False , None ),
67+ (True , 'continuous' , WD_SECTION .NEW_PAGE , False , None ),
68+ (True , None , WD_SECTION .NEW_PAGE , False , None ),
69+ (True , None , WD_SECTION .NEW_COLUMN , True , 'nextColumn' ),
4970 ])
5071 def start_type_set_fixture (self , request ):
51- initial_type_val , new_type , expected_type_val = request .param
52- sectPr = self .sectPr_bldr (initial_type_val ).element
72+ (has_type_child , initial_type_val , new_type , has_type_child_after ,
73+ expected_type_val ) = request .param
74+ # section ----------------------
75+ type_bldr = self .type_bldr (has_type_child , initial_type_val )
76+ sectPr = self .sectPr_bldr (type_bldr ).element
5377 section = Section (sectPr )
54- expected_xml = self .sectPr_bldr (expected_type_val ).xml ()
78+ # expected_xml -----------------
79+ type_bldr = self .type_bldr (has_type_child_after , expected_type_val )
80+ expected_xml = self .sectPr_bldr (type_bldr ).xml ()
5581 return section , new_type , expected_xml
5682
5783 # fixture components ---------------------------------------------
5884
59- def sectPr_bldr (self , start_type = None ):
85+ def pgSz_bldr (self , has_pgSz , w ):
86+ if not has_pgSz :
87+ return None
88+ pgSz_bldr = a_pgSz ()
89+ if w is not None :
90+ pgSz_bldr .with_w (w )
91+ return pgSz_bldr
92+
93+ def sectPr_bldr (self , * child_bldrs ):
6094 sectPr_bldr = a_sectPr ().with_nsdecls ()
61- if start_type is not None :
62- sectPr_bldr .with_child (
63- a_type ().with_val (start_type )
64- )
95+ for child_bldr in child_bldrs :
96+ if child_bldr is not None :
97+ sectPr_bldr .with_child (child_bldr )
6598 return sectPr_bldr
99+
100+ def type_bldr (self , has_type_elm , val ):
101+ if not has_type_elm :
102+ return None
103+ type_bldr = a_type ()
104+ if val is not None :
105+ type_bldr .with_val (val )
106+ return type_bldr
0 commit comments