3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
13 from testcollections_base
import CollectionsTestBase
14 from com
.sun
.star
.beans
import PropertyValue
17 # ContentIndex instance factory
18 def getContentIndexInstance(doc
):
19 return doc
.createInstance("com.sun.star.text.ContentIndex")
21 # Tests behaviour of objects implementing XIndexReplace using the new-style
22 # collection accessors
23 # The objects chosen have no special meaning, they just happen to implement the
26 class TestXIndexReplace(CollectionsTestBase
):
28 def generateTestContentIndex(self
, doc
):
29 index
= getContentIndexInstance(doc
)
31 styles
= ('n'+str(i
),)
32 uno
.invoke(index
.LevelParagraphStyles
, "replaceByIndex", (i
, uno
.Any("[]string", styles
)))
35 def generateTestTuple(self
, values
):
38 properties
.append(('n'+str(i
),),)
39 return tuple(properties
)
41 def assignValuesTestFixture(self
, doc
, key
, values
, expected
):
43 index
= self
.generateTestContentIndex(doc
)
44 to_assign
= self
.generateTestTuple(values
)
45 if not (isinstance(expected
, Exception)):
46 toCompare
= self
.generateTestTuple(expected
)
51 index
.LevelParagraphStyles
[key
] = to_assign
52 except Exception as e
:
56 if isinstance(expected
, Exception):
57 # expected is exception
58 self
.assertNotEqual(None, captured
)
59 self
.assertEqual(type(expected
).__name
__, type(captured
).__name
__)
62 self
.assertEqual(None, captured
)
64 self
.assertEqual(toCompare
[i
][0],
65 index
.LevelParagraphStyles
[i
][0])
68 # obj[0] = val # Replace by index
70 # Cases requiring sequence type coercion
71 def test_XIndexReplace_ReplaceIndex(self
):
73 doc
= self
.createBlankTextDocument()
74 index
= getContentIndexInstance(doc
)
77 index
.LevelParagraphStyles
[0] = ('Caption',)
80 self
.assertEqual(('Caption',), index
.LevelParagraphStyles
[0])
85 # obj[0] = val # Replace by index
87 # Invalid value (None)
88 def test_XIndexReplace_ReplaceIndex_Invalid_None(self
):
90 doc
= self
.createBlankTextDocument()
91 index
= getContentIndexInstance(doc
)
94 with self
.assertRaises(TypeError):
95 index
.LevelParagraphStyles
[0] = None
100 # obj[0] = val # Replace by index
102 # Invalid value (String)
103 def test_XIndexReplace_ReplaceIndex_Invalid_String(self
):
105 doc
= self
.createBlankTextDocument()
106 index
= getContentIndexInstance(doc
)
109 with self
.assertRaises(TypeError):
110 index
.LevelParagraphStyles
[0] = 'foo'
115 # obj[0] = val # Replace by index
117 # Invalid value (Float)
118 def test_XIndexReplace_ReplaceIndex_Invalid_Float(self
):
120 doc
= self
.createBlankTextDocument()
121 index
= getContentIndexInstance(doc
)
124 with self
.assertRaises(TypeError):
125 index
.LevelParagraphStyles
[0] = 12.34
130 # obj[0] = val # Replace by index
132 # Invalid value (List)
133 def test_XIndexReplace_ReplaceIndex_Invalid_List(self
):
135 doc
= self
.createBlankTextDocument()
136 index
= getContentIndexInstance(doc
)
139 with self
.assertRaises(TypeError):
140 index
.LevelParagraphStyles
[0] = [0, 1]
145 # obj[0] = val # Replace by index
147 # Invalid value (Dict)
148 def test_XIndexReplace_ReplaceIndex_Invalid_Dict(self
):
150 doc
= self
.createBlankTextDocument()
151 index
= getContentIndexInstance(doc
)
154 with self
.assertRaises(TypeError):
155 index
.LevelParagraphStyles
[0] = {'a': 'b'}
160 # obj[0] = val # Replace by index
162 # Invalid value (inconsistently typed tuple)
163 def test_XIndexReplace_ReplaceIndex_Invalid_InconsistentTuple(self
):
165 doc
= self
.createBlankTextDocument()
166 index
= getContentIndexInstance(doc
)
169 with self
.assertRaises(TypeError):
170 index
.LevelParagraphStyles
[0] = ('Caption', ())
175 # obj[2:4] = val1,val2 # Replace by slice
177 # Cases requiring sequence type coercion
178 def test_XIndexReplace_ReplaceSlice(self
):
180 doc
= self
.createBlankTextDocument()
182 for j
in [x
for x
in range(-12, 13)] + [None]:
183 for k
in [x
for x
in range(-12, 13)] + [None]:
185 for l
in range(assign_max
):
186 assign
= [y
+100 for y
in range(l
)]
187 expected
= list(range(10))
189 expected
[key
] = assign
190 except Exception as e
:
192 if (len(expected
) != 10):
193 expected
= ValueError()
194 self
.assignValuesTestFixture(doc
, key
, assign
, expected
)
198 # obj[2:4] = val1,val2 # Replace by slice
200 # Invalid values (inconsistently value types in tuple)
201 def test_XIndexReplace_ReplaceSlice_Invalid_InconsistentTuple(self
):
203 doc
= self
.createBlankTextDocument()
204 index
= getContentIndexInstance(doc
)
207 with self
.assertRaises(TypeError):
208 index
.LevelParagraphStyles
[0:2] = (
216 # obj[0:3:2] = val1,val2 # Replace by extended slice
218 # Cases requiring sequence type coercion
219 def test_XIndexReplace_ReplaceExtendedSlice(self
):
221 doc
= self
.createBlankTextDocument()
223 for j
in [x
for x
in range(-12, 13)] + [None]:
224 for k
in [x
for x
in range(-12, 13)] + [None]:
225 for l
in [-2, -1, 2]:
227 for m
in range(assign_max
):
228 assign
= [y
+100 for y
in range(m
)]
229 expected
= list(range(10))
231 expected
[key
] = assign
232 except Exception as e
:
234 self
.assignValuesTestFixture(doc
, key
, assign
, expected
)
238 if __name__
== '__main__':
241 # vim:set shiftwidth=4 softtabstop=4 expandtab: