|
7 | 7 | import pytest |
8 | 8 |
|
9 | 9 | from docx.enum.section import WD_ORIENT, WD_SECTION |
| 10 | +from docx.parts.document import DocumentPart |
10 | 11 | from docx.section import Section, Sections |
11 | 12 | from docx.shared import Inches |
12 | 13 |
|
13 | 14 | from .unitutil.cxml import element, xml |
| 15 | +from .unitutil.mock import call, class_mock, instance_mock |
14 | 16 |
|
15 | 17 |
|
16 | 18 | class DescribeSections(object): |
17 | 19 |
|
18 | | - def it_knows_how_many_sections_it_contains(self, len_fixture): |
19 | | - sections, expected_len = len_fixture |
20 | | - assert len(sections) == expected_len |
| 20 | + def it_knows_how_many_sections_it_contains(self): |
| 21 | + sections = Sections( |
| 22 | + element("w:document/w:body/(w:p/w:pPr/w:sectPr, w:sectPr)"), None |
| 23 | + ) |
| 24 | + assert len(sections) == 2 |
21 | 25 |
|
22 | | - def it_can_iterate_over_its_Section_instances(self, iter_fixture): |
23 | | - sections, expected_count = iter_fixture |
24 | | - section_count = 0 |
25 | | - for section in sections: |
26 | | - section_count += 1 |
27 | | - assert isinstance(section, Section) |
28 | | - assert section_count == expected_count |
| 26 | + def it_can_iterate_over_its_Section_instances(self, Section_, section_): |
| 27 | + document_elm = element("w:document/w:body/(w:p/w:pPr/w:sectPr, w:sectPr)") |
| 28 | + sectPrs = document_elm.xpath("//w:sectPr") |
| 29 | + Section_.return_value = section_ |
| 30 | + sections = Sections(document_elm, None) |
29 | 31 |
|
30 | | - def it_can_access_its_Section_instances_by_index(self, index_fixture): |
31 | | - sections, indicies = index_fixture |
32 | | - assert len(sections[0:2]) == 2 |
33 | | - for index in indicies: |
34 | | - assert isinstance(sections[index], Section) |
| 32 | + section_lst = [s for s in sections] |
35 | 33 |
|
36 | | - # fixtures ------------------------------------------------------- |
| 34 | + assert Section_.call_args_list == [call(sectPrs[0]), call(sectPrs[1])] |
| 35 | + assert section_lst == [section_, section_] |
37 | 36 |
|
38 | | - @pytest.fixture |
39 | | - def index_fixture(self, document_elm): |
40 | | - sections = Sections(document_elm) |
41 | | - return sections, [0, 1] |
| 37 | + def it_can_access_its_Section_instances_by_index(self, Section_, section_): |
| 38 | + document_elm = element( |
| 39 | + "w:document/w:body/(w:p/w:pPr/w:sectPr,w:p/w:pPr/w:sectPr,w:sectPr)" |
| 40 | + ) |
| 41 | + sectPrs = document_elm.xpath("//w:sectPr") |
| 42 | + Section_.return_value = section_ |
| 43 | + sections = Sections(document_elm, None) |
42 | 44 |
|
43 | | - @pytest.fixture |
44 | | - def iter_fixture(self, document_elm): |
45 | | - sections = Sections(document_elm) |
46 | | - return sections, 2 |
| 45 | + section_lst = [sections[idx] for idx in range(3)] |
47 | 46 |
|
48 | | - @pytest.fixture |
49 | | - def len_fixture(self, document_elm): |
50 | | - sections = Sections(document_elm) |
51 | | - return sections, 2 |
| 47 | + assert Section_.call_args_list == [ |
| 48 | + call(sectPrs[0]), call(sectPrs[1]), call(sectPrs[2]) |
| 49 | + ] |
| 50 | + assert section_lst == [section_, section_, section_] |
| 51 | + |
| 52 | + def it_can_access_its_Section_instances_by_slice(self, Section_, section_): |
| 53 | + document_elm = element( |
| 54 | + "w:document/w:body/(w:p/w:pPr/w:sectPr,w:p/w:pPr/w:sectPr,w:sectPr)" |
| 55 | + ) |
| 56 | + sectPrs = document_elm.xpath("//w:sectPr") |
| 57 | + Section_.return_value = section_ |
| 58 | + sections = Sections(document_elm, None) |
| 59 | + |
| 60 | + section_lst = sections[1:9] |
| 61 | + |
| 62 | + assert Section_.call_args_list == [call(sectPrs[1]), call(sectPrs[2])] |
| 63 | + assert section_lst == [section_, section_] |
52 | 64 |
|
53 | 65 | # fixture components --------------------------------------------- |
54 | 66 |
|
55 | 67 | @pytest.fixture |
56 | | - def document_elm(self): |
57 | | - return element('w:document/w:body/(w:p/w:pPr/w:sectPr, w:sectPr)') |
| 68 | + def document_part_(self, request): |
| 69 | + return instance_mock(request, DocumentPart) |
| 70 | + |
| 71 | + @pytest.fixture |
| 72 | + def Section_(self, request): |
| 73 | + return class_mock(request, "docx.section.Section") |
| 74 | + |
| 75 | + @pytest.fixture |
| 76 | + def section_(self, request): |
| 77 | + return instance_mock(request, Section) |
58 | 78 |
|
59 | 79 |
|
60 | 80 | class DescribeSection(object): |
|
0 commit comments