Skip to content

Commit c5d2223

Browse files
author
Steve Canny
committed
test: reorganize unitdata modules
No code changes.
1 parent 03e48de commit c5d2223

File tree

8 files changed

+276
-271
lines changed

8 files changed

+276
-271
lines changed

docx/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# -*- coding: utf-8 -*-
1+
# encoding: utf-8
22

33
from docx.api import Document # noqa
44

tests/oxml/test_parts.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
# -*- coding: utf-8 -*-
2-
#
3-
# test_parts.py
4-
#
5-
# Copyright (C) 2013 Steve Canny scanny@cisco.com
6-
#
7-
# This module is part of python-docx and is released under the MIT License:
8-
# http://www.opensource.org/licenses/mit-license.php
1+
# encoding: utf-8
92

10-
"""Test suite for the docx.oxml.parts module."""
3+
"""
4+
Test suite for the docx.oxml.parts module.
5+
"""
116

127
from docx.oxml.parts import CT_Body
138
from docx.oxml.text import CT_P
149

15-
from ..unitdata import a_body, a_document
10+
from .unitdata.parts import a_body, a_document
1611

1712

1813
class DescribeCT_Body(object):

tests/oxml/test_text.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
# -*- coding: utf-8 -*-
2-
#
3-
# test_text.py
4-
#
5-
# Copyright (C) 2013 Steve Canny scanny@cisco.com
6-
#
7-
# This module is part of python-docx and is released under the MIT License:
8-
# http://www.opensource.org/licenses/mit-license.php
1+
# encoding: utf-8
92

10-
"""Test suite for the docx.oxml.text module."""
3+
"""
4+
Test suite for the docx.oxml.text module.
5+
"""
116

127
from docx.oxml.text import CT_P, CT_PPr, CT_R, CT_Text
138

14-
from ..unitdata import a_p, a_pPr, a_t, an_r
9+
from .unitdata.text import a_p, a_pPr, a_t, an_r
1510

1611

1712
class DescribeCT_P(object):

tests/oxml/unitdata/__init__.py

Whitespace-only changes.

tests/oxml/unitdata/parts.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Test data builders for parts XML.
5+
"""
6+
7+
from ...unitdata import BaseBuilder, nsdecls
8+
from .text import a_p, a_sectPr
9+
10+
11+
class CT_BodyBuilder(BaseBuilder):
12+
"""
13+
Test data builder for CT_Body (<w:body>) XML element that appears in
14+
document.xml files.
15+
"""
16+
def __init__(self):
17+
"""Establish instance variables with default values"""
18+
super(CT_BodyBuilder, self).__init__()
19+
self._p = None
20+
self._sectPr = None
21+
22+
@property
23+
def is_empty(self):
24+
return self._p is None and self._sectPr is None
25+
26+
def with_p(self):
27+
"""Add an empty paragraph element"""
28+
self._p = a_p()
29+
return self
30+
31+
def with_sectPr(self):
32+
"""Add an empty section properties element"""
33+
self._sectPr = a_sectPr()
34+
return self
35+
36+
@property
37+
def xml(self):
38+
"""Return element XML based on attribute settings"""
39+
indent = ' ' * self._indent
40+
if self.is_empty:
41+
xml = '%s<w:body %s/>\n' % (indent, nsdecls('w'))
42+
else:
43+
xml = '%s<w:body %s>\n' % (indent, nsdecls('w'))
44+
if self._p:
45+
xml += self._p.with_indent(self._indent+2).xml
46+
if self._sectPr:
47+
xml += self._sectPr.with_indent(self._indent+2).xml
48+
xml += '%s</w:body>\n' % indent
49+
return xml
50+
51+
52+
class CT_DocumentBuilder(BaseBuilder):
53+
"""
54+
XML data builder for CT_Document (<w:document>) element, the root element
55+
in document.xml files.
56+
"""
57+
def __init__(self):
58+
"""Establish instance variables with default values"""
59+
super(CT_DocumentBuilder, self).__init__()
60+
self._body = None
61+
62+
def with_body(self):
63+
"""Add an empty body element"""
64+
self._body = a_body().with_indent(2)
65+
return self
66+
67+
@property
68+
def xml(self):
69+
"""
70+
Return XML string based on settings accumulated via method calls.
71+
"""
72+
if not self._body:
73+
return '<w:document %s/>\n' % nsdecls('w')
74+
75+
xml = '<w:document %s>\n' % nsdecls('w')
76+
xml += self._body.xml
77+
xml += '</w:document>\n'
78+
return xml
79+
80+
81+
def a_body():
82+
"""Return a CT_BodyBuilder instance"""
83+
return CT_BodyBuilder()
84+
85+
86+
def a_document():
87+
"""Return a CT_DocumentBuilder instance"""
88+
return CT_DocumentBuilder()

tests/oxml/unitdata/text.py

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Test data builders for text XML elements
5+
"""
6+
7+
from ...unitdata import BaseBuilder
8+
9+
10+
class CT_PBuilder(BaseBuilder):
11+
"""
12+
Test data builder for a CT_P (<w:p>) XML element that appears within the
13+
body element of a document.xml file.
14+
"""
15+
def __init__(self):
16+
"""Establish instance variables with default values"""
17+
super(CT_PBuilder, self).__init__()
18+
self._pPr = None
19+
self._r = []
20+
21+
@property
22+
def is_empty(self):
23+
return self._pPr is None and len(self._r) == 0
24+
25+
def with_pPr(self, pPr=None):
26+
"""Add a <w:pPr> child element"""
27+
if pPr is None:
28+
pPr = a_pPr()
29+
self._pPr = pPr
30+
return self
31+
32+
def with_r(self, count=1):
33+
"""Add *count* empty run elements"""
34+
for i in range(count):
35+
self._r.append(an_r())
36+
return self
37+
38+
@property
39+
def xml(self):
40+
"""Return element XML based on attribute settings"""
41+
indent = ' ' * self._indent
42+
if self.is_empty:
43+
xml = '%s<w:p%s/>\n' % (indent, self._nsdecls)
44+
else:
45+
xml = '%s<w:p%s>\n' % (indent, self._nsdecls)
46+
if self._pPr:
47+
xml += self._pPr.with_indent(self._indent+2).xml
48+
for r in self._r:
49+
xml += r.with_indent(self._indent+2).xml
50+
xml += '%s</w:p>\n' % indent
51+
return xml
52+
53+
54+
class CT_PPrBuilder(BaseBuilder):
55+
"""
56+
Test data builder for a CT_PPr (<w:pPr>) XML element that appears as a
57+
child of a <w:p> element in a document.xml file.
58+
"""
59+
def __init__(self):
60+
"""Establish instance variables with default values"""
61+
super(CT_PPrBuilder, self).__init__()
62+
self._pStyle = None
63+
64+
@property
65+
def is_empty(self):
66+
return self._pStyle is None
67+
68+
def with_style(self, style='foobar'):
69+
"""Add pStyle child with inner text *style*"""
70+
self._pStyle = '<w:pStyle w:val="%s"/>' % style
71+
return self
72+
73+
@property
74+
def xml(self):
75+
"""Return element XML based on attribute settings"""
76+
indent = ' ' * self._indent
77+
if self.is_empty:
78+
xml = '%s<w:pPr%s/>\n' % (indent, self._nsdecls)
79+
else:
80+
xml = '%s<w:pPr%s>\n' % (indent, self._nsdecls)
81+
if self._pStyle:
82+
xml += '%s%s\n' % (indent+' ', self._pStyle)
83+
xml += '%s</w:pPr>\n' % indent
84+
return xml
85+
86+
87+
class CT_RBuilder(BaseBuilder):
88+
"""
89+
Test data builder for a CT_R (<w:r>) XML element that appears within the
90+
body element of a document.xml file.
91+
"""
92+
def __init__(self):
93+
"""Establish instance variables with default values"""
94+
super(CT_RBuilder, self).__init__()
95+
self._t = []
96+
97+
@property
98+
def is_empty(self):
99+
return len(self._t) == 0
100+
101+
def with_t(self, text):
102+
"""Add an text element containing *text*"""
103+
self._t.append(a_t(text))
104+
return self
105+
106+
@property
107+
def xml(self):
108+
"""Return element XML based on attribute settings"""
109+
indent = ' ' * self._indent
110+
if self.is_empty:
111+
xml = '%s<w:r%s/>\n' % (indent, self._nsdecls)
112+
else:
113+
xml = '%s<w:r%s>\n' % (indent, self._nsdecls)
114+
for t_builder in self._t:
115+
xml += t_builder.with_indent(self._indent+2).xml
116+
xml += '%s</w:r>\n' % indent
117+
return xml
118+
119+
120+
class CT_TextBuilder(BaseBuilder):
121+
"""
122+
Test data builder for a CT_Text (<w:t>) XML element that appears within a
123+
run element.
124+
"""
125+
def __init__(self, text):
126+
"""Establish instance variables with default values"""
127+
super(CT_TextBuilder, self).__init__()
128+
self._text = text
129+
130+
@property
131+
def xml(self):
132+
"""Return element XML based on attribute settings"""
133+
indent = ' ' * self._indent
134+
return '%s<w:t%s>%s</w:t>\n' % (indent, self._nsdecls, self._text)
135+
136+
137+
class CT_SectPrBuilder(BaseBuilder):
138+
"""
139+
Test data builder for a CT_SectPr (<w:sectPr>) XML element that appears
140+
within the body element of a document.xml file.
141+
"""
142+
def __init__(self):
143+
"""Establish instance variables with default values"""
144+
super(CT_SectPrBuilder, self).__init__()
145+
146+
@property
147+
def xml(self):
148+
"""Return element XML based on attribute settings"""
149+
tmpl = '%s<w:sectPr/>\n'
150+
indent = ' ' * self._indent
151+
return tmpl % (indent)
152+
153+
154+
def a_p():
155+
"""Return a CT_PBuilder instance"""
156+
return CT_PBuilder()
157+
158+
159+
def a_pPr():
160+
"""Return a CT_PPrBuilder instance"""
161+
return CT_PPrBuilder()
162+
163+
164+
def a_t(text):
165+
"""Return a CT_TextBuilder instance"""
166+
return CT_TextBuilder(text)
167+
168+
169+
def a_sectPr():
170+
"""Return a CT_SectPr instance"""
171+
return CT_SectPrBuilder()
172+
173+
174+
def an_r():
175+
"""Return a CT_RBuilder instance"""
176+
return CT_RBuilder()

tests/test_parts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from mock import call, Mock
1212

13-
from .unitdata import a_body
13+
from .oxml.unitdata.parts import a_body
1414
from .unitutil import class_mock, function_mock, initializer_mock
1515

1616

0 commit comments

Comments
 (0)