|
11 | 11 | from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_BREAK, WD_UNDERLINE |
12 | 12 | from docx.oxml.ns import qn |
13 | 13 | from docx.oxml.text import CT_P, CT_R |
| 14 | +from docx.parts.document import InlineShapes |
| 15 | +from docx.shape import InlineShape |
14 | 16 | from docx.text import Paragraph, Run |
15 | 17 |
|
16 | 18 | import pytest |
@@ -280,6 +282,17 @@ def it_can_add_a_tab(self, add_tab_fixture): |
280 | 282 | run.add_tab() |
281 | 283 | assert run._r.xml == expected_xml |
282 | 284 |
|
| 285 | + def it_can_add_a_picture(self, add_picture_fixture): |
| 286 | + (run, image_descriptor_, width, height, inline_shapes_, |
| 287 | + expected_width, expected_height, picture_) = add_picture_fixture |
| 288 | + picture = run.add_picture(image_descriptor_, width, height) |
| 289 | + inline_shapes_.add_picture.assert_called_once_with( |
| 290 | + image_descriptor_, run |
| 291 | + ) |
| 292 | + assert picture is picture_ |
| 293 | + assert picture.width == expected_width |
| 294 | + assert picture.height == expected_height |
| 295 | + |
283 | 296 | def it_can_remove_its_content_but_keep_formatting(self, clear_fixture): |
284 | 297 | run, expected_xml = clear_fixture |
285 | 298 | _run = run.clear() |
@@ -314,6 +327,24 @@ def add_break_fixture(self, request): |
314 | 327 | expected_xml = xml(expected_cxml) |
315 | 328 | return run, break_type, expected_xml |
316 | 329 |
|
| 330 | + @pytest.fixture(params=[ |
| 331 | + (None, None, 200, 100), |
| 332 | + (1000, 500, 1000, 500), |
| 333 | + (2000, None, 2000, 1000), |
| 334 | + (None, 2000, 4000, 2000), |
| 335 | + ]) |
| 336 | + def add_picture_fixture( |
| 337 | + self, request, paragraph_, inline_shapes_, picture_): |
| 338 | + width, height, expected_width, expected_height = request.param |
| 339 | + paragraph_.part.inline_shapes = inline_shapes_ |
| 340 | + run = Run(None, paragraph_) |
| 341 | + image_descriptor_ = 'image_descriptor_' |
| 342 | + picture_.width, picture_.height = 200, 100 |
| 343 | + return ( |
| 344 | + run, image_descriptor_, width, height, inline_shapes_, |
| 345 | + expected_width, expected_height, picture_ |
| 346 | + ) |
| 347 | + |
317 | 348 | @pytest.fixture(params=[ |
318 | 349 | ('w:r/w:t"foo"', 'w:r/(w:t"foo", w:tab)'), |
319 | 350 | ]) |
@@ -529,6 +560,20 @@ def underline_raise_fixture(self, request): |
529 | 560 |
|
530 | 561 | # fixture components --------------------------------------------- |
531 | 562 |
|
| 563 | + @pytest.fixture |
| 564 | + def inline_shapes_(self, request, picture_): |
| 565 | + inline_shapes_ = instance_mock(request, InlineShapes) |
| 566 | + inline_shapes_.add_picture.return_value = picture_ |
| 567 | + return inline_shapes_ |
| 568 | + |
| 569 | + @pytest.fixture |
| 570 | + def paragraph_(self, request): |
| 571 | + return instance_mock(request, Paragraph) |
| 572 | + |
| 573 | + @pytest.fixture |
| 574 | + def picture_(self, request): |
| 575 | + return instance_mock(request, InlineShape) |
| 576 | + |
532 | 577 | @pytest.fixture |
533 | 578 | def Text_(self, request): |
534 | 579 | return class_mock(request, 'docx.text.Text') |
0 commit comments