forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_xmlchemy.py
More file actions
843 lines (677 loc) · 27.5 KB
/
test_xmlchemy.py
File metadata and controls
843 lines (677 loc) · 27.5 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
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
# encoding: utf-8
"""
Test suite for docx.oxml.xmlchemy
"""
from __future__ import absolute_import, print_function, unicode_literals
import pytest
from docx.compat import Unicode
from docx.oxml import parse_xml, register_element_cls
from docx.oxml.exceptions import InvalidXmlError
from docx.oxml.ns import qn
from docx.oxml.simpletypes import BaseIntType
from docx.oxml.xmlchemy import (
BaseOxmlElement, Choice, serialize_for_reading, OneOrMore, OneAndOnlyOne,
OptionalAttribute, RequiredAttribute, ZeroOrMore, ZeroOrOne,
ZeroOrOneChoice, XmlString
)
from ..unitdata import BaseBuilder
from .unitdata.text import a_b, a_u, an_i, an_rPr
class DescribeBaseOxmlElement(object):
def it_can_find_the_first_of_its_children_named_in_a_sequence(
self, first_fixture):
element, tagnames, matching_child = first_fixture
assert element.first_child_found_in(*tagnames) is matching_child
def it_can_insert_an_element_before_named_successors(
self, insert_fixture):
element, child, tagnames, expected_xml = insert_fixture
element.insert_element_before(child, *tagnames)
assert element.xml == expected_xml
def it_can_remove_all_children_with_name_in_sequence(
self, remove_fixture):
element, tagnames, expected_xml = remove_fixture
element.remove_all(*tagnames)
assert element.xml == expected_xml
# fixtures ---------------------------------------------
@pytest.fixture(params=[
('biu', 'iu', 'i'),
('bu', 'iu', 'u'),
('bi', 'u', None),
('b', 'iu', None),
('iu', 'biu', 'i'),
('', 'biu', None),
])
def first_fixture(self, request):
present, matching, match = request.param
element = self.rPr_bldr(present).element
tagnames = self.nsptags(matching)
matching_child = element.find(qn('w:%s' % match)) if match else None
return element, tagnames, matching_child
@pytest.fixture(params=[
('iu', 'b', 'iu', 'biu'),
('u', 'b', 'iu', 'bu'),
('', 'b', 'iu', 'b'),
('bu', 'i', 'u', 'biu'),
('bi', 'u', '', 'biu'),
])
def insert_fixture(self, request):
present, new, successors, after = request.param
element = self.rPr_bldr(present).element
child = {
'b': a_b(), 'i': an_i(), 'u': a_u()
}[new].with_nsdecls().element
tagnames = [('w:%s' % char) for char in successors]
expected_xml = self.rPr_bldr(after).xml()
return element, child, tagnames, expected_xml
@pytest.fixture(params=[
('biu', 'b', 'iu'), ('biu', 'bi', 'u'), ('bbiiuu', 'i', 'bbuu'),
('biu', 'i', 'bu'), ('biu', 'bu', 'i'), ('bbiiuu', '', 'bbiiuu'),
('biu', 'u', 'bi'), ('biu', 'ui', 'b'), ('bbiiuu', 'bi', 'uu'),
('bu', 'i', 'bu'), ('', 'ui', ''),
])
def remove_fixture(self, request):
present, remove, after = request.param
element = self.rPr_bldr(present).element
tagnames = self.nsptags(remove)
expected_xml = self.rPr_bldr(after).xml()
return element, tagnames, expected_xml
# fixture components ---------------------------------------------
def nsptags(self, letters):
return [('w:%s' % letter) for letter in letters]
def rPr_bldr(self, children):
rPr_bldr = an_rPr().with_nsdecls()
for char in children:
if char == 'b':
rPr_bldr.with_child(a_b())
elif char == 'i':
rPr_bldr.with_child(an_i())
elif char == 'u':
rPr_bldr.with_child(a_u())
else:
raise NotImplementedError("got '%s'" % char)
return rPr_bldr
class DescribeSerializeForReading(object):
def it_pretty_prints_an_lxml_element(self, pretty_fixture):
element, expected_xml_text = pretty_fixture
xml_text = serialize_for_reading(element)
assert xml_text == expected_xml_text
def it_returns_unicode_text(self, type_fixture):
element = type_fixture
xml_text = serialize_for_reading(element)
assert isinstance(xml_text, Unicode)
# fixtures ---------------------------------------------
@pytest.fixture
def pretty_fixture(self, element):
expected_xml_text = (
'<foø>\n'
' <bår>text</bår>\n'
'</foø>\n'
)
return element, expected_xml_text
@pytest.fixture
def type_fixture(self, element):
return element
# fixture components -----------------------------------
@pytest.fixture
def element(self):
return parse_xml('<foø><bår>text</bår></foø>')
class DescribeXmlString(object):
def it_parses_a_line_to_help_compare(self, parse_fixture):
"""
This internal function is important to test separately because if it
doesn't parse a line properly, false equality can result.
"""
line, expected_front, expected_attrs = parse_fixture[:3]
expected_close, expected_text = parse_fixture[3:]
front, attrs, close, text = XmlString._parse_line(line)
# print("'%s' '%s' '%s' %s" % (
# front, attrs, close, ('%s' % text) if text else text))
assert front == expected_front
assert attrs == expected_attrs
assert close == expected_close
assert text == expected_text
def it_knows_if_two_xml_lines_are_equivalent(self, xml_line_case):
line, other, differs = xml_line_case
xml = XmlString(line)
assert xml == other
assert xml != differs
# fixtures ---------------------------------------------
@pytest.fixture(params=[
('<a>text</a>', '<a', '', '>', 'text</a>'),
('<a:f/>', '<a:f', '', '/>', None),
('<a:f b="c"/>', '<a:f', ' b="c"', '/>', None),
('<a:f>t</a:f>', '<a:f', '', '>', 't</a:f>'),
('<dcterms:created xsi:type="dcterms:W3CDTF">2013-12-23T23:15:00Z</d'
'cterms:created>', '<dcterms:created', ' xsi:type="dcterms:W3CDTF"',
'>', '2013-12-23T23:15:00Z</dcterms:created>'),
])
def parse_fixture(self, request):
line, front, attrs, close, text = request.param
return line, front, attrs, close, text
@pytest.fixture(params=[
'simple_elm', 'nsp_tagname', 'indent', 'attrs', 'nsdecl_order',
'closing_elm',
])
def xml_line_case(self, request):
cases = {
'simple_elm': (
'<name/>',
'<name/>',
'<name>',
),
'nsp_tagname': (
'<xyz:name/>',
'<xyz:name/>',
'<abc:name/>',
),
'indent': (
' <xyz:name/>',
' <xyz:name/>',
'<xyz:name/>',
),
'attrs': (
' <abc:Name foo="bar" bar="foo">',
' <abc:Name bar="foo" foo="bar">',
' <abc:Name far="boo" foo="bar">',
),
'nsdecl_order': (
' <name xmlns:a="http://ns/1" xmlns:b="http://ns/2"/>',
' <name xmlns:b="http://ns/2" xmlns:a="http://ns/1"/>',
' <name xmlns:b="http://ns/2" xmlns:a="http://ns/1">',
),
'closing_elm': (
'</xyz:name>',
'</xyz:name>',
'<xyz:name>',
),
}
line, other, differs = cases[request.param]
return line, other, differs
class DescribeChoice(object):
def it_adds_a_getter_property_for_the_choice_element(
self, getter_fixture):
parent, expected_choice = getter_fixture
assert parent.choice is expected_choice
def it_adds_a_creator_method_for_the_child_element(self, new_fixture):
parent, expected_xml = new_fixture
choice = parent._new_choice()
assert choice.xml == expected_xml
def it_adds_an_insert_method_for_the_child_element(self, insert_fixture):
parent, choice, expected_xml = insert_fixture
parent._insert_choice(choice)
assert parent.xml == expected_xml
assert parent._insert_choice.__doc__.startswith(
'Return the passed ``<w:choice>`` '
)
def it_adds_an_add_method_for_the_child_element(self, add_fixture):
parent, expected_xml = add_fixture
choice = parent._add_choice()
assert parent.xml == expected_xml
assert isinstance(choice, CT_Choice)
assert parent._add_choice.__doc__.startswith(
'Add a new ``<w:choice>`` child element '
)
def it_adds_a_get_or_change_to_method_for_the_child_element(
self, get_or_change_to_fixture):
parent, expected_xml = get_or_change_to_fixture
choice = parent.get_or_change_to_choice()
assert isinstance(choice, CT_Choice)
assert parent.xml == expected_xml
# fixtures -------------------------------------------------------
@pytest.fixture
def add_fixture(self):
parent = self.parent_bldr().element
expected_xml = self.parent_bldr('choice').xml()
return parent, expected_xml
@pytest.fixture(params=[
('choice2', 'choice'),
(None, 'choice'),
('choice', 'choice'),
])
def get_or_change_to_fixture(self, request):
before_member_tag, after_member_tag = request.param
parent = self.parent_bldr(before_member_tag).element
expected_xml = self.parent_bldr(after_member_tag).xml()
return parent, expected_xml
@pytest.fixture(params=['choice', None])
def getter_fixture(self, request):
choice_tag = request.param
parent = self.parent_bldr(choice_tag).element
expected_choice = parent.find(qn('w:choice')) # None if not found
return parent, expected_choice
@pytest.fixture
def insert_fixture(self):
parent = (
a_parent().with_nsdecls().with_child(
an_oomChild()).with_child(
an_oooChild())
).element
choice = a_choice().with_nsdecls().element
expected_xml = (
a_parent().with_nsdecls().with_child(
a_choice()).with_child(
an_oomChild()).with_child(
an_oooChild())
).xml()
return parent, choice, expected_xml
@pytest.fixture
def new_fixture(self):
parent = self.parent_bldr().element
expected_xml = a_choice().with_nsdecls().xml()
return parent, expected_xml
# fixture components ---------------------------------------------
def parent_bldr(self, choice_tag=None):
parent_bldr = a_parent().with_nsdecls()
if choice_tag == 'choice':
parent_bldr.with_child(a_choice())
if choice_tag == 'choice2':
parent_bldr.with_child(a_choice2())
return parent_bldr
class DescribeOneAndOnlyOne(object):
def it_adds_a_getter_property_for_the_child_element(self, getter_fixture):
parent, oooChild = getter_fixture
assert parent.oooChild is oooChild
# fixtures -------------------------------------------------------
@pytest.fixture
def getter_fixture(self):
parent = a_parent().with_nsdecls().with_child(an_oooChild()).element
oooChild = parent.find(qn('w:oooChild'))
return parent, oooChild
class DescribeOneOrMore(object):
def it_adds_a_getter_property_for_the_child_element_list(
self, getter_fixture):
parent, oomChild = getter_fixture
assert parent.oomChild_lst[0] is oomChild
def it_adds_a_creator_method_for_the_child_element(self, new_fixture):
parent, expected_xml = new_fixture
oomChild = parent._new_oomChild()
assert oomChild.xml == expected_xml
def it_adds_an_insert_method_for_the_child_element(self, insert_fixture):
parent, oomChild, expected_xml = insert_fixture
parent._insert_oomChild(oomChild)
assert parent.xml == expected_xml
assert parent._insert_oomChild.__doc__.startswith(
'Return the passed ``<w:oomChild>`` '
)
def it_adds_a_private_add_method_for_the_child_element(self, add_fixture):
parent, expected_xml = add_fixture
oomChild = parent._add_oomChild()
assert parent.xml == expected_xml
assert isinstance(oomChild, CT_OomChild)
assert parent._add_oomChild.__doc__.startswith(
'Add a new ``<w:oomChild>`` child element '
)
def it_adds_a_public_add_method_for_the_child_element(self, add_fixture):
parent, expected_xml = add_fixture
oomChild = parent.add_oomChild()
assert parent.xml == expected_xml
assert isinstance(oomChild, CT_OomChild)
assert parent._add_oomChild.__doc__.startswith(
'Add a new ``<w:oomChild>`` child element '
)
# fixtures -------------------------------------------------------
@pytest.fixture
def add_fixture(self):
parent = self.parent_bldr(False).element
expected_xml = self.parent_bldr(True).xml()
return parent, expected_xml
@pytest.fixture
def getter_fixture(self):
parent = self.parent_bldr(True).element
oomChild = parent.find(qn('w:oomChild'))
return parent, oomChild
@pytest.fixture
def insert_fixture(self):
parent = (
a_parent().with_nsdecls().with_child(
an_oooChild()).with_child(
a_zomChild()).with_child(
a_zooChild())
).element
oomChild = an_oomChild().with_nsdecls().element
expected_xml = (
a_parent().with_nsdecls().with_child(
an_oomChild()).with_child(
an_oooChild()).with_child(
a_zomChild()).with_child(
a_zooChild())
).xml()
return parent, oomChild, expected_xml
@pytest.fixture
def new_fixture(self):
parent = self.parent_bldr(False).element
expected_xml = an_oomChild().with_nsdecls().xml()
return parent, expected_xml
# fixture components ---------------------------------------------
def parent_bldr(self, oomChild_is_present):
parent_bldr = a_parent().with_nsdecls()
if oomChild_is_present:
parent_bldr.with_child(an_oomChild())
return parent_bldr
class DescribeOptionalAttribute(object):
def it_adds_a_getter_property_for_the_attr_value(self, getter_fixture):
parent, optAttr_python_value = getter_fixture
assert parent.optAttr == optAttr_python_value
def it_adds_a_setter_property_for_the_attr(self, setter_fixture):
parent, value, expected_xml = setter_fixture
parent.optAttr = value
assert parent.xml == expected_xml
def it_adds_a_docstring_for_the_property(self):
assert CT_Parent.optAttr.__doc__.startswith(
"ST_IntegerType type-converted value of "
)
# fixtures -------------------------------------------------------
@pytest.fixture
def getter_fixture(self):
parent = a_parent().with_nsdecls().with_optAttr('24').element
return parent, 24
@pytest.fixture(params=[36, None])
def setter_fixture(self, request):
value = request.param
parent = a_parent().with_nsdecls().with_optAttr('42').element
if value is None:
expected_xml = a_parent().with_nsdecls().xml()
else:
expected_xml = a_parent().with_nsdecls().with_optAttr(value).xml()
return parent, value, expected_xml
class DescribeRequiredAttribute(object):
def it_adds_a_getter_property_for_the_attr_value(self, getter_fixture):
parent, reqAttr_python_value = getter_fixture
assert parent.reqAttr == reqAttr_python_value
def it_adds_a_setter_property_for_the_attr(self, setter_fixture):
parent, value, expected_xml = setter_fixture
parent.reqAttr = value
assert parent.xml == expected_xml
def it_adds_a_docstring_for_the_property(self):
assert CT_Parent.reqAttr.__doc__.startswith(
"ST_IntegerType type-converted value of "
)
def it_raises_on_get_when_attribute_not_present(self):
parent = a_parent().with_nsdecls().element
with pytest.raises(InvalidXmlError):
parent.reqAttr
def it_raises_on_assign_invalid_value(self, invalid_assign_fixture):
parent, value, expected_exception = invalid_assign_fixture
with pytest.raises(expected_exception):
parent.reqAttr = value
# fixtures -------------------------------------------------------
@pytest.fixture
def getter_fixture(self):
parent = a_parent().with_nsdecls().with_reqAttr('42').element
return parent, 42
@pytest.fixture(params=[
(None, TypeError),
(-4, ValueError),
('2', TypeError),
])
def invalid_assign_fixture(self, request):
invalid_value, expected_exception = request.param
parent = a_parent().with_nsdecls().with_reqAttr(1).element
return parent, invalid_value, expected_exception
@pytest.fixture
def setter_fixture(self):
parent = a_parent().with_nsdecls().with_reqAttr('42').element
value = 24
expected_xml = a_parent().with_nsdecls().with_reqAttr(value).xml()
return parent, value, expected_xml
class DescribeZeroOrMore(object):
def it_adds_a_getter_property_for_the_child_element_list(
self, getter_fixture):
parent, zomChild = getter_fixture
assert parent.zomChild_lst[0] is zomChild
def it_adds_a_creator_method_for_the_child_element(self, new_fixture):
parent, expected_xml = new_fixture
zomChild = parent._new_zomChild()
assert zomChild.xml == expected_xml
def it_adds_an_insert_method_for_the_child_element(self, insert_fixture):
parent, zomChild, expected_xml = insert_fixture
parent._insert_zomChild(zomChild)
assert parent.xml == expected_xml
assert parent._insert_zomChild.__doc__.startswith(
'Return the passed ``<w:zomChild>`` '
)
def it_adds_an_add_method_for_the_child_element(self, add_fixture):
parent, expected_xml = add_fixture
zomChild = parent._add_zomChild()
assert parent.xml == expected_xml
assert isinstance(zomChild, CT_ZomChild)
assert parent._add_zomChild.__doc__.startswith(
'Add a new ``<w:zomChild>`` child element '
)
def it_adds_a_public_add_method_for_the_child_element(self, add_fixture):
parent, expected_xml = add_fixture
zomChild = parent.add_zomChild()
assert parent.xml == expected_xml
assert isinstance(zomChild, CT_ZomChild)
assert parent._add_zomChild.__doc__.startswith(
'Add a new ``<w:zomChild>`` child element '
)
def it_removes_the_property_root_name_used_for_declaration(self):
assert not hasattr(CT_Parent, 'zomChild')
# fixtures -------------------------------------------------------
@pytest.fixture
def add_fixture(self):
parent = self.parent_bldr(False).element
expected_xml = self.parent_bldr(True).xml()
return parent, expected_xml
@pytest.fixture
def getter_fixture(self):
parent = self.parent_bldr(True).element
zomChild = parent.find(qn('w:zomChild'))
return parent, zomChild
@pytest.fixture
def insert_fixture(self):
parent = (
a_parent().with_nsdecls().with_child(
an_oomChild()).with_child(
an_oooChild()).with_child(
a_zooChild())
).element
zomChild = a_zomChild().with_nsdecls().element
expected_xml = (
a_parent().with_nsdecls().with_child(
an_oomChild()).with_child(
an_oooChild()).with_child(
a_zomChild()).with_child(
a_zooChild())
).xml()
return parent, zomChild, expected_xml
@pytest.fixture
def new_fixture(self):
parent = self.parent_bldr(False).element
expected_xml = a_zomChild().with_nsdecls().xml()
return parent, expected_xml
def parent_bldr(self, zomChild_is_present):
parent_bldr = a_parent().with_nsdecls()
if zomChild_is_present:
parent_bldr.with_child(a_zomChild())
return parent_bldr
class DescribeZeroOrOne(object):
def it_adds_a_getter_property_for_the_child_element(self, getter_fixture):
parent, zooChild = getter_fixture
assert parent.zooChild is zooChild
def it_adds_an_add_method_for_the_child_element(self, add_fixture):
parent, expected_xml = add_fixture
zooChild = parent._add_zooChild()
assert parent.xml == expected_xml
assert isinstance(zooChild, CT_ZooChild)
assert parent._add_zooChild.__doc__.startswith(
'Add a new ``<w:zooChild>`` child element '
)
def it_adds_an_insert_method_for_the_child_element(self, insert_fixture):
parent, zooChild, expected_xml = insert_fixture
parent._insert_zooChild(zooChild)
assert parent.xml == expected_xml
assert parent._insert_zooChild.__doc__.startswith(
'Return the passed ``<w:zooChild>`` '
)
def it_adds_a_get_or_add_method_for_the_child_element(
self, get_or_add_fixture):
parent, expected_xml = get_or_add_fixture
zooChild = parent.get_or_add_zooChild()
assert isinstance(zooChild, CT_ZooChild)
assert parent.xml == expected_xml
def it_adds_a_remover_method_for_the_child_element(self, remove_fixture):
parent, expected_xml = remove_fixture
parent._remove_zooChild()
assert parent.xml == expected_xml
# fixtures -------------------------------------------------------
@pytest.fixture
def add_fixture(self):
parent = self.parent_bldr(False).element
expected_xml = self.parent_bldr(True).xml()
return parent, expected_xml
@pytest.fixture(params=[True, False])
def getter_fixture(self, request):
zooChild_is_present = request.param
parent = self.parent_bldr(zooChild_is_present).element
zooChild = parent.find(qn('w:zooChild')) # None if not found
return parent, zooChild
@pytest.fixture(params=[True, False])
def get_or_add_fixture(self, request):
zooChild_is_present = request.param
parent = self.parent_bldr(zooChild_is_present).element
expected_xml = self.parent_bldr(True).xml()
return parent, expected_xml
@pytest.fixture
def insert_fixture(self):
parent = (
a_parent().with_nsdecls().with_child(
an_oomChild()).with_child(
an_oooChild()).with_child(
a_zomChild())
).element
zooChild = a_zooChild().with_nsdecls().element
expected_xml = (
a_parent().with_nsdecls().with_child(
an_oomChild()).with_child(
an_oooChild()).with_child(
a_zomChild()).with_child(
a_zooChild())
).xml()
return parent, zooChild, expected_xml
@pytest.fixture(params=[True, False])
def remove_fixture(self, request):
zooChild_is_present = request.param
parent = self.parent_bldr(zooChild_is_present).element
expected_xml = self.parent_bldr(False).xml()
return parent, expected_xml
# fixture components ---------------------------------------------
def parent_bldr(self, zooChild_is_present):
parent_bldr = a_parent().with_nsdecls()
if zooChild_is_present:
parent_bldr.with_child(a_zooChild())
return parent_bldr
class DescribeZeroOrOneChoice(object):
def it_adds_a_getter_for_the_current_choice(self, getter_fixture):
parent, expected_choice = getter_fixture
assert parent.eg_zooChoice is expected_choice
# fixtures -------------------------------------------------------
@pytest.fixture(params=[None, 'choice', 'choice2'])
def getter_fixture(self, request):
choice_tag = request.param
parent = self.parent_bldr(choice_tag).element
tagname = 'w:%s' % choice_tag
expected_choice = parent.find(qn(tagname)) # None if not found
return parent, expected_choice
# fixture components ---------------------------------------------
def parent_bldr(self, choice_tag=None):
parent_bldr = a_parent().with_nsdecls()
if choice_tag == 'choice':
parent_bldr.with_child(a_choice())
if choice_tag == 'choice2':
parent_bldr.with_child(a_choice2())
return parent_bldr
# --------------------------------------------------------------------
# static shared fixture
# --------------------------------------------------------------------
class ST_IntegerType(BaseIntType):
@classmethod
def validate(cls, value):
cls.validate_int(value)
if value < 1 or value > 42:
raise ValueError(
"value must be in range 1 to 42 inclusive"
)
class CT_Parent(BaseOxmlElement):
"""
``<w:parent>`` element, an invented element for use in testing.
"""
eg_zooChoice = ZeroOrOneChoice(
(Choice('w:choice'), Choice('w:choice2')),
successors=('w:oomChild', 'w:oooChild')
)
oomChild = OneOrMore('w:oomChild', successors=(
'w:oooChild', 'w:zomChild', 'w:zooChild'
))
oooChild = OneAndOnlyOne('w:oooChild')
zomChild = ZeroOrMore('w:zomChild', successors=('w:zooChild',))
zooChild = ZeroOrOne('w:zooChild', successors=())
optAttr = OptionalAttribute('w:optAttr', ST_IntegerType)
reqAttr = RequiredAttribute('reqAttr', ST_IntegerType)
class CT_Choice(BaseOxmlElement):
"""
``<w:choice>`` element
"""
class CT_OomChild(BaseOxmlElement):
"""
Oom standing for 'OneOrMore', ``<w:oomChild>`` element, representing a
child element that can appear multiple times in sequence, but must appear
at least once.
"""
class CT_ZomChild(BaseOxmlElement):
"""
Zom standing for 'ZeroOrMore', ``<w:zomChild>`` element, representing an
optional child element that can appear multiple times in sequence.
"""
class CT_ZooChild(BaseOxmlElement):
"""
Zoo standing for 'ZeroOrOne', ``<w:zooChild>`` element, an invented
element for use in testing.
"""
register_element_cls('w:parent', CT_Parent)
register_element_cls('w:choice', CT_Choice)
register_element_cls('w:oomChild', CT_OomChild)
register_element_cls('w:zomChild', CT_ZomChild)
register_element_cls('w:zooChild', CT_ZooChild)
class CT_ChoiceBuilder(BaseBuilder):
__tag__ = 'w:choice'
__nspfxs__ = ('w',)
__attrs__ = ()
class CT_Choice2Builder(BaseBuilder):
__tag__ = 'w:choice2'
__nspfxs__ = ('w',)
__attrs__ = ()
class CT_ParentBuilder(BaseBuilder):
__tag__ = 'w:parent'
__nspfxs__ = ('w',)
__attrs__ = ('w:optAttr', 'reqAttr')
class CT_OomChildBuilder(BaseBuilder):
__tag__ = 'w:oomChild'
__nspfxs__ = ('w',)
__attrs__ = ()
class CT_OooChildBuilder(BaseBuilder):
__tag__ = 'w:oooChild'
__nspfxs__ = ('w',)
__attrs__ = ()
class CT_ZomChildBuilder(BaseBuilder):
__tag__ = 'w:zomChild'
__nspfxs__ = ('w',)
__attrs__ = ()
class CT_ZooChildBuilder(BaseBuilder):
__tag__ = 'w:zooChild'
__nspfxs__ = ('w',)
__attrs__ = ()
def a_choice():
return CT_ChoiceBuilder()
def a_choice2():
return CT_Choice2Builder()
def a_parent():
return CT_ParentBuilder()
def a_zomChild():
return CT_ZomChildBuilder()
def a_zooChild():
return CT_ZooChildBuilder()
def an_oomChild():
return CT_OomChildBuilder()
def an_oooChild():
return CT_OooChildBuilder()