1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
# encoding: utf-8
"""
Test suite for the docx.text.tabstops module, containing the TabStops and
TabStop objects.
"""
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
from docx.enum.text import WD_TAB_ALIGNMENT, WD_TAB_LEADER
from docx.shared import Twips
from docx.text.tabstops import TabStop, TabStops
import pytest
from ..unitutil.cxml import element, xml
from ..unitutil.mock import call, class_mock, instance_mock
class DescribeTabStop(object):
def it_knows_its_position(self, position_get_fixture):
tab_stop, expected_value = position_get_fixture
assert tab_stop.position == expected_value
def it_can_change_its_position(self, position_set_fixture):
tab_stop, value, tabs, new_idx, expected_xml = position_set_fixture
tab_stop.position = value
assert tab_stop._tab is tabs[new_idx]
assert tabs.xml == expected_xml
def it_knows_its_alignment(self, alignment_get_fixture):
tab_stop, expected_value = alignment_get_fixture
assert tab_stop.alignment == expected_value
def it_can_change_its_alignment(self, alignment_set_fixture):
tab_stop, value, expected_xml = alignment_set_fixture
tab_stop.alignment = value
assert tab_stop._element.xml == expected_xml
def it_knows_its_leader(self, leader_get_fixture):
tab_stop, expected_value = leader_get_fixture
assert tab_stop.leader == expected_value
def it_can_change_its_leader(self, leader_set_fixture):
tab_stop, value, expected_xml = leader_set_fixture
tab_stop.leader = value
assert tab_stop._element.xml == expected_xml
# fixture --------------------------------------------------------
@pytest.fixture(params=[
('w:tab{w:val=left}', 'LEFT'),
('w:tab{w:val=right}', 'RIGHT'),
])
def alignment_get_fixture(self, request):
tab_stop_cxml, member = request.param
tab_stop = TabStop(element(tab_stop_cxml))
expected_value = getattr(WD_TAB_ALIGNMENT, member)
return tab_stop, expected_value
@pytest.fixture(params=[
('w:tab{w:val=left}', 'RIGHT', 'w:tab{w:val=right}'),
('w:tab{w:val=right}', 'LEFT', 'w:tab{w:val=left}'),
])
def alignment_set_fixture(self, request):
tab_stop_cxml, member, expected_cxml = request.param
tab_stop = TabStop(element(tab_stop_cxml))
expected_xml = xml(expected_cxml)
value = getattr(WD_TAB_ALIGNMENT, member)
return tab_stop, value, expected_xml
@pytest.fixture(params=[
('w:tab', 'SPACES'),
('w:tab{w:leader=none}', 'SPACES'),
('w:tab{w:leader=dot}', 'DOTS'),
])
def leader_get_fixture(self, request):
tab_stop_cxml, member = request.param
tab_stop = TabStop(element(tab_stop_cxml))
expected_value = getattr(WD_TAB_LEADER, member)
return tab_stop, expected_value
@pytest.fixture(params=[
('w:tab', 'DOTS', 'w:tab{w:leader=dot}'),
('w:tab{w:leader=dot}', 'DASHES', 'w:tab{w:leader=hyphen}'),
('w:tab{w:leader=hyphen}', 'SPACES', 'w:tab'),
('w:tab{w:leader=hyphen}', None, 'w:tab'),
('w:tab', 'SPACES', 'w:tab'),
('w:tab', None, 'w:tab'),
])
def leader_set_fixture(self, request):
tab_stop_cxml, new_value, expected_cxml = request.param
tab_stop = TabStop(element(tab_stop_cxml))
value = (
None if new_value is None else getattr(WD_TAB_LEADER, new_value)
)
expected_xml = xml(expected_cxml)
return tab_stop, value, expected_xml
@pytest.fixture
def position_get_fixture(self, request):
tab_stop = TabStop(element('w:tab{w:pos=720}'))
return tab_stop, Twips(720)
@pytest.fixture(params=[
('w:tabs/w:tab{w:pos=360,w:val=left}',
Twips(720), 0,
'w:tabs/w:tab{w:pos=720,w:val=left}'),
('w:tabs/(w:tab{w:pos=360,w:val=left},w:tab{w:pos=720,w:val=left})',
Twips(180), 0,
'w:tabs/(w:tab{w:pos=180,w:val=left},w:tab{w:pos=720,w:val=left})'),
('w:tabs/(w:tab{w:pos=360,w:val=left},w:tab{w:pos=720,w:val=left})',
Twips(960), 1,
'w:tabs/(w:tab{w:pos=720,w:val=left},w:tab{w:pos=960,w:val=left})'),
('w:tabs/(w:tab{w:pos=-72,w:val=left},w:tab{w:pos=-36,w:val=left})',
Twips(-48), 0,
'w:tabs/(w:tab{w:pos=-48,w:val=left},w:tab{w:pos=-36,w:val=left})'),
('w:tabs/(w:tab{w:pos=-72,w:val=left},w:tab{w:pos=-36,w:val=left})',
Twips(-16), 1,
'w:tabs/(w:tab{w:pos=-36,w:val=left},w:tab{w:pos=-16,w:val=left})'),
])
def position_set_fixture(self, request):
tabs_cxml, value, new_idx, expected_cxml = request.param
tabs = element(tabs_cxml)
tab = tabs.tab_lst[0]
tab_stop = TabStop(tab)
expected_xml = xml(expected_cxml)
return tab_stop, value, tabs, new_idx, expected_xml
class DescribeTabStops(object):
def it_knows_its_length(self, len_fixture):
tab_stops, expected_value = len_fixture
assert len(tab_stops) == expected_value
def it_can_iterate_over_its_tab_stops(self, iter_fixture):
tab_stops, expected_count, tab_stop_, TabStop_, expected_calls = (
iter_fixture
)
count = 0
for tab_stop in tab_stops:
assert tab_stop is tab_stop_
count += 1
assert count == expected_count
assert TabStop_.call_args_list == expected_calls
def it_can_get_a_tab_stop_by_index(self, index_fixture):
tab_stops, idx, TabStop_, tab, tab_stop_ = index_fixture
tab_stop = tab_stops[idx]
TabStop_.assert_called_once_with(tab)
assert tab_stop is tab_stop_
def it_raises_on_indexed_access_when_empty(self):
tab_stops = TabStops(element('w:pPr'))
with pytest.raises(IndexError):
tab_stops[0]
def it_can_add_a_tab_stop(self, add_tab_fixture):
tab_stops, position, kwargs, expected_xml = add_tab_fixture
tab_stops.add_tab_stop(position, **kwargs)
assert tab_stops._element.xml == expected_xml
def it_can_delete_a_tab_stop(self, del_fixture):
tab_stops, idx, expected_xml = del_fixture
del tab_stops[idx]
assert tab_stops._element.xml == expected_xml
def it_raises_on_del_idx_invalid(self, del_raises_fixture):
tab_stops, idx = del_raises_fixture
with pytest.raises(IndexError) as exc:
del tab_stops[idx]
assert exc.value.args[0] == 'tab index out of range'
def it_can_clear_all_its_tab_stops(self, clear_all_fixture):
tab_stops, expected_xml = clear_all_fixture
tab_stops.clear_all()
assert tab_stops._element.xml == expected_xml
# fixture --------------------------------------------------------
@pytest.fixture(params=[
'w:pPr',
'w:pPr/w:tabs/w:tab{w:pos=42}',
'w:pPr/w:tabs/(w:tab{w:pos=24},w:tab{w:pos=42})',
])
def clear_all_fixture(self, request):
pPr_cxml = request.param
tab_stops = TabStops(element(pPr_cxml))
expected_xml = xml('w:pPr')
return tab_stops, expected_xml
@pytest.fixture(params=[
('w:pPr/w:tabs/w:tab{w:pos=42}', 0,
'w:pPr'),
('w:pPr/w:tabs/(w:tab{w:pos=24},w:tab{w:pos=42})', 0,
'w:pPr/w:tabs/w:tab{w:pos=42}'),
('w:pPr/w:tabs/(w:tab{w:pos=24},w:tab{w:pos=42})', 1,
'w:pPr/w:tabs/w:tab{w:pos=24}'),
])
def del_fixture(self, request):
pPr_cxml, idx, expected_cxml = request.param
tab_stops = TabStops(element(pPr_cxml))
expected_xml = xml(expected_cxml)
return tab_stops, idx, expected_xml
@pytest.fixture(params=[
('w:pPr', 0),
('w:pPr/w:tabs/w:tab{w:pos=42}', 1),
])
def del_raises_fixture(self, request):
tab_stops_cxml, idx = request.param
tab_stops = TabStops(element(tab_stops_cxml))
return tab_stops, idx
@pytest.fixture(params=[
('w:pPr', Twips(42), {},
'w:pPr/w:tabs/w:tab{w:pos=42,w:val=left}'),
('w:pPr', Twips(72), {'alignment': WD_TAB_ALIGNMENT.RIGHT},
'w:pPr/w:tabs/w:tab{w:pos=72,w:val=right}'),
('w:pPr', Twips(24),
{'alignment': WD_TAB_ALIGNMENT.CENTER,
'leader': WD_TAB_LEADER.DOTS},
'w:pPr/w:tabs/w:tab{w:pos=24,w:val=center,w:leader=dot}'),
('w:pPr/w:tabs/w:tab{w:pos=42}', Twips(72), {},
'w:pPr/w:tabs/(w:tab{w:pos=42},w:tab{w:pos=72,w:val=left})'),
('w:pPr/w:tabs/w:tab{w:pos=42}', Twips(24), {},
'w:pPr/w:tabs/(w:tab{w:pos=24,w:val=left},w:tab{w:pos=42})'),
('w:pPr/w:tabs/w:tab{w:pos=42}', Twips(42), {},
'w:pPr/w:tabs/(w:tab{w:pos=42},w:tab{w:pos=42,w:val=left})'),
])
def add_tab_fixture(self, request):
pPr_cxml, position, kwargs, expected_cxml = request.param
tab_stops = TabStops(element(pPr_cxml))
expected_xml = xml(expected_cxml)
return tab_stops, position, kwargs, expected_xml
@pytest.fixture(params=[
('w:pPr/w:tabs/w:tab{w:pos=0}', 0),
('w:pPr/w:tabs/(w:tab{w:pos=1},w:tab{w:pos=2},w:tab{w:pos=3})', 1),
('w:pPr/w:tabs/(w:tab{w:pos=4},w:tab{w:pos=5},w:tab{w:pos=6})', 2),
])
def index_fixture(self, request, TabStop_, tab_stop_):
pPr_cxml, idx = request.param
pPr = element(pPr_cxml)
tab = pPr.xpath('./w:tabs/w:tab')[idx]
tab_stops = TabStops(pPr)
return tab_stops, idx, TabStop_, tab, tab_stop_
@pytest.fixture(params=[
('w:pPr', 0),
('w:pPr/w:tabs/w:tab{w:pos=2880}', 1),
('w:pPr/w:tabs/(w:tab{w:pos=2880},w:tab{w:pos=5760})', 2),
])
def iter_fixture(self, request, TabStop_, tab_stop_):
pPr_cxml, expected_count = request.param
pPr = element(pPr_cxml)
tab_elms = pPr.xpath('//w:tab')
tab_stops = TabStops(pPr)
expected_calls = [call(tab) for tab in tab_elms]
return tab_stops, expected_count, tab_stop_, TabStop_, expected_calls
@pytest.fixture(params=[
('w:pPr', 0),
('w:pPr/w:tabs/w:tab{w:pos=2880}', 1),
])
def len_fixture(self, request):
tab_stops_cxml, expected_value = request.param
tab_stops = TabStops(element(tab_stops_cxml))
return tab_stops, expected_value
# fixture components ---------------------------------------------
@pytest.fixture
def TabStop_(self, request, tab_stop_):
return class_mock(
request, 'docx.text.tabstops.TabStop', return_value=tab_stop_
)
@pytest.fixture
def tab_stop_(self, request):
return instance_mock(request, TabStop)
|