Skip to content

Commit 66f5b78

Browse files
author
Steve Canny
committed
run: rewrite Run.style setter
* revise test for Paragraph.add_run() to mock out assignment to Run.style.
1 parent 3f1a01a commit 66f5b78

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

docx/text/run.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ def style(self):
144144
return self.part.get_style(style_id, WD_STYLE_TYPE.CHARACTER)
145145

146146
@style.setter
147-
def style(self, char_style):
148-
self._r.style = char_style
147+
def style(self, style_or_name):
148+
style_id = self.part.get_style_id(
149+
style_or_name, WD_STYLE_TYPE.CHARACTER
150+
)
151+
self._r.style = style_id
149152

150153
@property
151154
def text(self):

features/run-char-style.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Feature: Each run has a read/write style
1515
| Strong | Strong |
1616

1717

18-
@wip
1918
Scenario Outline: Set the style of a run
2019
Given a run having <style> style
2120
When I assign <value> to run.style

tests/text/test_paragraph.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ def it_provides_access_to_the_runs_it_contains(self, runs_fixture):
7878
assert runs == [run_, run_2_]
7979

8080
def it_can_add_a_run_to_itself(self, add_run_fixture):
81-
paragraph, text, style, expected_xml = add_run_fixture
81+
paragraph, text, style, style_prop_, expected_xml = add_run_fixture
8282
run = paragraph.add_run(text, style)
8383
assert paragraph._p.xml == expected_xml
8484
assert isinstance(run, Run)
8585
assert run._r is paragraph._p.r_lst[0]
86+
if style:
87+
style_prop_.assert_called_once_with(style)
8688

8789
def it_can_insert_a_paragraph_before_itself(self, insert_before_fixture):
8890
paragraph, text, style, paragraph_, add_run_calls = (
@@ -111,20 +113,16 @@ def it_inserts_a_paragraph_before_to_help(self, _insert_before_fixture):
111113
# fixtures -------------------------------------------------------
112114

113115
@pytest.fixture(params=[
114-
('w:p', None, None,
115-
'w:p/w:r'),
116-
('w:p', 'foobar', None,
117-
'w:p/w:r/w:t"foobar"'),
118-
('w:p', None, 'Strong',
119-
'w:p/w:r/w:rPr/w:rStyle{w:val=Strong}'),
120-
('w:p', 'foobar', 'Strong',
121-
'w:p/w:r/(w:rPr/w:rStyle{w:val=Strong}, w:t"foobar")'),
116+
('w:p', None, None, 'w:p/w:r'),
117+
('w:p', 'foobar', None, 'w:p/w:r/w:t"foobar"'),
118+
('w:p', None, 'Strong', 'w:p/w:r'),
119+
('w:p', 'foobar', 'Strong', 'w:p/w:r/w:t"foobar"'),
122120
])
123-
def add_run_fixture(self, request):
121+
def add_run_fixture(self, request, run_style_prop_):
124122
before_cxml, text, style, after_cxml = request.param
125123
paragraph = Paragraph(element(before_cxml), None)
126124
expected_xml = xml(after_cxml)
127-
return paragraph, text, style, expected_xml
125+
return paragraph, text, style, run_style_prop_, expected_xml
128126

129127
@pytest.fixture(params=[
130128
('w:p/w:pPr/w:jc{w:val=center}', WD_ALIGN_PARAGRAPH.CENTER),
@@ -301,6 +299,10 @@ def r_(self, request):
301299
def r_2_(self, request):
302300
return instance_mock(request, CT_R)
303301

302+
@pytest.fixture
303+
def run_style_prop_(self, request):
304+
return property_mock(request, Run, 'style')
305+
304306
@pytest.fixture
305307
def runs_(self, request):
306308
run_ = instance_mock(request, Run, name='run_')

tests/text/test_run.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ def it_knows_its_character_style(self, style_get_fixture):
4242
assert style is style_
4343

4444
def it_can_change_its_character_style(self, style_set_fixture):
45-
run, style, expected_xml = style_set_fixture
46-
run.style = style
45+
run, value, expected_xml = style_set_fixture
46+
run.style = value
47+
run.part.get_style_id.assert_called_once_with(
48+
value, WD_STYLE_TYPE.CHARACTER
49+
)
4750
assert run._r.xml == expected_xml
4851

4952
def it_knows_its_underline_type(self, underline_get_fixture):
@@ -234,20 +237,23 @@ def style_get_fixture(self, part_prop_):
234237
return run, style_id, style_
235238

236239
@pytest.fixture(params=[
237-
('w:r', None,
240+
('w:r', 'Foo Font', 'FooFont',
241+
'w:r/w:rPr/w:rStyle{w:val=FooFont}'),
242+
('w:r/w:rPr', 'Foo Font', 'FooFont',
243+
'w:r/w:rPr/w:rStyle{w:val=FooFont}'),
244+
('w:r/w:rPr/w:rStyle{w:val=FooFont}', 'Bar Font', 'BarFont',
245+
'w:r/w:rPr/w:rStyle{w:val=BarFont}'),
246+
('w:r/w:rPr/w:rStyle{w:val=FooFont}', None, None,
238247
'w:r/w:rPr'),
239-
('w:r', 'Foo',
240-
'w:r/w:rPr/w:rStyle{w:val=Foo}'),
241-
('w:r/w:rPr/w:rStyle{w:val=Foo}', None,
248+
('w:r', None, None,
242249
'w:r/w:rPr'),
243-
('w:r/w:rPr/w:rStyle{w:val=Foo}', 'Bar',
244-
'w:r/w:rPr/w:rStyle{w:val=Bar}'),
245250
])
246-
def style_set_fixture(self, request):
247-
initial_r_cxml, new_style, expected_cxml = request.param
248-
run = Run(element(initial_r_cxml), None)
251+
def style_set_fixture(self, request, part_prop_):
252+
r_cxml, value, style_id, expected_cxml = request.param
253+
run = Run(element(r_cxml), None)
254+
part_prop_.return_value.get_style_id.return_value = style_id
249255
expected_xml = xml(expected_cxml)
250-
return run, new_style, expected_xml
256+
return run, value, expected_xml
251257

252258
@pytest.fixture(params=[
253259
('w:r', ''),

0 commit comments

Comments
 (0)