Skip to content

Commit e3c2e2b

Browse files
author
Steve Canny
committed
acpt: add scenarios for Run.text getter and setter
Both handling text containing embedded \n, \t, and/or \r characters.
1 parent 0fc52d4 commit e3c2e2b

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Feature: Access run content
2+
In order to discover or locate existing inline content
3+
As a developer using python-docx
4+
I need ways to access the run content
5+
6+
7+
@wip
8+
Scenario: Get run content as Python text
9+
Given a run having mixed text content
10+
Then the text of the run represents the textual run content

features/run-add-content.feature

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ Feature: Add content to a run
88
Given a run
99
When I add a tab
1010
Then the tab appears at the end of the run
11+
12+
13+
@wip
14+
Scenario: Assign mixed text to text property
15+
Given a run
16+
When I assign mixed text to the text property
17+
Then the text of the run represents the textual run content

features/steps/text.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
from docx import Document
1212
from docx.enum.text import WD_BREAK, WD_UNDERLINE
13-
from docx.oxml.ns import qn
13+
from docx.oxml import parse_xml
14+
from docx.oxml.ns import nsdecls, qn
15+
from docx.text import Run
1416

1517
from helpers import test_docx, test_text
1618

@@ -38,6 +40,26 @@ def given_a_run_having_known_text_and_formatting(context):
3840
context.run = run
3941

4042

43+
@given('a run having mixed text content')
44+
def given_a_run_having_mixed_text_content(context):
45+
"""
46+
Mixed here meaning it contains ``<w:tab/>``, ``<w:cr/>``, etc. elements.
47+
"""
48+
r_xml = """\
49+
<w:r %s>
50+
<w:t>abc</w:t>
51+
<w:tab/>
52+
<w:t>def</w:t>
53+
<w:cr/>
54+
<w:t>ghi</w:t>
55+
<w:drawing/>
56+
<w:br/>
57+
<w:t>jkl</w:t>
58+
</w:r>""" % nsdecls('w')
59+
r = parse_xml(r_xml)
60+
context.run = Run(r)
61+
62+
4163
@given('a run having {underline_type} underline')
4264
def given_a_run_having_underline_type(context, underline_type):
4365
run_idx = {
@@ -91,6 +113,11 @@ def when_I_add_a_tab(context):
91113
context.run.add_tab()
92114

93115

116+
@when('I assign mixed text to the text property')
117+
def when_I_assign_mixed_text_to_the_text_property(context):
118+
context.run.text = 'abc\tdef\nghi\rjkl'
119+
120+
94121
@when('I assign {value_str} to its {bool_prop_name} property')
95122
def when_assign_true_to_bool_run_prop(context, value_str, bool_prop_name):
96123
value = {'True': True, 'False': False, 'None': None}[value_str]
@@ -207,3 +234,10 @@ def then_the_tab_appears_at_the_end_of_the_run(context):
207234
r = context.run._r
208235
tab = r.find(qn('w:tab'))
209236
assert tab is not None
237+
238+
239+
@then('the text of the run represents the textual run content')
240+
def then_the_text_of_the_run_represents_the_textual_run_content(context):
241+
assert context.run.text == 'abc\tdef\nghi\njkl', (
242+
'got \'%s\'' % context.run.text
243+
)

0 commit comments

Comments
 (0)