8000 acpt: add Run inner-content scenarios · python-openxml/python-docx@fd54be1 · GitHub
[go: up one dir, main page]

Skip to content

Commit fd54be1

Browse files
committed
acpt: add Run inner-content scenarios
1 parent 4935c30 commit fd54be1

File tree

5 files changed

+69
-18
lines changed

5 files changed

+69
-18
lines changed

features/run-access-content.feature

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Feature: Access run inner-content including rendered page-breaks
2+
In order to extract run content with high-fidelity
3+
As a developer using python-docx
4+
I need to access differentiated run content in document order
5+
6+
7+
@wip
8+
Scenario Outline: Run.contains_page_break reports presence of page-break
9+
Given a run having <zero-or-more> rendered page breaks
10+
Then run.contains_page_break is <value>
11+
12+
Examples: Run.contains_page_break cases
13+
| zero-or-more | value |
14+
| no | False |
15+
| one | True |
16+
| two | True |
17+
18+
19+
@wip
20+
Scenario: Run.iter_inner_content() generates the run's text and rendered page-breaks
21+
Given a run having two rendered page breaks
22+
Then run.iter_inner_content() generates the run text and rendered page-breaks
23+
24+
25+
@wip
26+
Scenario: Run.text contains the text content of the run
27+
Given a run having mixed text content
28+
Then run.text contains the text content of the run

features/run-add-content.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ Feature: Add content to a run
1111
Scenario: Assign mixed text to text property
1212
Given a run
1313
When I assign mixed text to the text property
14-
Then the text of the run represents the textual run content
14+
Then run.text contains the text content of the run
Binary file not shown.

features/steps/text.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import hashlib
44

55
from behave import given, then, when
6+
from behave.runner import Context
67

78
from docx import Document
89
from docx.enum.text import WD_BREAK, WD_UNDERLINE
@@ -47,13 +48,18 @@ def given_a_run_having_mixed_text_content(context):
4748
r_xml = """\
4849
<w:r %s>
4950
<w:t>abc</w:t>
50-
<w:tab/>
51+
<w:br/>
5152
<w:t>def</w:t>
5253
<w:cr/>
5354
<w:t>ghi</w:t>
5455
<w:drawing/>
55-
<w:br/>
5656
<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>
5763
</w:r>""" % nsdecls(
5864
"w"
5965
)
@@ -79,6 +85,14 @@ def given_a_run_having_style(context, style):
7985
context.run = document.paragraphs[0].runs[run_idx]
8086

8187

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+
8296
@given("a run inside a table cell retrieved from {cell_source}")
8397
def given_a_run_inside_a_table_cell_from_source(context, cell_source):
8498
document = Document()
@@ -143,7 +157,7 @@ def when_I_add_text_to_the_run(context):
143157

144158
@when("I assign mixed text to the text property")
145159
def when_I_assign_mixed_text_to_the_text_property(context):
146-
context.run.text = "abc\tdef\nghi\rjkl"
160+
context.run.text = "abc\ndef\rghijkl\tmno-pqr\tstu"
147161

148162

149163
@when("I assign {value_str} to its {bool_prop_name} property")
@@ -203,20 +217,43 @@ def then_type_is_page_break(context):
203217
assert attrib == {qn("w:type"): "page"}
204218

205219

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+
206227
@then("run.font is the Font object for the run")
207228
def then_run_font_is_the_Font_object_for_the_run(context):
208229
run, font = context.run, context.run.font
209230
assert isinstance(font, Font)
210231
assert font.element is run.element
211232

212233

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+
213243
@then("run.style is styles['{style_name}']")
214244
def then_run_style_is_style(context, style_name):
215245
expected_value = context.document.styles[style_name]
216246
run = context.run
217247
assert run.style == expected_value, "got %s" % run.style
218248

219249

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\ndef\nghijkl\tmno-pqr\tstu"
254+
assert actual == expected, f"expected:\n'{expected}'\n\ngot:\n'{actual}'"
255+
256+
220257
@then("the last item in the run is a break")
221258
def then_last_item_in_run_is_a_break(context):
222259
run = context.run
@@ -291,8 +328,3 @@ def then_the_tab_appears_at_the_end_of_the_run(context):
291328
r = context.run._r
292329
tab = r.find(qn("w:tab"))
293330
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\tdef\nghi\njkl", "got '%s'" % context.run.text

0 commit comments

Comments
 (0)
0