|
11 | 11 | import pytest |
12 | 12 |
|
13 | 13 | from docx.document import _Body, Document |
| 14 | +from docx.enum.section import WD_SECTION |
14 | 15 | from docx.enum.text import WD_BREAK |
15 | 16 | from docx.parts.document import DocumentPart |
16 | 17 | from docx.shape import InlineShape |
17 | 18 | from docx.text.paragraph import Paragraph |
18 | 19 | from docx.text.run import Run |
19 | 20 |
|
20 | | -from .unitutil.cxml import element |
| 21 | +from .unitutil.cxml import element, xml |
21 | 22 | from .unitutil.mock import ( |
22 | 23 | class_mock, instance_mock, method_mock, property_mock |
23 | 24 | ) |
@@ -58,6 +59,17 @@ def it_can_add_a_picture(self, add_picture_fixture): |
58 | 59 | run_.add_picture.assert_called_once_with(path, width, height) |
59 | 60 | assert picture is picture_ |
60 | 61 |
|
| 62 | + def it_can_add_a_section(self, add_section_fixture): |
| 63 | + document, start_type, Section_ = add_section_fixture[:3] |
| 64 | + section_, expected_xml = add_section_fixture[3:] |
| 65 | + |
| 66 | + section = document.add_section(start_type) |
| 67 | + |
| 68 | + assert document.element.xml == expected_xml |
| 69 | + sectPr = document.element.xpath('w:body/w:sectPr')[0] |
| 70 | + Section_.assert_called_once_with(sectPr) |
| 71 | + assert section is section_ |
| 72 | + |
61 | 73 | def it_provides_access_to_the_document_part(self, part_fixture): |
62 | 74 | document, part_ = part_fixture |
63 | 75 | assert document.part is part_ |
@@ -109,6 +121,25 @@ def add_picture_fixture(self, request, add_paragraph_, run_, picture_): |
109 | 121 | run_.add_picture.return_value = picture_ |
110 | 122 | return document, path, width, height, run_, picture_ |
111 | 123 |
|
| 124 | + @pytest.fixture(params=[ |
| 125 | + ('w:sectPr', WD_SECTION.EVEN_PAGE, |
| 126 | + 'w:sectPr/w:type{w:val=evenPage}'), |
| 127 | + ('w:sectPr/w:type{w:val=evenPage}', WD_SECTION.ODD_PAGE, |
| 128 | + 'w:sectPr/w:type{w:val=oddPage}'), |
| 129 | + ('w:sectPr/w:type{w:val=oddPage}', WD_SECTION.NEW_PAGE, |
| 130 | + 'w:sectPr'), |
| 131 | + ]) |
| 132 | + def add_section_fixture(self, request, Section_): |
| 133 | + sentinel, start_type, new_sentinel = request.param |
| 134 | + document_cxml = 'w:document/w:body/(w:p,%s)' % sentinel |
| 135 | + document = Document(element(document_cxml), None) |
| 136 | + expected_xml = xml( |
| 137 | + 'w:document/w:body/(w:p,w:p/w:pPr/%s,%s)' % |
| 138 | + (sentinel, new_sentinel) |
| 139 | + ) |
| 140 | + section_ = Section_.return_value |
| 141 | + return document, start_type, Section_, section_, expected_xml |
| 142 | + |
112 | 143 | @pytest.fixture |
113 | 144 | def body_fixture(self, _Body_, body_): |
114 | 145 | document_elm = element('w:document/w:body') |
@@ -154,3 +185,7 @@ def picture_(self, request): |
154 | 185 | @pytest.fixture |
155 | 186 | def run_(self, request): |
156 | 187 | return instance_mock(request, Run) |
| 188 | + |
| 189 | + @pytest.fixture |
| 190 | + def Section_(self, request): |
| 191 | + return class_mock(request, 'docx.document.Section') |
0 commit comments