|
11 | 11 | from docx.enum.style import WD_STYLE_TYPE |
12 | 12 | from docx.enum.text import WD_BREAK, WD_UNDERLINE |
13 | 13 | from docx.parts.document import DocumentPart |
| 14 | +from docx.shape import InlineShape |
14 | 15 | from docx.shared import Pt |
15 | 16 | from docx.text.run import Font, Run |
16 | 17 |
|
@@ -84,6 +85,17 @@ def it_can_add_a_tab(self, add_tab_fixture): |
84 | 85 | run.add_tab() |
85 | 86 | assert run._r.xml == expected_xml |
86 | 87 |
|
| 88 | + def it_can_add_a_picture(self, add_picture_fixture): |
| 89 | + run, image, width, height, inline = add_picture_fixture[:5] |
| 90 | + expected_xml, InlineShape_, picture_ = add_picture_fixture[5:] |
| 91 | + |
| 92 | + picture = run.add_picture(image, width, height) |
| 93 | + |
| 94 | + run.part.new_pic_inline.assert_called_once_with(image, width, height) |
| 95 | + assert run._r.xml == expected_xml |
| 96 | + InlineShape_.assert_called_once_with(inline) |
| 97 | + assert picture is picture_ |
| 98 | + |
87 | 99 | def it_can_remove_its_content_but_keep_formatting(self, clear_fixture): |
88 | 100 | run, expected_xml = clear_fixture |
89 | 101 | _run = run.clear() |
@@ -118,6 +130,20 @@ def add_break_fixture(self, request): |
118 | 130 | expected_xml = xml(expected_cxml) |
119 | 131 | return run, break_type, expected_xml |
120 | 132 |
|
| 133 | + @pytest.fixture |
| 134 | + def add_picture_fixture(self, part_prop_, document_part_, InlineShape_, |
| 135 | + picture_): |
| 136 | + run = Run(element('w:r/wp:x'), None) |
| 137 | + image = 'foobar.png' |
| 138 | + width, height, inline = 1111, 2222, element('wp:inline{id=42}') |
| 139 | + expected_xml = xml('w:r/(wp:x,w:drawing/wp:inline{id=42})') |
| 140 | + document_part_.new_pic_inline.return_value = inline |
| 141 | + InlineShape_.return_value = picture_ |
| 142 | + return ( |
| 143 | + run, image, width, height, inline, expected_xml, InlineShape_, |
| 144 | + picture_ |
| 145 | + ) |
| 146 | + |
121 | 147 | @pytest.fixture(params=[ |
122 | 148 | ('w:r/w:t"foo"', 'w:r/(w:t"foo", w:tab)'), |
123 | 149 | ]) |
@@ -304,12 +330,20 @@ def Font_(self, request, font_): |
304 | 330 | def font_(self, request): |
305 | 331 | return instance_mock(request, Font) |
306 | 332 |
|
| 333 | + @pytest.fixture |
| 334 | + def InlineShape_(self, request): |
| 335 | + return class_mock(request, 'docx.text.run.InlineShape') |
| 336 | + |
307 | 337 | @pytest.fixture |
308 | 338 | def part_prop_(self, request, document_part_): |
309 | 339 | return property_mock( |
310 | 340 | request, Run, 'part', return_value=document_part_ |
311 | 341 | ) |
312 | 342 |
|
| 343 | + @pytest.fixture |
| 344 | + def picture_(self, request): |
| 345 | + return instance_mock(request, InlineShape) |
| 346 | + |
313 | 347 | @pytest.fixture |
314 | 348 | def Text_(self, request): |
315 | 349 | return class_mock(request, 'docx.text.run._Text') |
|
0 commit comments