1
1
"""Step implementations for section-related features."""
2
2
3
3
from behave import given , then , when
4
+ from behave .runner import Context
4
5
5
6
from docx import Document
6
7
from docx .enum .section import WD_ORIENT , WD_SECTION
13
14
14
15
15
16
@given ("a Section object as section" )
16
- def given_a_Section_object_as_section (context ):
17
+ def given_a_Section_object_as_section (context : Context ):
17
18
context .section = Document (test_docx ("sct-section-props" )).sections [- 1 ]
18
19
19
20
21
+ @given ("a Section object of a multi-section document as section" )
22
+ def given_a_Section_object_of_a_multi_section_document_as_section (context : Context ):
23
+ context .section = Document (test_docx ("sct-inner-content" )).sections [1 ]
24
+
25
+
20
26
@given ("a Section object {with_or_without} a distinct first-page header as section" )
21
- def given_a_Section_object_with_or_without_first_page_header (context , with_or_without ):
27
+ def given_a_Section_object_with_or_without_first_page_header (
28
+ context : Context , with_or_without : str
29
+ ):
22
30
section_idx = {"with" : 1 , "without" : 0 }[with_or_without ]
23
31
context .section = Document (test_docx ("sct-first-page-hdrftr" )).sections [section_idx ]
24
32
25
33
26
34
@given ("a section collection containing 3 sections" )
27
- def given_a_section_collection_containing_3_sections (context ):
35
+ def given_a_section_collection_containing_3_sections (context : Context ):
28
36
document = Document (test_docx ("doc-access-sections" ))
29
37
context .sections = document .sections
30
38
31
39
32
40
@given ("a section having known page dimension" )
33
- def given_a_section_having_known_page_dimension (context ):
41
+ def given_a_section_having_known_page_dimension (context : Context ):
34
42
document = Document (test_docx ("sct-section-props" ))
35
43
context .section = document .sections [- 1 ]
36
44
37
45
38
46
@given ("a section having known page margins" )
39
- def given_a_section_having_known_page_margins (context ):
47
+ def given_a_section_having_known_page_margins (context : Context ):
40
48
document = Document (test_docx ("sct-section-props" ))
41
49
context .section = document .sections [0 ]
42
50
43
51
44
52
@given ("a section having start type {start_type}" )
45
- def given_a_section_having_start_type (context , start_type ):
53
+ def given_a_section_having_start_type (context : Context , start_type : str ):
46
54
section_idx = {
47
55
"CONTINUOUS" : 0 ,
48
56
"NEW_PAGE" : 1 ,
@@ -55,7 +63,7 @@ def given_a_section_having_start_type(context, start_type):
55
63
56
64
57
65
@given ("a section known to have {orientation} orientation" )
58
- def given_a_section_having_known_orientation (context , orientation ):
66
+ def given_a_section_having_known_orientation (context : Context , orientation : str ):
59
67
section_idx = {"landscape" : 0 , "portrait" : 1 }[orientation ]
60
68
document = Document (test_docx ("sct-section-props" ))
61
69
context .section = document .sections [section_idx ]
@@ -65,12 +73,14 @@ def given_a_section_having_known_orientation(context, orientation):
65
73
66
74
67
75
@when ("I assign {bool_val} to section.different_first_page_header_footer" )
68
- def when_I_assign_value_to_section_different_first_page_hdrftr (context , bool_val ):
76
+ def when_I_assign_value_to_section_different_first_page_hdrftr (
77
+ context : Context , bool_val : str
78
+ ):
69
79
context .section .different_first_page_header_footer = eval (bool_val )
70
80
71
81
72
82
@when ("I set the {margin_side} margin to {inches} inches" )
73
- def when_I_set_the_margin_side_length (context , margin_side , inches ):
83
+ def when_I_set_the_margin_side_length (context : Context , margin_side : str , inches : str ):
74
84
prop_name = {
75
85
"left" : "left_margin" ,
76
86
"right" : "right_margin" ,
@@ -85,7 +95,7 @@ def when_I_set_the_margin_side_length(context, margin_side, inches):
85
95
86
96
87
97
@when ("I set the section orientation to {orientation}" )
88
-
10000
def when_I_set_the_section_orientation (context , orientation ):
98
+ def when_I_set_the_section_orientation (context : Context , orientation : str ):
89
99
new_orientation = {
90
100
"WD_ORIENT.PORTRAIT" : WD_ORIENT .PORTRAIT ,
91
101
"WD_ORIENT.LANDSCAPE" : WD_ORIENT .LANDSCAPE ,
@@ -95,17 +105,17 @@ def when_I_set_the_section_orientation(context, orientation):
95
105
96
106
97
107
@when ("I set the section page height to {y} inches" )
98
- def when_I_set_the_section_page_height_to_y_inches (context , y ):
108
+ def when_I_set_the_section_page_height_to_y_inches (context : Context , y : str ):
99
109
context .section .page_height = Inches (float (y ))
100
110
101
111
102
112
@when ("I set the section page width to {x} inches" )
103
- def when_I_set_the_section_page_width_to_x_inches (context , x ):
113
+ def when_I_set_the_section_page_width_to_x_inches (context : Context , x : str ):
104
114
context .section .page_width = Inches (float (x ))
105
115
106
116
107
117
@when ("I set the section start type to {start_type}" )
108
- def when_I_set_the_section_start_type_to_start_type (context , start_type ):
118
+ def when_I_set_the_section_start_type_to_start_type (context : Context , start_type : str ):
109
119
new_start_type = {
110
120
"None" : None ,
111
121
"CONTINUOUS" : WD_SECTION .CONTINUOUS ,
@@ -121,15 +131,15 @@ def when_I_set_the_section_start_type_to_start_type(context, start_type):
121
131
122
132
123
133
@then ("I can access a section by index" )
124
- def then_I_can_access_a_section_by_index (context ):
134
+ def then_I_can_access_a_section_by_index (context : Context ):
125
135
sections = context .sections
126
136
for idx in range (3 ):
127
137
section = sections [idx ]
128
138
assert isinstance (section , Section )
129
139
130
140
131
141
@then ("I can iterate over the sections" )
132
- def then_I_can_iterate_over_the_sections (context ):
142
+ def then_I_can_iterate_over_the_sections (context : Context ):
133
143
sections = context .sections
134
144
actual_count = 0
135
145
for section in sections :
@@ -139,13 +149,13 @@ def then_I_can_iterate_over_the_sections(context):
139
149
140
150
141
151
@then ("len(sections) is 3" )
142
- def then_len_sections_is_3 (context ):
152
+ def then_len_sections_is_3 (context : Context ):
143
153
sections = context .sections
144
154
assert len (sections ) == 3 , "expected len(sections) of 3, got %s" % len (sections )
145
155
146
156
147
157
@then ("section.different_first_page_header_footer is {bool_val}" )
148
- def then_section_different_first_page_header_footer_is (context , bool_val ):
158
+ def then_section_different_first_page_header_footer_is (context : Context , bool_val : str ):
149
159
actual = context .section .different_first_page_header_footer
150
160
expected = eval (bool_val )
151
161
assert actual == expected , (
@@ -154,49 +164,58 @@ def then_section_different_first_page_header_footer_is(context, bool_val):
154
164
155
165
156
166
@then ("section.even_page_footer is a _Footer object" )
157
- def then_section_even_page_footer_is_a_Footer_object (context ):
167
+ def then_section_even_page_footer_is_a_Footer_object (context : Context ):
158
168
actual = type (context .section .even_page_footer ).__name__
159
169
expected = "_Footer"
160
170
assert actual == expected , "section.even_page_footer is a %s object" % actual
161
171
162
172
163
173
@then ("section.even_page_header is a _Header object" )
164
- def then_section_even_page_header_is_a_Header_object (context ):
174
+ def then_section_even_page_header_is_a_Header_object (context : Context ):
165
175
actual = type (context .section .even_page_header ).__name__
166
176
expected = "_Header"
167
177
assert actual == expected , "section.even_page_header is a %s object" % actual
168
178
169
179
170
180
@then ("section.first_page_footer is a _Footer object" )
171
- def then_section_first_page_footer_is_a_Footer_object (context ):
181
+ def then_section_first_page_footer_is_a_Footer_object (context : Context ):
172
182
actual = type (context .section .first_page_footer ).__name__
173
183
expected = "_Footer"
174
184
assert actual == expected , "section.first_page_footer is a %s object" % actual
175
185
176
186
177
187
@then ("section.first_page_header is a _Header object" )
178
- def then_section_first_page_header_is_a_Header_object (context ):
188
+ def then_section_first_page_header_is_a_Header_object (context : Context ):
179
189
actual = type (context .section .first_page_header ).__name__
180
190
expected = "_Header"
181
191
assert actual == expected , "section.first_page_header is a %s object" % actual
182
192
183
193
184
194
@then ("section.footer is a _Footer object" )
185
- def then_section_footer_is_a_Footer_object (context ):
195
+ def then_section_footer_is_a_Footer_object (context : Context ):
186
196
actual = type (context .section .footer ).__name__
187
197
expected = "_Footer"
188
198
assert actual == expected , "section.footer is a %s object" % actual
189
199
190
200
191
201
@then ("section.header is a _Header object" )
192
- def then_section_header_is_a_Header_object (context ):
202
+ def then_section_header_is_a_Header_object (context : Context ):
193
203
actual = type (context .section .header ).__name__
194
204
expected = "_Header"
195
205
assert actual == expected , "section.header is a %s object" % actual
196
206
197
207
208
+ @then ("section.iter_inner_content() produces the paragraphs and tables in section" )
209
+ def step_impl (context : Context ):
210
+ actual = [type (item ).__name__ for item in context .section .iter_inner_content ()]
211
+ expected = ["Table" , "Paragraph" , "Paragraph" ]
212
+ assert actual == expected , f"expected: { expected } , got: { actual } "
213
+
214
+
198
215
@then ("section.{propname}.is_linked_to_previous is True" )
199
- def then_section_hdrftr_prop_is_linked_to_previous_is_True (context , propname ):
216
+ def then_section_hdrftr_prop_is_linked_to_previous_is_True (
217
+ context : Context , propname : str
218
+ ):
200
219
actual = getattr (context .section , propname ).is_linked_to_previous
201
220
expected = True
202
221
assert actual == expected , "section.%s.is_linked_to_previous is %s" % (
@@ -206,7 +225,7 @@ def then_section_hdrftr_prop_is_linked_to_previous_is_True(context, propname):
206
225
207
226
208
227
@then ("the reported {margin_side} margin is {inches} inches" )
209
- def then_the_reported_margin_is_inches (context , margin_side , inches ):
228
+ def then_the_reported_margin_is_inches (context : Context , margin_side : str , inches : str ):
210
229
prop_name = {
211
230
"left" : "left_margin" ,
212
231
"right" : "right_margin" ,
@@ -222,7 +241,9 @@ def then_the_reported_margin_is_inches(context, margin_side, inches):
222
241
223
242
224
243
@then ("the reported page orientation is {orientation}" )
225
- def then_the_reported_page_orientation_is_orientation (context , orientation ):
244
+ def then_the_reported_page_orientation_is_orientation (
245
+ context : Context , orientation : str
246
+ ):
226
247
expected_value = {
227
248
"WD_ORIENT.LANDSCAPE" : WD_ORIENT .LANDSCAPE ,
228
249
"WD_ORIENT.PORTRAIT" : WD_ORIENT .PORTRAIT ,
@@ -231,17 +252,17 @@ def then_the_reported_page_orientation_is_orientation(context, orientation):
231
252
232
253
233
254
@then ("the reported page width is {x} inches" )
234
- def then_the_reported_page_width_is_width (context , x ):
255
+ def then_the_reported_page_width_is_width (context : Context , x : str ):
235
256
assert context .section .page_width == Inches (float (x ))
236
257
237
258
238
259
@then ("the reported page height is {y} inches" )
239
- def then_the_reported_page_height_is_11_inches (context , y ):
260
+ def then_the_reported_page_height_is_11_inches (context : Context , y : str ):
240
261
assert context .section .page_height == Inches (float (y ))
241
262
242
263
243
264
@then ("the reported section start type is {start_type}" )
244
- def then_the_reported_section_start_type_is_type (context , start_type ):
265
+ def then_the_reported_section_start_type_is_type (context : Context , start_type : str ):
245
266
expected_start_type = {
246
267
"CONTINUOUS" : WD_SECTION .CONTINUOUS ,
247
268
"EVEN_PAGE" : WD_SECTION .EVEN_PAGE ,
0 commit comments