|
4 | 4 | Test suite for the docx.text module |
5 | 5 | """ |
6 | 6 |
|
| 7 | +from __future__ import absolute_import, print_function, unicode_literals |
| 8 | + |
| 9 | +from docx.enum.text import WD_BREAK |
7 | 10 | from docx.oxml.text import CT_P |
8 | 11 | from docx.text import Paragraph, Run |
9 | 12 |
|
10 | 13 | import pytest |
11 | 14 |
|
12 | 15 | from mock import call, create_autospec, Mock |
13 | 16 |
|
| 17 | +from .oxml.unitdata.text import a_br, a_t, an_r |
14 | 18 | from .unitutil import class_mock |
15 | 19 |
|
16 | 20 |
|
@@ -68,27 +72,68 @@ def it_can_set_its_paragraph_style(self): |
68 | 72 |
|
69 | 73 | class DescribeRun(object): |
70 | 74 |
|
| 75 | + def it_can_add_text(self, add_text_fixture): |
| 76 | + run, text_str, expected_xml, Text_ = add_text_fixture |
| 77 | + _text = run.add_text(text_str) |
| 78 | + assert run._r.xml == expected_xml |
| 79 | + assert _text is Text_.return_value |
| 80 | + |
| 81 | + def it_can_add_a_break(self, add_break_fixture): |
| 82 | + run, break_type, expected_xml = add_break_fixture |
| 83 | + run.add_break(break_type) |
| 84 | + assert run._r.xml == expected_xml |
| 85 | + |
| 86 | + def it_knows_the_text_it_contains(self, text_prop_fixture): |
| 87 | + run, expected_text = text_prop_fixture |
| 88 | + assert run.text == expected_text |
| 89 | + |
| 90 | + # fixtures ------------------------------------------------------- |
| 91 | + |
| 92 | + @pytest.fixture(params=[ |
| 93 | + 'line', 'page', 'column', 'clr_lt', 'clr_rt', 'clr_all' |
| 94 | + ]) |
| 95 | + def add_break_fixture(self, request, run): |
| 96 | + type_, clear, break_type = { |
| 97 | + 'line': (None, None, WD_BREAK.LINE), |
| 98 | + 'page': ('page', None, WD_BREAK.PAGE), |
| 99 | + 'column': ('column', None, WD_BREAK.COLUMN), |
| 100 | + 'clr_lt': ('textWrapping', 'left', WD_BREAK.LINE_CLEAR_LEFT), |
| 101 | + 'clr_rt': ('textWrapping', 'right', WD_BREAK.LINE_CLEAR_RIGHT), |
| 102 | + 'clr_all': ('textWrapping', 'all', WD_BREAK.LINE_CLEAR_ALL), |
| 103 | + }[request.param] |
| 104 | + # expected_xml ----------------- |
| 105 | + br_bldr = a_br() |
| 106 | + if type_ is not None: |
| 107 | + br_bldr.with_type(type_) |
| 108 | + if clear is not None: |
| 109 | + br_bldr.with_clear(clear) |
| 110 | + expected_xml = an_r().with_nsdecls().with_child(br_bldr).xml() |
| 111 | + return run, break_type, expected_xml |
| 112 | + |
| 113 | + @pytest.fixture |
| 114 | + def add_text_fixture(self, run, Text_): |
| 115 | + text_str = 'foobar' |
| 116 | + expected_xml = ( |
| 117 | + an_r().with_nsdecls().with_child( |
| 118 | + a_t().with_text(text_str)) |
| 119 | + ).xml() |
| 120 | + return run, text_str, expected_xml, Text_ |
| 121 | + |
| 122 | + @pytest.fixture |
| 123 | + def run(self): |
| 124 | + r = an_r().with_nsdecls().element |
| 125 | + return Run(r) |
| 126 | + |
71 | 127 | @pytest.fixture |
72 | 128 | def Text_(self, request): |
73 | 129 | return class_mock(request, 'docx.text.Text') |
74 | 130 |
|
75 | | - def it_can_add_text_to_itself(self, Text_): |
76 | | - # mockery ---------------------- |
77 | | - r_elm = Mock(name='r_elm') |
78 | | - r_elm.add_t.return_value = t_elm = Mock(name='t_elm') |
79 | | - text = Mock(name='text') |
80 | | - r = Run(r_elm) |
81 | | - # exercise --------------------- |
82 | | - t = r.add_text(text) |
83 | | - # verify ----------------------- |
84 | | - r_elm.add_t.assert_called_once_with(text) |
85 | | - Text_.assert_called_once_with(t_elm) |
86 | | - assert t is Text_.return_value |
87 | | - |
88 | | - def it_has_a_composite_of_the_text_it_contains(self): |
89 | | - # mockery ---------------------- |
90 | | - t1, t2 = (Mock(name='t1', text='foo'), Mock(name='t2', text='bar')) |
91 | | - r_elm = Mock(name='r_elm', t_lst=[t1, t2]) |
92 | | - r = Run(r_elm) |
93 | | - # verify ----------------------- |
94 | | - assert r.text == 'foobar' |
| 131 | + @pytest.fixture |
| 132 | + def text_prop_fixture(self, Text_): |
| 133 | + r = ( |
| 134 | + an_r().with_nsdecls().with_child( |
| 135 | + a_t().with_text('foo')).with_child( |
| 136 | + a_t().with_text('bar')) |
| 137 | + ).element |
| 138 | + run = Run(r) |
| 139 | + return run, 'foobar' |
0 commit comments