forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_styles.py
More file actions
394 lines (328 loc) · 14.8 KB
/
test_styles.py
File metadata and controls
394 lines (328 loc) · 14.8 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# encoding: utf-8
"""
Test suite for the docx.styles.styles module
"""
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
import pytest
from docx.enum.style import WD_STYLE_TYPE
from docx.oxml.styles import CT_Style, CT_Styles
from docx.styles.latent import LatentStyles
from docx.styles.style import BaseStyle
from docx.styles.styles import Styles
from ..unitutil.cxml import element
from ..unitutil.mock import (
call, class_mock, function_mock, instance_mock, method_mock
)
class DescribeStyles(object):
def it_supports_the_in_operator_on_style_name(self, in_fixture):
styles, name, expected_value = in_fixture
assert (name in styles) is expected_value
def it_knows_its_length(self, len_fixture):
styles, expected_value = len_fixture
assert len(styles) == expected_value
def it_can_iterate_over_its_styles(self, iter_fixture):
styles, expected_count, style_, StyleFactory_, expected_calls = (
iter_fixture
)
count = 0
for style in styles:
assert style is style_
count += 1
assert count == expected_count
assert StyleFactory_.call_args_list == expected_calls
def it_can_get_a_style_by_id(self, getitem_id_fixture):
styles, key, expected_element = getitem_id_fixture
style = styles[key]
assert style._element is expected_element
def it_can_get_a_style_by_name(self, getitem_name_fixture):
styles, key, expected_element = getitem_name_fixture
style = styles[key]
assert style._element is expected_element
def it_raises_on_style_not_found(self, get_raises_fixture):
styles, key = get_raises_fixture
with pytest.raises(KeyError):
styles[key]
def it_can_add_a_new_style(self, add_fixture):
styles, name, style_type, builtin = add_fixture[:4]
name_, StyleFactory_, style_elm_, style_ = add_fixture[4:]
style = styles.add_style(name, style_type, builtin)
styles._element.add_style_of_type.assert_called_once_with(
name_, style_type, builtin
)
StyleFactory_.assert_called_once_with(style_elm_)
assert style is style_
def it_raises_when_style_name_already_used(self, add_raises_fixture):
styles, name = add_raises_fixture
with pytest.raises(ValueError):
styles.add_style(name, None)
def it_can_get_the_default_style_for_a_type(self, default_fixture):
styles, style_type, StyleFactory_ = default_fixture[:3]
StyleFactory_calls, style_ = default_fixture[3:]
style = styles.default(style_type)
assert StyleFactory_.call_args_list == StyleFactory_calls
assert style is style_
def it_can_get_a_style_of_type_by_id(self, get_by_id_fixture):
styles, style_id, style_type = get_by_id_fixture[:3]
default_calls, _get_by_id_calls, style_ = get_by_id_fixture[3:]
style = styles.get_by_id(style_id, style_type)
assert styles.default.call_args_list == default_calls
assert styles._get_by_id.call_args_list == _get_by_id_calls
assert style is style_
def it_can_get_a_style_id(self, get_style_id_fixture):
styles, style_or_name, style_type = get_style_id_fixture[:3]
style_calls, name_calls, style_id_ = get_style_id_fixture[3:]
style_id = styles.get_style_id(style_or_name, style_type)
assert styles._get_style_id_from_style.call_args_list == style_calls
assert styles._get_style_id_from_name.call_args_list == name_calls
assert style_id is style_id_
def it_gets_a_style_by_id_to_help(self, _get_by_id_fixture):
styles, style_id, style_type, default_calls = _get_by_id_fixture[:4]
StyleFactory_, StyleFactory_calls, style_ = _get_by_id_fixture[4:]
style = styles._get_by_id(style_id, style_type)
assert styles.default.call_args_list == default_calls
assert StyleFactory_.call_args_list == StyleFactory_calls
assert style is style_
def it_gets_a_style_id_from_a_name_to_help(self, id_name_fixture):
styles, style_name, style_type, style_, style_id_ = id_name_fixture
style_id = styles._get_style_id_from_name(style_name, style_type)
styles.__getitem__.assert_called_once_with(style_name)
styles._get_style_id_from_style.assert_called_once_with(
style_, style_type
)
assert style_id is style_id_
def it_gets_a_style_id_from_a_style_to_help(self, id_style_fixture):
styles, style_, style_type, style_id_ = id_style_fixture
style_id = styles._get_style_id_from_style(style_, style_type)
styles.default.assert_called_once_with(style_type)
assert style_id is style_id_
def it_raises_on_style_type_mismatch(self, id_style_raises_fixture):
styles, style_, style_type = id_style_raises_fixture
with pytest.raises(ValueError):
styles._get_style_id_from_style(style_, style_type)
def it_provides_access_to_the_latent_styles(self, latent_styles_fixture):
styles, LatentStyles_, latent_styles_ = latent_styles_fixture
latent_styles = styles.latent_styles
LatentStyles_.assert_called_once_with(styles._element.latentStyles)
assert latent_styles is latent_styles_
# fixture --------------------------------------------------------
@pytest.fixture(params=[
('Foo Bar', 'Foo Bar', WD_STYLE_TYPE.CHARACTER, False),
('Heading 1', 'heading 1', WD_STYLE_TYPE.PARAGRAPH, True),
])
def add_fixture(self, request, styles_elm_, _getitem_, style_elm_,
StyleFactory_, style_):
name, name_, style_type, builtin = request.param
styles = Styles(styles_elm_)
_getitem_.return_value = None
styles_elm_.add_style_of_type.return_value = style_elm_
StyleFactory_.return_value = style_
return (
styles, name, style_type, builtin, name_, StyleFactory_,
style_elm_, style_
)
@pytest.fixture
def add_raises_fixture(self, _getitem_):
styles = Styles(element('w:styles/w:style/w:name{w:val=heading 1}'))
name = 'Heading 1'
return styles, name
@pytest.fixture(params=[
('w:styles',
False, WD_STYLE_TYPE.CHARACTER),
('w:styles/w:style{w:type=paragraph,w:default=1}',
True, WD_STYLE_TYPE.PARAGRAPH),
('w:styles/(w:style{w:type=table,w:default=1},w:style{w:type=table,w'
':default=1})',
True, WD_STYLE_TYPE.TABLE),
])
def default_fixture(self, request, StyleFactory_, style_):
styles_cxml, is_defined, style_type = request.param
styles_elm = element(styles_cxml)
styles = Styles(styles_elm)
StyleFactory_calls = [call(styles_elm[-1])] if is_defined else []
StyleFactory_.return_value = style_
expected_value = style_ if is_defined else None
return (
styles, style_type, StyleFactory_, StyleFactory_calls,
expected_value
)
@pytest.fixture(params=[None, 'Foo'])
def get_by_id_fixture(self, request, default_, _get_by_id_, style_):
style_id, style_type = request.param, 1
styles = Styles(None)
default_calls = [call(style_type)] if style_id is None else []
_get_by_id_calls = (
[] if style_id is None else [call(style_id, style_type)]
)
default_.return_value = _get_by_id_.return_value = style_
return (
styles, style_id, style_type, default_calls, _get_by_id_calls,
style_
)
@pytest.fixture(params=[None, BaseStyle(None), 'Style Name'])
def get_style_id_fixture(self, request, _get_style_id_from_style_,
_get_style_id_from_name_):
style_or_name, style_type = request.param, 1
styles = Styles(None)
style_calls = (
[call(style_or_name, style_type)]
if isinstance(style_or_name, BaseStyle) else []
)
name_calls = (
[call(style_or_name, style_type)]
if style_or_name == 'Style Name' else []
)
style_id_ = None if style_or_name is None else 'StyleName'
_get_style_id_from_style_.return_value = style_id_
_get_style_id_from_name_.return_value = style_id_
return (
styles, style_or_name, style_type, style_calls, name_calls,
style_id_
)
@pytest.fixture(params=[
('w:styles/w:style{w:type=paragraph,w:styleId=Foo}', 'Foo',
WD_STYLE_TYPE.PARAGRAPH),
('w:styles/w:style{w:type=paragraph,w:styleId=Foo}', 'Bar',
WD_STYLE_TYPE.PARAGRAPH),
('w:styles/w:style{w:type=table,w:styleId=Bar}', 'Bar',
WD_STYLE_TYPE.PARAGRAPH),
])
def _get_by_id_fixture(self, request, default_, StyleFactory_, style_):
styles_cxml, style_id, style_type = request.param
styles_elm = element(styles_cxml)
style_elm = styles_elm[0]
styles = Styles(styles_elm)
default_calls = [] if style_id == 'Foo' else [call(style_type)]
StyleFactory_calls = [call(style_elm)] if style_id == 'Foo' else []
default_.return_value = StyleFactory_.return_value = style_
return (
styles, style_id, style_type, default_calls, StyleFactory_,
StyleFactory_calls, style_
)
@pytest.fixture(params=[
('w:styles/(w:style{%s,w:styleId=Foobar},w:style,w:style)', 0),
('w:styles/(w:style,w:style{%s,w:styleId=Foobar},w:style)', 1),
('w:styles/(w:style,w:style,w:style{%s,w:styleId=Foobar})', 2),
])
def getitem_id_fixture(self, request):
styles_cxml_tmpl, style_idx = request.param
styles_cxml = styles_cxml_tmpl % 'w:type=paragraph'
styles = Styles(element(styles_cxml))
expected_element = styles._element[style_idx]
return styles, 'Foobar', expected_element
@pytest.fixture(params=[
('w:styles/(w:style%s/w:name{w:val=foo},w:style)', 'foo', 0),
('w:styles/(w:style,w:style%s/w:name{w:val=foo})', 'foo', 1),
('w:styles/w:style%s/w:name{w:val=heading 1}', 'Heading 1', 0),
])
def getitem_name_fixture(self, request):
styles_cxml_tmpl, key, style_idx = request.param
styles_cxml = styles_cxml_tmpl % '{w:type=character}'
styles = Styles(element(styles_cxml))
expected_element = styles._element[style_idx]
return styles, key, expected_element
@pytest.fixture(params=[
('w:styles/(w:style,w:style/w:name{w:val=foo},w:style)'),
('w:styles/(w:style{w:styleId=foo},w:style,w:style)'),
])
def get_raises_fixture(self, request):
styles_cxml = request.param
styles = Styles(element(styles_cxml))
return styles, 'bar'
@pytest.fixture
def id_name_fixture(self, _getitem_, _get_style_id_from_style_, style_):
styles = Styles(None)
style_name, style_type, style_id_ = 'Foo Bar', 1, 'FooBar'
_getitem_.return_value = style_
_get_style_id_from_style_.return_value = style_id_
return styles, style_name, style_type, style_, style_id_
@pytest.fixture(params=[True, False])
def id_style_fixture(self, request, default_, style_):
style_is_default = request.param
styles = Styles(None)
style_id, style_type = 'FooBar', 1
default_.return_value = style_ if style_is_default else None
style_.style_id, style_.type = style_id, style_type
expected_value = None if style_is_default else style_id
return styles, style_, style_type, expected_value
@pytest.fixture
def id_style_raises_fixture(self, style_):
styles = Styles(None)
style_.type = 1
style_type = 2
return styles, style_, style_type
@pytest.fixture(params=[
('w:styles/w:style/w:name{w:val=heading 1}', 'Heading 1', True),
('w:styles/w:style/w:name{w:val=Foo Bar}', 'Foo Bar', True),
('w:styles/w:style/w:name{w:val=heading 1}', 'Foobar', False),
('w:styles', 'Foobar', False),
])
def in_fixture(self, request):
styles_cxml, name, expected_value = request.param
styles = Styles(element(styles_cxml))
return styles, name, expected_value
@pytest.fixture(params=[
('w:styles', 0),
('w:styles/w:style', 1),
('w:styles/(w:style,w:style)', 2),
('w:styles/(w:style,w:style,w:style)', 3),
])
def iter_fixture(self, request, StyleFactory_, style_):
styles_cxml, expected_count = request.param
styles_elm = element(styles_cxml)
styles = Styles(styles_elm)
expected_calls = [call(style_elm) for style_elm in styles_elm]
StyleFactory_.return_value = style_
return styles, expected_count, style_, StyleFactory_, expected_calls
@pytest.fixture
def latent_styles_fixture(self, LatentStyles_, latent_styles_):
styles = Styles(element('w:styles/w:latentStyles'))
return styles, LatentStyles_, latent_styles_
@pytest.fixture(params=[
('w:styles', 0),
('w:styles/w:style', 1),
('w:styles/(w:style,w:style)', 2),
('w:styles/(w:style,w:style,w:style)', 3),
])
def len_fixture(self, request):
styles_cxml, expected_value = request.param
styles = Styles(element(styles_cxml))
return styles, expected_value
# fixture components ---------------------------------------------
@pytest.fixture
def default_(self, request):
return method_mock(request, Styles, 'default')
@pytest.fixture
def _get_by_id_(self, request):
return method_mock(request, Styles, '_get_by_id')
@pytest.fixture
def _getitem_(self, request):
return method_mock(request, Styles, '__getitem__')
@pytest.fixture
def _get_style_id_from_name_(self, request):
return method_mock(request, Styles, '_get_style_id_from_name')
@pytest.fixture
def _get_style_id_from_style_(self, request):
return method_mock(request, Styles, '_get_style_id_from_style')
@pytest.fixture
def LatentStyles_(self, request, latent_styles_):
return class_mock(
request, 'docx.styles.styles.LatentStyles',
return_value=latent_styles_
)
@pytest.fixture
def latent_styles_(self, request):
return instance_mock(request, LatentStyles)
@pytest.fixture
def style_(self, request):
return instance_mock(request, BaseStyle)
@pytest.fixture
def StyleFactory_(self, request):
return function_mock(request, 'docx.styles.styles.StyleFactory')
@pytest.fixture
def style_elm_(self, request):
return instance_mock(request, CT_Style)
@pytest.fixture
def styles_elm_(self, request):
return instance_mock(request, CT_Styles)