-
Notifications
You must be signed in to change notification settings - Fork 553
/
Copy pathtest_text.py
1286 lines (1108 loc) · 46.5 KB
/
test_text.py
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# pyright: reportPrivateUsage=false
"""Unit-test suite for `pptx.text.text` module."""
from __future__ import annotations
from typing import TYPE_CHECKING, cast
import pytest
from pptx.dml.color import ColorFormat
from pptx.dml.fill import FillFormat
from pptx.enum.lang import MSO_LANGUAGE_ID
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE, MSO_UNDERLINE, PP_ALIGN
from pptx.opc.constants import RELATIONSHIP_TYPE as RT
from pptx.opc.package import XmlPart
from pptx.shapes.autoshape import Shape
from pptx.text.text import Font, TextFrame, _Hyperlink, _Paragraph, _Run
from pptx.util import Inches, Pt
from ..oxml.unitdata.text import a_p, a_t, an_hlinkClick, an_r, an_rPr
from ..unitutil.cxml import element, xml
from ..unitutil.mock import (
class_mock,
instance_mock,
loose_mock,
method_mock,
property_mock,
)
if TYPE_CHECKING:
from pptx.oxml.text import CT_TextBody, CT_TextParagraph
class DescribeTextFrame(object):
"""Unit-test suite for `pptx.text.text.TextFrame` object."""
def it_can_add_a_paragraph_to_itself(self, add_paragraph_fixture):
text_frame, expected_xml = add_paragraph_fixture
text_frame.add_paragraph()
assert text_frame._txBody.xml == expected_xml
def it_knows_its_autosize_setting(self, autosize_get_fixture):
text_frame, expected_value = autosize_get_fixture
assert text_frame.auto_size == expected_value
@pytest.mark.parametrize(
("txBody_cxml", "value", "expected_cxml"),
[
("p:txBody/a:bodyPr", MSO_AUTO_SIZE.NONE, "p:txBody/a:bodyPr/a:noAutofit"),
(
"p:txBody/a:bodyPr/a:noAutofit",
MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT,
"p:txBody/a:bodyPr/a:spAutoFit",
),
(
"p:txBody/a:bodyPr/a:spAutoFit",
MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE,
"p:txBody/a:bodyPr/a:normAutofit",
),
("p:txBody/a:bodyPr/a:normAutofit", None, "p:txBody/a:bodyPr"),
],
)
def it_can_change_its_autosize_setting(
self, txBody_cxml: str, value: MSO_AUTO_SIZE | None, expected_cxml: str
):
text_frame = TextFrame(element(txBody_cxml), None)
text_frame.auto_size = value
assert text_frame._txBody.xml == xml(expected_cxml)
@pytest.mark.parametrize(
"txBody_cxml",
(
"p:txBody/(a:p,a:p,a:p)",
'p:txBody/a:p/a:r/a:t"foo"',
'p:txBody/a:p/(a:br,a:r/a:t"foo")',
'p:txBody/a:p/(a:fld,a:br,a:r/a:t"foo")',
),
)
def it_can_clear_itself_of_content(self, txBody_cxml):
text_frame = TextFrame(element(txBody_cxml), None)
text_frame.clear()
assert text_frame._element.xml == xml("p:txBody/a:p")
def it_knows_its_margin_settings(self, margin_get_fixture):
text_frame, prop_name, unit, expected_value = margin_get_fixture
margin_value = getattr(text_frame, prop_name)
assert getattr(margin_value, unit) == expected_value
def it_can_change_its_margin_settings(self, margin_set_fixture):
text_frame, prop_name, new_value, expected_xml = margin_set_fixture
setattr(text_frame, prop_name, new_value)
assert text_frame._txBody.xml == expected_xml
@pytest.mark.parametrize(
("txBody_cxml", "expected_value"),
[
("p:txBody/a:bodyPr", None),
("p:txBody/a:bodyPr{anchor=t}", MSO_ANCHOR.TOP),
("p:txBody/a:bodyPr{anchor=b}", MSO_ANCHOR.BOTTOM),
],
)
def it_knows_its_vertical_alignment(self, txBody_cxml: str, expected_value: MSO_ANCHOR | None):
text_frame = TextFrame(cast("CT_TextBody", element(txBody_cxml)), None)
assert text_frame.vertical_anchor == expected_value
@pytest.mark.parametrize(
("txBody_cxml", "new_value", "expected_cxml"),
[
("p:txBody/a:bodyPr", MSO_ANCHOR.TOP, "p:txBody/a:bodyPr{anchor=t}"),
(
"p:txBody/a:bodyPr{anchor=t}",
MSO_ANCHOR.MIDDLE,
"p:txBody/a:bodyPr{anchor=ctr}",
),
(
"p:txBody/a:bodyPr{anchor=ctr}",
MSO_ANCHOR.BOTTOM,
"p:txBody/a:bodyPr{anchor=b}",
),
("p:txBody/a:bodyPr{anchor=b}", None, "p:txBody/a:bodyPr"),
],
)
def it_can_change_its_vertical_alignment(
self, txBody_cxml: str, new_value: MSO_ANCHOR | None, expected_cxml: str
):
text_frame = TextFrame(cast("CT_TextBody", element(txBody_cxml)), None)
text_frame.vertical_anchor = new_value
assert text_frame._element.xml == xml(expected_cxml)
def it_knows_its_word_wrap_setting(self, wrap_get_fixture):
text_frame, expected_value = wrap_get_fixture
assert text_frame.word_wrap == expected_value
def it_can_change_its_word_wrap_setting(self, wrap_set_fixture):
text_frame, new_value, expected_xml = wrap_set_fixture
text_frame.word_wrap = new_value
assert text_frame._element.xml == expected_xml
def it_provides_access_to_its_paragraphs(self, paragraphs_fixture):
text_frame, ps = paragraphs_fixture
paragraphs = text_frame.paragraphs
assert len(paragraphs) == len(ps)
for idx, paragraph in enumerate(paragraphs):
assert isinstance(paragraph, _Paragraph)
assert paragraph._element is ps[idx]
def it_raises_on_attempt_to_set_margin_to_non_int(self):
text_frame = TextFrame(element("p:txBody/a:bodyPr"), None)
with pytest.raises(TypeError):
text_frame.margin_bottom = "0.1"
def it_knows_the_part_it_belongs_to(self, text_frame_with_parent_):
text_frame, parent_ = text_frame_with_parent_
part = text_frame.part
assert part is parent_.part
def it_knows_what_text_it_contains(self, request, text_get_fixture, paragraphs_prop_):
paragraph_texts, expected_value = text_get_fixture
paragraphs_prop_.return_value = tuple(
instance_mock(request, _Paragraph, text=text) for text in paragraph_texts
)
text_frame = TextFrame(None, None)
text = text_frame.text
assert text == expected_value
def it_can_replace_the_text_it_contains(self, text_set_fixture):
txBody, text, expected_xml = text_set_fixture
text_frame = TextFrame(txBody, None)
text_frame.text = text
assert text_frame._element.xml == expected_xml
def it_can_resize_its_text_to_best_fit(self, request, text_prop_):
family, max_size, bold, italic, font_file, font_size = (
"Family",
42,
"bold",
"italic",
"font_file",
21,
)
text_prop_.return_value = "some text"
_best_fit_font_size_ = method_mock(
request, TextFrame, "_best_fit_font_size", return_value=font_size
)
_apply_fit_ = method_mock(request, TextFrame, "_apply_fit")
text_frame = TextFrame(None, None)
text_frame.fit_text(family, max_size, bold, italic, font_file)
_best_fit_font_size_.assert_called_once_with(
text_frame, family, max_size, bold, italic, font_file
)
_apply_fit_.assert_called_once_with(text_frame, family, font_size, bold, italic)
def it_calculates_its_best_fit_font_size_to_help_fit_text(self, size_font_fixture):
text_frame, family, max_size, bold, italic = size_font_fixture[:5]
FontFiles_, TextFitter_, text, extents = size_font_fixture[5:9]
font_file_, font_size_ = size_font_fixture[9:]
font_size = text_frame._best_fit_font_size(family, max_size, bold, italic, None)
FontFiles_.find.assert_called_once_with(family, bold, italic)
TextFitter_.best_fit_font_size.assert_called_once_with(text, extents, max_size, font_file_)
assert font_size is font_size_
def it_calculates_its_effective_size_to_help_fit_text(self):
sp_cxml = (
"p:sp/(p:spPr/a:xfrm/(a:off{x=914400,y=914400},a:ext{cx=914400,c"
"y=914400}),p:txBody/(a:bodyPr,a:p))"
)
text_frame = Shape(element(sp_cxml), None).text_frame
assert text_frame._extents == (731520, 822960)
def it_applies_fit_to_help_fit_text(self, request):
family, font_size, bold, italic = "Family", 42, True, False
_set_font_ = method_mock(request, TextFrame, "_set_font")
text_frame = TextFrame(element("p:txBody/a:bodyPr"), None)
text_frame._apply_fit(family, font_size, bold, italic)
assert text_frame.auto_size is MSO_AUTO_SIZE.NONE
assert text_frame.word_wrap is True
_set_font_.assert_called_once_with(text_frame, family, font_size, bold, italic)
def it_sets_its_font_to_help_fit_text(self, set_font_fixture):
text_frame, family, size, bold, italic, expected_xml = set_font_fixture
text_frame._set_font(family, size, bold, italic)
assert text_frame._element.xml == expected_xml
# fixtures ---------------------------------------------
@pytest.fixture(
params=[
("p:txBody/a:bodyPr", "p:txBody/(a:bodyPr,a:p)"),
("p:txBody/(a:bodyPr,a:p)", "p:txBody/(a:bodyPr,a:p,a:p)"),
]
)
def add_paragraph_fixture(self, request):
txBody_cxml, expected_cxml = request.param
text_frame = TextFrame(element(txBody_cxml), None)
expected_xml = xml(expected_cxml)
return text_frame, expected_xml
@pytest.fixture(
params=[
("p:txBody/a:bodyPr", None),
("p:txBody/a:bodyPr/a:noAutofit", MSO_AUTO_SIZE.NONE),
("p:txBody/a:bodyPr/a:spAutoFit", MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT),
("p:txBody/a:bodyPr/a:normAutofit", MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE),
]
)
def autosize_get_fixture(self, request):
txBody_cxml, expected_value = request.param
text_frame = TextFrame(element(txBody_cxml), None)
return text_frame, expected_value
@pytest.fixture(
params=[
("p:txBody/a:bodyPr", "left", "emu", Inches(0.1)),
("p:txBody/a:bodyPr", "top", "emu", Inches(0.05)),
("p:txBody/a:bodyPr", "right", "emu", Inches(0.1)),
("p:txBody/a:bodyPr", "bottom", "emu", Inches(0.05)),
("p:txBody/a:bodyPr{lIns=9144}", "left", "cm", 0.0254),
("p:txBody/a:bodyPr{tIns=18288}", "top", "mm", 0.508),
("p:txBody/a:bodyPr{rIns=76200}", "right", "pt", 6.0),
("p:txBody/a:bodyPr{bIns=36576}", "bottom", "inches", 0.04),
]
)
def margin_get_fixture(self, request):
txBody_cxml, side, unit, expected_value = request.param
text_frame = TextFrame(element(txBody_cxml), None)
prop_name = "margin_%s" % side
return text_frame, prop_name, unit, expected_value
@pytest.fixture(
params=[
(
"p:txBody/a:bodyPr",
"left",
Inches(0.11),
"p:txBody/a:bodyPr{lIns=100584}",
),
(
"p:txBody/a:bodyPr{tIns=1234}",
"top",
Inches(0.12),
"p:txBody/a:bodyPr{tIns=109728}",
),
(
"p:txBody/a:bodyPr{rIns=2345}",
"right",
Inches(0.13),
"p:txBody/a:bodyPr{rIns=118872}",
),
(
"p:txBody/a:bodyPr{bIns=3456}",
"bottom",
Inches(0.14),
"p:txBody/a:bodyPr{bIns=128016}",
),
("p:txBody/a:bodyPr", "left", Inches(0.1), "p:txBody/a:bodyPr"),
("p:txBody/a:bodyPr", "top", Inches(0.05), "p:txBody/a:bodyPr"),
("p:txBody/a:bodyPr", "right", Inches(0.1), "p:txBody/a:bodyPr"),
("p:txBody/a:bodyPr", "bottom", Inches(0.05), "p:txBody/a:bodyPr"),
]
)
def margin_set_fixture(self, request):
txBody_cxml, side, new_value, expected_txBody_cxml = request.param
text_frame = TextFrame(element(txBody_cxml), None)
prop_name = "margin_%s" % side
expected_xml = xml(expected_txBody_cxml)
return text_frame, prop_name, new_value, expected_xml
@pytest.fixture(params=["p:txBody", "p:txBody/a:p", "p:txBody/(a:p,a:p)"])
def paragraphs_fixture(self, request):
txBody_cxml = request.param
txBody = element(txBody_cxml)
text_frame = TextFrame(txBody, None)
ps = txBody.xpath(".//a:p")
return text_frame, ps
@pytest.fixture(
params=[
(
"p:txBody/(a:bodyPr,a:p/a:r)",
True,
False,
"p:txBody/(a:bodyPr,a:p/(a:r/a:rPr{sz=600,b=1,i=0}/a:latin{typeface"
"=F},a:endParaRPr{sz=600,b=1,i=0}/a:latin{typeface=F}))",
),
(
"p:txBody/a:p/a:br",
True,
False,
"p:txBody/a:p/(a:br/a:rPr{sz=600,b=1,i=0}/a:latin{typeface=F},a:end"
"ParaRPr{sz=600,b=1,i=0}/a:latin{typeface=F})",
),
(
"p:txBody/a:p/a:fld",
True,
False,
"p:txBody/a:p/(a:fld/a:rPr{sz=600,b=1,i=0}/a:latin{typeface=F},a:en"
"dParaRPr{sz=600,b=1,i=0}/a:latin{typeface=F})",
),
]
)
def set_font_fixture(self, request):
txBody_cxml, bold, italic, expected_cxml = request.param
family, size = "F", 6
text_frame = TextFrame(element(txBody_cxml), None)
expected_xml = xml(expected_cxml)
return text_frame, family, size, bold, italic, expected_xml
@pytest.fixture
def size_font_fixture(self, FontFiles_, TextFitter_, text_prop_, _extents_prop_):
text_frame = TextFrame(None, None)
family, max_size, bold, italic = "Family", 42, True, False
text, extents, font_size, font_file = "text", (111, 222), 21, "f.ttf"
text_prop_.return_value = text
_extents_prop_.return_value = extents
FontFiles_.find.return_value = font_file
TextFitter_.best_fit_font_size.return_value = font_size
return (
text_frame,
family,
max_size,
bold,
italic,
FontFiles_,
TextFitter_,
text,
extents,
font_file,
font_size,
)
@pytest.fixture(params=[(["foobar"], "foobar"), (["foo", "bar", "baz"], "foo\nbar\nbaz")])
def text_get_fixture(self, request):
paragraph_texts, expected_value = request.param
return paragraph_texts, expected_value
@pytest.fixture(
params=[
# ---empty to something---
("p:txBody/a:p", "foobar", 'p:txBody/a:p/a:r/a:t"foobar"'),
# ---something to something else---
('p:txBody/a:p/a:r/a:t"foobar"', "barfoo", 'p:txBody/a:p/a:r/a:t"barfoo"'),
# ---single paragraph to multiple---
(
'p:txBody/a:p/a:r/a:t"barfoo"',
"foo\nbar",
'p:txBody/(a:p/a:r/a:t"foo",a:p/a:r/a:t"bar")',
),
# ---multiple paragraphs to single---
(
'p:txBody/(a:p/a:r/a:t"foo",a:p/a:r/a:t"bar")',
"barfoo",
'p:txBody/a:p/a:r/a:t"barfoo"',
),
# ---something to empty---
('p:txBody/a:p/a:r/a:t"foobar"', "", "p:txBody/a:p"),
# ---vertical-tab becomes line-break---
("p:txBody/a:p", "a\vb", 'p:txBody/a:p/(a:r/a:t"a",a:br,a:r/a:t"b")'),
]
)
def text_set_fixture(self, request):
txBody_cxml, text, expected_cxml = request.param
txBody = element(txBody_cxml)
expected_xml = xml(expected_cxml)
return txBody, text, expected_xml
@pytest.fixture(
params=[
("p:txBody/a:bodyPr", None),
("p:txBody/a:bodyPr{wrap=square}", True),
("p:txBody/a:bodyPr{wrap=none}", False),
]
)
def wrap_get_fixture(self, request):
txBody_cxml, expected_value = request.param
text_frame = TextFrame(element(txBody_cxml), None)
return text_frame, expected_value
@pytest.fixture(
params=[
("p:txBody/a:bodyPr", True, "p:txBody/a:bodyPr{wrap=square}"),
("p:txBody/a:bodyPr{wrap=square}", False, "p:txBody/a:bodyPr{wrap=none}"),
("p:txBody/a:bodyPr{wrap=none}", None, "p:txBody/a:bodyPr"),
]
)
def wrap_set_fixture(self, request):
txBody_cxml, new_value, expected_txBody_cxml = request.param
text_frame = TextFrame(element(txBody_cxml), None)
expected_xml = xml(expected_txBody_cxml)
return text_frame, new_value, expected_xml
# fixture components -----------------------------------
@pytest.fixture
def _extents_prop_(self, request):
return property_mock(request, TextFrame, "_extents")
@pytest.fixture
def FontFiles_(self, request):
return class_mock(request, "pptx.text.text.FontFiles")
@pytest.fixture
def paragraphs_prop_(self, request):
return property_mock(request, TextFrame, "paragraphs")
@pytest.fixture
def TextFitter_(self, request):
return class_mock(request, "pptx.text.text.TextFitter")
@pytest.fixture
def text_frame_with_parent_(self, request):
parent_ = loose_mock(request, name="parent_")
text_frame = TextFrame(None, parent_)
return text_frame, parent_
@pytest.fixture
def text_prop_(self, request):
return property_mock(request, TextFrame, "text")
class DescribeFont(object):
"""Unit-test suite for `pptx.text.text.Font` object."""
def it_knows_its_bold_setting(self, bold_get_fixture):
font, expected_value = bold_get_fixture
assert font.bold == expected_value
def it_can_change_its_bold_setting(self, bold_set_fixture):
font, new_value, expected_xml = bold_set_fixture
font.bold = new_value
assert font._element.xml == expected_xml
def it_knows_its_italic_setting(self, italic_get_fixture):
font, expected_value = italic_get_fixture
assert font.italic == expected_value
def it_can_change_its_italic_setting(self, italic_set_fixture):
font, new_value, expected_xml = italic_set_fixture
font.italic = new_value
assert font._element.xml == expected_xml
def it_knows_its_language_id(self, language_id_get_fixture):
font, expected_value = language_id_get_fixture
assert font.language_id == expected_value
def it_can_change_its_language_id_setting(self, language_id_set_fixture):
font, new_value, expected_xml = language_id_set_fixture
font.language_id = new_value
assert font._element.xml == expected_xml
def it_knows_its_superscript_setting(self, superscript_get_fixture):
font, expected_value = superscript_get_fixture
assert font.superscript == expected_value
def it_can_change_its_superscript_setting(self, superscript_set_fixture):
font, new_value, expected_xml = superscript_set_fixture
font.superscript = new_value
assert font._element.xml == expected_xml
def it_knows_its_subscript_setting(self, subscript_get_fixture):
font, expected_value = subscript_get_fixture
assert font.subscript == expected_value
def it_can_change_its_subscript_setting(self, subscript_set_fixture):
font, new_value, expected_xml = subscript_set_fixture
font.subscript = new_value
assert font._element.xml == expected_xml
def it_knows_its_underline_setting(self, underline_get_fixture):
font, expected_value = underline_get_fixture
assert font.underline is expected_value, "got %s" % font.underline
def it_can_change_its_underline_setting(self, underline_set_fixture):
font, new_value, expected_xml = underline_set_fixture
font.underline = new_value
assert font._element.xml == expected_xml
def it_knows_its_size(self, size_get_fixture):
font, expected_value = size_get_fixture
assert font.size == expected_value
def it_can_change_its_size(self, size_set_fixture):
font, new_value, expected_xml = size_set_fixture
font.size = new_value
assert font._element.xml == expected_xml
def it_knows_its_latin_typeface(self, name_get_fixture):
font, expected_value = name_get_fixture
assert font.name == expected_value
def it_can_change_its_latin_typeface(self, name_set_fixture):
font, new_value, expected_xml = name_set_fixture
font.name = new_value
assert font._element.xml == expected_xml
def it_provides_access_to_its_color(self, font):
assert isinstance(font.color, ColorFormat)
def it_provides_access_to_its_fill(self, font):
assert isinstance(font.fill, FillFormat)
# fixtures ---------------------------------------------
@pytest.fixture(params=[("a:rPr", None), ("a:rPr{b=0}", False), ("a:rPr{b=1}", True)])
def bold_get_fixture(self, request):
rPr_cxml, expected_value = request.param
font = Font(element(rPr_cxml))
return font, expected_value
@pytest.fixture(
params=[
("a:rPr", True, "a:rPr{b=1}"),
("a:rPr{b=1}", False, "a:rPr{b=0}"),
("a:rPr{b=0}", None, "a:rPr"),
]
)
def bold_set_fixture(self, request):
rPr_cxml, new_value, expected_rPr_cxml = request.param
font = Font(element(rPr_cxml))
expected_xml = xml(expected_rPr_cxml)
return font, new_value, expected_xml
@pytest.fixture(params=[("a:rPr", None), ("a:rPr{i=0}", False), ("a:rPr{i=1}", True)])
def italic_get_fixture(self, request):
rPr_cxml, expected_value = request.param
font = Font(element(rPr_cxml))
return font, expected_value
@pytest.fixture(
params=[
("a:rPr", True, "a:rPr{i=1}"),
("a:rPr{i=1}", False, "a:rPr{i=0}"),
("a:rPr{i=0}", None, "a:rPr"),
]
)
def italic_set_fixture(self, request):
rPr_cxml, new_value, expected_rPr_cxml = request.param
font = Font(element(rPr_cxml))
expected_xml = xml(expected_rPr_cxml)
return font, new_value, expected_xml
@pytest.fixture(
params=[
("a:rPr", MSO_LANGUAGE_ID.NONE),
("a:rPr{lang=pl-PL}", MSO_LANGUAGE_ID.POLISH),
("a:rPr{lang=de-AT}", MSO_LANGUAGE_ID.GERMAN_AUSTRIA),
("a:rPr{lang=fr-FR}", MSO_LANGUAGE_ID.FRENCH),
]
)
def language_id_get_fixture(self, request):
rPr_cxml, expected_value = request.param
font = Font(element(rPr_cxml))
return font, expected_value
@pytest.fixture(
params=[
("a:rPr", MSO_LANGUAGE_ID.ZULU, "a:rPr{lang=zu-ZA}"),
("a:rPr{lang=zu-ZA}", MSO_LANGUAGE_ID.URDU, "a:rPr{lang=ur-PK}"),
("a:rPr{lang=ur-PK}", MSO_LANGUAGE_ID.NONE, "a:rPr"),
("a:rPr{lang=ur-PK}", None, "a:rPr"),
("a:rPr", MSO_LANGUAGE_ID.NONE, "a:rPr"),
("a:rPr", None, "a:rPr"),
]
)
def language_id_set_fixture(self, request):
rPr_cxml, new_value, expected_rPr_cxml = request.param
font = Font(element(rPr_cxml))
expected_xml = xml(expected_rPr_cxml)
return font, new_value, expected_xml
@pytest.fixture(params=[("a:rPr", None), ("a:rPr/a:latin{typeface=Foobar}", "Foobar")])
def name_get_fixture(self, request):
rPr_cxml, expected_value = request.param
font = Font(element(rPr_cxml))
return font, expected_value
@pytest.fixture(
params=[
("a:rPr", "Foobar", "a:rPr/a:latin{typeface=Foobar}"),
(
"a:rPr/a:latin{typeface=Foobar}",
"Barfoo",
"a:rPr/a:latin{typeface=Barfoo}",
),
("a:rPr/a:latin{typeface=Barfoo}", None, "a:rPr"),
]
)
def name_set_fixture(self, request):
rPr_cxml, new_value, expected_rPr_cxml = request.param
font = Font(element(rPr_cxml))
expected_xml = xml(expected_rPr_cxml)
return font, new_value, expected_xml
@pytest.fixture(params=[("a:rPr", None), ("a:rPr{sz=2400}", 304800)])
def size_get_fixture(self, request):
rPr_cxml, expected_value = request.param
font = Font(element(rPr_cxml))
return font, expected_value
@pytest.fixture(params=[("a:rPr", Pt(24), "a:rPr{sz=2400}"), ("a:rPr{sz=2400}", None, "a:rPr")])
def size_set_fixture(self, request):
rPr_cxml, new_value, expected_rPr_cxml = request.param
font = Font(element(rPr_cxml))
expected_xml = xml(expected_rPr_cxml)
return font, new_value, expected_xml
@pytest.fixture(
params=[
("a:rPr", None),
("a:rPr{baseline=30000}", 30000),
("a:rPr{baseline=15000}", 15000)
]
)
def superscript_get_fixture(self, request):
rPr_cxml, expected_value = request.param
font = Font(element(rPr_cxml))
return font, expected_value
@pytest.fixture(
params=[
("a:rPr", True, "a:rPr{baseline=30000}"),
("a:rPr", 20000, "a:rPr{baseline=20000}"),
("a:rPr{baseline=30000}", False, "a:rPr"),
("a:rPr{baseline=15000}", None, "a:rPr")
]
)
def superscript_set_fixture(self, request):
rPr_cxml, new_value, expected_rPr_cxml = request.param
font = Font(element(rPr_cxml))
expected_xml = xml(expected_rPr_cxml)
return font, new_value, expected_xml
@pytest.fixture(
params=[
("a:rPr", None),
("a:rPr{baseline=-10000}", -10000),
("a:rPr{baseline=-15000}", -15000)
]
)
def subscript_get_fixture(self, request):
rPr_cxml, expected_value = request.param
font = Font(element(rPr_cxml))
return font, expected_value
@pytest.fixture(
params=[
("a:rPr", True, "a:rPr{baseline=-15000}"),
("a:rPr", -10000, "a:rPr{baseline=-10000}"),
("a:rPr", 10000, "a:rPr"),
("a:rPr{baseline=-10000}", False, "a:rPr"),
("a:rPr{baseline=-15000}", None, "a:rPr")
]
)
def subscript_set_fixture(self, request):
rPr_cxml, new_value, expected_rPr_cxml = request.param
font = Font(element(rPr_cxml))
expected_xml = xml(expected_rPr_cxml)
return font, new_value, expected_xml
@pytest.fixture(
params=[
("a:rPr", None),
("a:rPr{u=none}", False),
("a:rPr{u=sng}", True),
("a:rPr{u=dbl}", MSO_UNDERLINE.DOUBLE_LINE),
]
)
def underline_get_fixture(self, request):
rPr_cxml, expected_value = request.param
font = Font(element(rPr_cxml))
return font, expected_value
@pytest.fixture(
params=[
("a:rPr", True, "a:rPr{u=sng}"),
("a:rPr{u=sng}", False, "a:rPr{u=none}"),
("a:rPr{u=none}", MSO_UNDERLINE.WAVY_LINE, "a:rPr{u=wavy}"),
("a:rPr{u=wavy}", MSO_UNDERLINE.NONE, "a:rPr{u=none}"),
("a:rPr{u=wavy}", None, "a:rPr"),
]
)
def underline_set_fixture(self, request):
rPr_cxml, new_value, expected_rPr_cxml = request.param
font = Font(element(rPr_cxml))
expected_xml = xml(expected_rPr_cxml)
return font, new_value, expected_xml
# fixture components ---------------------------------------------
@pytest.fixture
def font(self):
return Font(element("a:rPr"))
class Describe_Hyperlink(object):
"""Unit-test suite for `pptx.text.text._Hyperlink` object."""
def it_knows_the_target_url_of_the_hyperlink(self, hlink_with_url_):
hlink, rId, url = hlink_with_url_
assert hlink.address == url
hlink.part.target_ref.assert_called_once_with(rId)
def it_has_None_for_address_when_no_hyperlink_is_present(self, hlink):
assert hlink.address is None
def it_can_set_the_target_url(self, hlink, rPr_with_hlinkClick_xml, url):
hlink.address = url
# verify -----------------------
hlink.part.relate_to.assert_called_once_with(url, RT.HYPERLINK, is_external=True)
assert hlink._rPr.xml == rPr_with_hlinkClick_xml
assert hlink.address == url
def it_can_remove_the_hyperlink(self, remove_hlink_fixture_):
hlink, rPr_xml, rId = remove_hlink_fixture_
hlink.address = None
assert hlink._rPr.xml == rPr_xml
hlink.part.drop_rel.assert_called_once_with(rId)
def it_should_remove_the_hyperlink_when_url_set_to_empty_string(self, remove_hlink_fixture_):
hlink, rPr_xml, rId = remove_hlink_fixture_
hlink.address = ""
assert hlink._rPr.xml == rPr_xml
hlink.part.drop_rel.assert_called_once_with(rId)
def it_can_change_the_target_url(self, change_hlink_fixture_):
# fixture ----------------------
hlink, rId_existing, new_url, new_rPr_xml = change_hlink_fixture_
# exercise ---------------------
hlink.address = new_url
# verify -----------------------
assert hlink._rPr.xml == new_rPr_xml
hlink.part.drop_rel.assert_called_once_with(rId_existing)
hlink.part.relate_to.assert_called_once_with(new_url, RT.HYPERLINK, is_external=True)
# fixtures ---------------------------------------------
@pytest.fixture
def change_hlink_fixture_(self, request, hlink_with_hlinkClick, rId, rId_2, part_, url_2):
hlinkClick_bldr = an_hlinkClick().with_rId(rId_2)
new_rPr_xml = an_rPr().with_nsdecls("a", "r").with_child(hlinkClick_bldr).xml()
part_.relate_to.return_value = rId_2
property_mock(request, _Hyperlink, "part", return_value=part_)
return hlink_with_hlinkClick, rId, url_2, new_rPr_xml
@pytest.fixture
def hlink(self, request, part_):
rPr = an_rPr().with_nsdecls("a", "r").element
hlink = _Hyperlink(rPr, None)
property_mock(request, _Hyperlink, "part", return_value=part_)
return hlink
@pytest.fixture
def hlink_with_hlinkClick(self, request, rPr_with_hlinkClick_bldr):
rPr = rPr_with_hlinkClick_bldr.element
return _Hyperlink(rPr, None)
@pytest.fixture
def hlink_with_url_(self, request, part_, hlink_with_hlinkClick, rId, url):
property_mock(request, _Hyperlink, "part", return_value=part_)
return hlink_with_hlinkClick, rId, url
@pytest.fixture
def part_(self, request, url, rId):
"""
Mock Part instance suitable for patching into _Hyperlink.part
property. It returns url for target_ref() and rId for relate_to().
"""
part_ = instance_mock(request, XmlPart)
part_.target_ref.return_value = url
part_.relate_to.return_value = rId
return part_
@pytest.fixture
def rId(self):
return "rId2"
@pytest.fixture
def rId_2(self):
return "rId6"
@pytest.fixture
def remove_hlink_fixture_(self, request, hlink_with_hlinkClick, rPr_xml, rId):
property_mock(request, _Hyperlink, "part")
return hlink_with_hlinkClick, rPr_xml, rId
@pytest.fixture
def rPr_with_hlinkClick_bldr(self, rId):
hlinkClick_bldr = an_hlinkClick().with_rId(rId)
rPr_bldr = an_rPr().with_nsdecls("a", "r").with_child(hlinkClick_bldr)
return rPr_bldr
@pytest.fixture
def rPr_with_hlinkClick_xml(self, rPr_with_hlinkClick_bldr):
return rPr_with_hlinkClick_bldr.xml()
@pytest.fixture
def rPr_xml(self):
return an_rPr().with_nsdecls("a", "r").xml()
@pytest.fixture
def url(self):
return "https://github.com/scanny/python-pptx"
@pytest.fixture
def url_2(self):
return "https://pypi.python.org/pypi/python-pptx"
class Describe_Paragraph(object):
"""Unit test suite for pptx.text.text._Paragraph object."""
def it_can_add_a_line_break(self, line_break_fixture):
paragraph, expected_xml = line_break_fixture
paragraph.add_line_break()
assert paragraph._p.xml == expected_xml
def it_can_add_a_run(self, paragraph, p_with_r_xml):
run = paragraph.add_run()
assert paragraph._p.xml == p_with_r_xml
assert isinstance(run, _Run)
def it_knows_its_horizontal_alignment(self, alignment_get_fixture):
paragraph, expected_value = alignment_get_fixture
assert paragraph.alignment == expected_value
def it_can_change_its_horizontal_alignment(self, alignment_set_fixture):
paragraph, new_value, expected_xml = alignment_set_fixture
paragraph.alignment = new_value
assert paragraph._element.xml == expected_xml
def it_can_clear_itself_of_content(self, clear_fixture):
paragraph, expected_xml = clear_fixture
paragraph.clear()
assert paragraph._element.xml == expected_xml
def it_provides_access_to_the_default_paragraph_font(self, paragraph, Font_):
font = paragraph.font
Font_.assert_called_once_with(paragraph._defRPr)
assert font == Font_.return_value
def it_knows_its_indentation_level(self, level_get_fixture):
paragraph, expected_value = level_get_fixture
assert paragraph.level == expected_value
def it_can_change_its_indentation_level(self, level_set_fixture):
paragraph, new_value, expected_xml = level_set_fixture
paragraph.level = new_value
assert paragraph._element.xml == expected_xml
def it_knows_its_line_spacing(self, spacing_get_fixture):
paragraph, expected_value = spacing_get_fixture
assert paragraph.line_spacing == expected_value
def it_can_change_its_line_spacing(self, spacing_set_fixture):
paragraph, new_value, expected_xml = spacing_set_fixture
paragraph.line_spacing = new_value
assert paragraph._element.xml == expected_xml
def it_provides_access_to_its_runs(self, runs_fixture):
paragraph, expected_text = runs_fixture
runs = paragraph.runs
assert tuple(r.text for r in runs) == expected_text
for r in runs:
assert isinstance(r, _Run)
assert r._parent == paragraph
def it_knows_its_space_after(self, after_get_fixture):
paragraph, expected_value = after_get_fixture
assert paragraph.space_after == expected_value
def it_can_change_its_space_after(self, after_set_fixture):
paragraph, new_value, expected_xml = after_set_fixture
paragraph.space_after = new_value
assert paragraph._element.xml == expected_xml
def it_knows_its_space_before(self, before_get_fixture):
paragraph, expected_value = before_get_fixture
assert paragraph.space_before == expected_value
def it_can_change_its_space_before(self, before_set_fixture):
paragraph, new_value, expected_xml = before_set_fixture
paragraph.space_before = new_value
assert paragraph._element.xml == expected_xml
def it_knows_what_text_it_contains(self, text_get_fixture):
p, expected_value = text_get_fixture
paragraph = _Paragraph(p, None)
text = paragraph.text
assert text == expected_value
assert isinstance(text, str)
@pytest.mark.parametrize(
("p_cxml", "value", "expected_cxml"),
[
('a:p/(a:r/a:t"foo",a:r/a:t"bar")', "foobar", 'a:p/a:r/a:t"foobar"'),
("a:p", "", "a:p"),
("a:p", "foobar", 'a:p/a:r/a:t"foobar"'),
("a:p", "foo\nbar", 'a:p/(a:r/a:t"foo",a:br,a:r/a:t"bar")'),
("a:p", "\vfoo\n", 'a:p/(a:br,a:r/a:t"foo",a:br)'),
("a:p", "\n\nfoo", 'a:p/(a:br,a:br,a:r/a:t"foo")'),
("a:p", "foo\n", 'a:p/(a:r/a:t"foo",a:br)'),
("a:p", "foo\x07\n", 'a:p/(a:r/a:t"foo_x0007_",a:br)'),
("a:p", "ŮŦƑ-8\x1bliteral", 'a:p/a:r/a:t"ŮŦƑ-8_x001B_literal"'),
(
"a:p",
"utf-8 unicode: Hér er texti",
'a:p/a:r/a:t"utf-8 unicode: Hér er texti"',
),
],
)
def it_can_change_its_text(self, p_cxml: str, value: str, expected_cxml: str):
p = cast("CT_TextParagraph", element(p_cxml))
paragraph = _Paragraph(p, None)
paragraph.text = value
assert paragraph._element.xml == xml(expected_cxml)
# fixtures ---------------------------------------------
@pytest.fixture(
params=[
("a:p", None),
("a:p/a:pPr", None),
("a:p/a:pPr/a:spcAft/a:spcPct{val=150000}", None),
("a:p/a:pPr/a:spcAft/a:spcPts{val=600}", 76200),
]
)
def after_get_fixture(self, request):
p_cxml, expected_value = request.param
paragraph = _Paragraph(element(p_cxml), None)
return paragraph, expected_value
@pytest.fixture(
params=[
("a:p", Pt(8.333), "a:p/a:pPr/a:spcAft/a:spcPts{val=833}"),
(
"a:p/a:pPr/a:spcAft/a:spcPts{val=600}",
Pt(42),
"a:p/a:pPr/a:spcAft/a:spcPts{val=4200}",
),
(
"a:p/a:pPr/a:spcAft/a:spcPct{val=150000}",
Pt(24),
"a:p/a:pPr/a:spcAft/a:spcPts{val=2400}",
),
("a:p/a:pPr/a:spcAft/a:spcPts{val=600}", None, "a:p/a:pPr"),