3
3
import hashlib
4
4
5
5
from behave import given , then , when
6
+ from behave .runner import Context
6
7
7
8
from docx import Document
8
9
from docx .enum .text import WD_BREAK , WD_UNDERLINE
@@ -47,13 +48,18 @@ def given_a_run_having_mixed_text_content(context):
47
48
r_xml = """\
48
49
<w:r %s>
49
50
<w:t>abc</w:t>
50
- <w:tab />
51
+ <w:br />
51
52
<w:t>def</w:t>
52
53
<w:cr/>
53
54
<w:t>ghi</w:t>
54
55
<w:drawing/>
55
- <w:br/>
56
56
<w:t>jkl</w:t>
57
+ <w:tab/>
58
+ <w:t>mno</w:t>
59
+ <w:noBreakHyphen/>
60
+ <w:t>pqr</w:t>
61
+ <w:ptab/>
62
+ <w:t>stu</w:t>
57
63
</w:r>""" % nsdecls (
58
64
"w"
59
65
)
@@ -79,6 +85,14 @@ def given_a_run_having_style(context, style):
79
85
context .run = document .paragraphs [0 ].runs [run_idx ]
80
86
81
87
88
+ @given ("a run having {zero_or_more} rendered page breaks" )
89
+ def given_a_run_having_rendered_page_breaks (context : Context , zero_or_more : str ):
90
+ paragraph_idx = {"no" : 0 , "one" : 1 , "two" : 3 }[zero_or_more ]
91
+ document = Document (test_docx ("par-rendered-page-breaks" ))
92
+ paragraph = document .paragraphs [paragraph_idx ]
93
+ context .run = paragraph .runs [0 ]
94
+
95
+
82
96
@given ("a run inside a table cell retrieved from {cell_source}" )
83
97
def given_a_run_inside_a_table_cell_from_source (context , cell_source ):
84
98
document = Document ()
@@ -143,7 +157,7 @@ def when_I_add_text_to_the_run(context):
143
157
144
158
@when ("I assign mixed text to the text property" )
145
159
def when_I_assign_mixed_text_to_the_text_property (context ):
146
- context .run .text = "abc\t def \n ghi \r jkl "
160
+ context .run .text = "abc\n def \r ghijkl \t mno-pqr \t stu "
147
161
148
162
149
163
@when ("I assign {value_str} to its {bool_prop_name} property" )
@@ -203,20 +217,43 @@ def then_type_is_page_break(context):
203
217
assert attrib == {qn ("w:type" ): "page" }
204
218
205
219
220
+ @then ("run.contains_page_break is {value}" )
221
+ def then_run_contains_page_break_is_value (context : Context , value : str ):
222
+ actual = context .run .contains_page_break
223
+ expected = {"True" : True , "False" : False }[value ]
224
+ assert actual == expected , f"expected: { expected } , got: { actual } "
225
+
226
+
206
227
@then ("run.font is the Font object for the run" )
207
228
def then_run_font_is_the_Font_object_for_the_run (context ):
208
229
run , font = context .run , context .run .font
209
230
assert isinstance (font , Font )
210
231
assert font .element is run .element
211
232
212
233
234
+ @then ("run.iter_inner_content() generates the run text and rendered page-breaks" )
235
+ def then_run_iter_inner_content_generates_text_and_page_breaks (context : Context ):
236
+ actual_value = [type (item ).__name__ for item in context .run .iter_inner_content ()]
237
+ expected_value = ["str" , "RenderedPageBreak" , "str" , "RenderedPageBreak" , "str" ]
238
+ assert (
239
+ actual_value == expected_value
240
+ ), f"expected: { expected_value } , got: { actual_value } "
241
+
242
+
213
243
@then ("run.style is styles['{style_name}']" )
214
244
def then_run_style_is_style (context , style_name ):
215
245
expected_value = context .document .styles [style_name ]
216
246
run = context .run
217
247
assert run .style == expected_value , "got %s" % run .style
218
248
219
249
250
+ @then ("run.text contains the text content of the run" )
251
+ def then_run_text_contains_the_text_content_of_the_run (context ):
252
+ actual = context .run .text
253
+ expected = "abc\n def\n ghijkl\t mno-pqr\t stu"
254
+ assert actual == expected , f"expected:\n '{ expected } '\n \n got:\n '{ actual } '"
255
+
256
+
220
257
@then ("the last item in the run is a break" )
221
258
def then_last_item_in_run_is_a_break (context ):
222
259
run = context .run
@@ -291,8 +328,3 @@ def then_the_tab_appears_at_the_end_of_the_run(context):
291
328
r = context .run ._r
292
329
tab = r .find (qn ("w:tab" ))
293
330
assert tab is not None
294
-
295
-
296
- @then ("the text of the run represents the textual run content" )
297
- def then_the_text_of_the_run_represents_the_textual_run_content (context ):
298
- assert context .run .text == "abc\t def\n ghi\n jkl" , "got '%s'" % context .run .text
0 commit comments