X Tutup
Skip to content

Commit 520d90f

Browse files
Victor VolleSteve Canny
authored andcommitted
acpt: add run-char-style.feature
Acceptance tests for character style support, implemented as the read/write Run.style property.
1 parent 703fce1 commit 520d90f

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

docx/text.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ def strike(self):
300300
"""
301301
return 'strike'
302302

303+
@property
304+
def style(self):
305+
"""
306+
The string name of the character style applied to this run, or |None|
307+
if it has no directly-applied character style.
308+
"""
309+
raise NotImplementedError
310+
303311
@property
304312
def text(self):
305313
"""

features/run-char-style.feature

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Feature: Each run has a read/write style
2+
In order to use the stylesheet capability built into Word
3+
As an python-docx developer
4+
I need the ability to get and set the character style of a run
5+
6+
7+
@wip
8+
Scenario Outline: Get the character style of a run
9+
Given a run having style <char style>
10+
Then the style of the run is <char style>
11+
12+
Examples: Character styles
13+
| char style |
14+
| None |
15+
| Emphasis |
16+
| Strong |
17+
18+
19+
@wip
20+
Scenario Outline: Set the style of a run
21+
Given a run having style <char style>
22+
When I set the character style of the run to <new char style>
23+
Then the style of the run is <new char style>
24+
25+
Examples: Character style transitions
26+
| char style | new char style |
27+
| None | None |
28+
| None | Emphasis |
29+
| Emphasis | None |
30+
| Emphasis | Emphasis |
31+
| Emphasis | Strong |
26 KB
Binary file not shown.

features/steps/text.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from docx.enum.text import WD_BREAK
1313
from docx.oxml.shared import qn
1414

15+
from .helpers import test_docx
16+
1517

1618
# given ===================================================
1719

@@ -28,6 +30,15 @@ def given_a_run_having_bool_prop_set_on(context, bool_prop_name):
2830
context.run = run
2931

3032

33+
@given('a run having style {char_style}')
34+
def given_a_run_having_style_char_style(context, char_style):
35+
run_idx = {
36+
'None': 0, 'Emphasis': 1, 'Strong': 2
37+
}[char_style]
38+
document = Document(test_docx('run-char-style'))
39+
context.run = document.paragraphs[0].runs[run_idx]
40+
41+
3142
# when ====================================================
3243

3344
@when('I add a column break')
@@ -55,6 +66,14 @@ def when_assign_true_to_bool_run_prop(context, value_str, bool_prop_name):
5566
setattr(run, bool_prop_name, value)
5667

5768

69+
@when('I set the character style of the run to {char_style}')
70+
def when_I_set_the_character_style_of_the_run(context, char_style):
71+
style_value = {
72+
'None': None, 'Emphasis': 'Emphasis', 'Strong': 'Strong'
73+
}[char_style]
74+
context.run.style = style_value
75+
76+
5877
# then =====================================================
5978

6079
@then('it is a column break')
@@ -101,3 +120,11 @@ def then_run_inherits_bool_prop_value(context, boolean_prop_name):
101120
def then_run_appears_without_bool_prop(context, boolean_prop_name):
102121
run = context.run
103122
assert getattr(run, boolean_prop_name) is False
123+
124+
125+
@then('the style of the run is {char_style}')
126+
def then_the_style_of_the_run_is_char_style(context, char_style):
127+
expected_value = {
128+
'None': None, 'Emphasis': 'Emphasis', 'Strong': 'Strong'
129+
}[char_style]
130+
assert context.run.style == expected_value

0 commit comments

Comments
 (0)
X Tutup