|
4 | 4 | Test suite for the docx.text module |
5 | 5 | """ |
6 | 6 |
|
7 | | -from __future__ import absolute_import, print_function, unicode_literals |
| 7 | +from __future__ import ( |
| 8 | + absolute_import, division, print_function, unicode_literals |
| 9 | +) |
8 | 10 |
|
9 | 11 | from docx.enum.text import WD_BREAK |
10 | | -from docx.oxml.text import CT_P |
| 12 | +from docx.oxml.text import CT_P, CT_R |
11 | 13 | from docx.text import Paragraph, Run |
12 | 14 |
|
13 | 15 | import pytest |
14 | 16 |
|
15 | | -from mock import call, create_autospec, Mock |
| 17 | +from mock import call, Mock |
16 | 18 |
|
17 | 19 | from .oxml.unitdata.text import a_b, a_br, a_t, a_p, an_i, an_r, an_rPr |
18 | | -from .unitutil import class_mock |
| 20 | +from .unitutil import class_mock, instance_mock |
19 | 21 |
|
20 | 22 |
|
21 | 23 | class DescribeParagraph(object): |
22 | 24 |
|
23 | | - @pytest.fixture |
24 | | - def Run_(self, request): |
25 | | - return class_mock(request, 'docx.text.Run') |
26 | | - |
27 | | - def it_has_a_sequence_of_the_runs_it_contains(self, Run_): |
28 | | - p_elm = Mock(name='p_elm') |
29 | | - r1, r2 = (Mock(name='r1'), Mock(name='r2')) |
30 | | - R1, R2 = (Mock(name='Run1'), Mock(name='Run2')) |
31 | | - p_elm.r_lst = [r1, r2] |
32 | | - p = Paragraph(p_elm) |
33 | | - Run_.side_effect = [R1, R2] |
34 | | - # exercise --------------------- |
35 | | - runs = p.runs |
36 | | - # verify ----------------------- |
37 | | - assert Run_.mock_calls == [call(r1), call(r2)] |
38 | | - assert runs == [R1, R2] |
39 | | - |
40 | | - def it_can_add_a_run_to_itself(self, Run_): |
41 | | - # mockery ---------------------- |
42 | | - p_elm = create_autospec(CT_P) |
43 | | - p_elm.add_r.return_value = r_elm = Mock(name='r_elm') |
44 | | - p = Paragraph(p_elm) |
45 | | - # exercise --------------------- |
46 | | - r = p.add_run() |
47 | | - # verify ----------------------- |
48 | | - p_elm.add_r.assert_called_once_with() |
49 | | - Run_.assert_called_once_with(r_elm) |
50 | | - assert r is Run_.return_value |
| 25 | + def it_has_a_sequence_of_the_runs_it_contains(self, runs_fixture): |
| 26 | + paragraph, Run_, r_, r_2_, run_, run_2_ = runs_fixture |
| 27 | + runs = paragraph.runs |
| 28 | + assert Run_.mock_calls == [call(r_), call(r_2_)] |
| 29 | + assert runs == [run_, run_2_] |
| 30 | + |
| 31 | + def it_can_add_a_run_to_itself(self, add_run_fixture): |
| 32 | + paragraph, expected_xml = add_run_fixture |
| 33 | + run = paragraph.add_run() |
| 34 | + assert paragraph._p.xml == expected_xml |
| 35 | + assert isinstance(run, Run) |
| 36 | + assert run._r is paragraph._p.r_lst[0] |
51 | 37 |
|
52 | 38 | def it_knows_its_paragraph_style(self): |
53 | 39 | cases = ( |
@@ -75,6 +61,47 @@ def it_knows_the_text_it_contains(self, text_prop_fixture): |
75 | 61 |
|
76 | 62 | # fixtures ------------------------------------------------------- |
77 | 63 |
|
| 64 | + @pytest.fixture |
| 65 | + def add_run_fixture(self, paragraph): |
| 66 | + expected_xml = a_p().with_nsdecls().with_child(an_r()).xml() |
| 67 | + return paragraph, expected_xml |
| 68 | + |
| 69 | + @pytest.fixture |
| 70 | + def p_(self, request, r_, r_2_): |
| 71 | + return instance_mock(request, CT_P, r_lst=(r_, r_2_)) |
| 72 | + |
| 73 | + @pytest.fixture |
| 74 | + def paragraph(self, request): |
| 75 | + p = a_p().with_nsdecls().element |
| 76 | + return Paragraph(p) |
| 77 | + |
| 78 | + @pytest.fixture |
| 79 | + def Run_(self, request, runs_): |
| 80 | + run_, run_2_ = runs_ |
| 81 | + return class_mock( |
| 82 | + request, 'docx.text.Run', side_effect=[run_, run_2_] |
| 83 | + ) |
| 84 | + |
| 85 | + @pytest.fixture |
| 86 | + def r_(self, request): |
| 87 | + return instance_mock(request, CT_R) |
| 88 | + |
| 89 | + @pytest.fixture |
| 90 | + def r_2_(self, request): |
| 91 | + return instance_mock(request, CT_R) |
| 92 | + |
| 93 | + @pytest.fixture |
| 94 | + def runs_(self, request): |
| 95 | + run_ = instance_mock(request, Run, name='run_') |
| 96 | + run_2_ = instance_mock(request, Run, name='run_2_') |
| 97 | + return run_, run_2_ |
| 98 | + |
| 99 | + @pytest.fixture |
| 100 | + def runs_fixture(self, p_, Run_, r_, r_2_, runs_): |
| 101 | + paragraph = Paragraph(p_) |
| 102 | + run_, run_2_ = runs_ |
| 103 | + return paragraph, Run_, r_, r_2_, run_, run_2_ |
| 104 | + |
78 | 105 | @pytest.fixture |
79 | 106 | def text_prop_fixture(self): |
80 | 107 | p = ( |
|
0 commit comments