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
16 # ContentIndex instance factory
17 def getContentIndexInstance(doc
):
18 return doc
.createInstance("com.sun.star.text.ContentIndex")
20 # Tests behaviour of objects implementing XIndexReplace using the new-style
21 # collection accessors
22 # The objects chosen have no special meaning, they just happen to implement the
25 class TestXIndexReplace(CollectionsTestBase
):
27 def generateTestContentIndex(self
, doc
):
28 index
= getContentIndexInstance(doc
)
30 styles
= ('n'+str(i
),)
31 uno
.invoke(index
.LevelParagraphStyles
, "replaceByIndex", (i
, uno
.Any("[]string", styles
)))
34 def generateTestTuple(self
, values
):
37 properties
.append(('n'+str(i
),),)
38 return tuple(properties
)
40 def assignValuesTestFixture(self
, doc
, key
, values
, expected
):
42 index
= self
.generateTestContentIndex(doc
)
43 to_assign
= self
.generateTestTuple(values
)
44 if not (isinstance(expected
, Exception)):
45 toCompare
= self
.generateTestTuple(expected
)
50 index
.LevelParagraphStyles
[key
] = to_assign
51 except Exception as e
:
55 if isinstance(expected
, Exception):
56 # expected is exception
57 self
.assertNotEqual(None, captured
)
58 self
.assertEqual(type(expected
).__name
__, type(captured
).__name
__)
61 self
.assertEqual(None, captured
)
63 self
.assertEqual(toCompare
[i
][0],
64 index
.LevelParagraphStyles
[i
][0])
67 # obj[0] = val # Replace by index
69 # Cases requiring sequence type coercion
70 def test_XIndexReplace_ReplaceIndex(self
):
72 doc
= self
.createBlankTextDocument()
73 index
= getContentIndexInstance(doc
)
76 index
.LevelParagraphStyles
[0] = ('Caption',)
79 self
.assertEqual(('Caption',), index
.LevelParagraphStyles
[0])
84 # obj[0] = val # Replace by index
86 # Invalid value (None)
87 def test_XIndexReplace_ReplaceIndex_Invalid_None(self
):
89 doc
= self
.createBlankTextDocument()
90 index
= getContentIndexInstance(doc
)
93 with self
.assertRaises(TypeError):
94 index
.LevelParagraphStyles
[0] = None
99 # obj[0] = val # Replace by index
101 # Invalid value (String)
102 def test_XIndexReplace_ReplaceIndex_Invalid_String(self
):
104 doc
= self
.createBlankTextDocument()
105 index
= getContentIndexInstance(doc
)
108 with self
.assertRaises(TypeError):
109 index
.LevelParagraphStyles
[0] = 'foo'
114 # obj[0] = val # Replace by index
116 # Invalid value (Float)
117 def test_XIndexReplace_ReplaceIndex_Invalid_Float(self
):
119 doc
= self
.createBlankTextDocument()
120 index
= getContentIndexInstance(doc
)
123 with self
.assertRaises(TypeError):
124 index
.LevelParagraphStyles
[0] = 12.34
129 # obj[0] = val # Replace by index
131 # Invalid value (List)
132 def test_XIndexReplace_ReplaceIndex_Invalid_List(self
):
134 doc
= self
.createBlankTextDocument()
135 index
= getContentIndexInstance(doc
)
138 with self
.assertRaises(TypeError):
139 index
.LevelParagraphStyles
[0] = [0, 1]
144 # obj[0] = val # Replace by index
146 # Invalid value (Dict)
147 def test_XIndexReplace_ReplaceIndex_Invalid_Dict(self
):
149 doc
= self
.createBlankTextDocument()
150 index
= getContentIndexInstance(doc
)
153 with self
.assertRaises(TypeError):
154 index
.LevelParagraphStyles
[0] = {'a': 'b'}
159 # obj[0] = val # Replace by index
161 # Invalid value (inconsistently typed tuple)
162 def test_XIndexReplace_ReplaceIndex_Invalid_InconsistentTuple(self
):
164 doc
= self
.createBlankTextDocument()
165 index
= getContentIndexInstance(doc
)
168 with self
.assertRaises(TypeError):
169 index
.LevelParagraphStyles
[0] = ('Caption', ())
174 # obj[2:4] = val1,val2 # Replace by slice
176 # Cases requiring sequence type coercion
177 def test_XIndexReplace_ReplaceSlice(self
):
179 doc
= self
.createBlankTextDocument()
180 for j
in [x
for x
in range(-12, 13)] + [None]:
181 for k
in [x
for x
in range(-12, 13)] + [None]:
183 for l
in range(assign_max
):
184 assign
= [y
+100 for y
in range(l
)]
185 expected
= list(range(10))
187 expected
[key
] = assign
188 except Exception as e
:
190 if (len(expected
) != 10):
191 expected
= ValueError()
192 self
.assignValuesTestFixture(doc
, key
, assign
, expected
)
196 # obj[2:4] = val1,val2 # Replace by slice
198 # Invalid values (inconsistently value types in tuple)
199 def test_XIndexReplace_ReplaceSlice_Invalid_InconsistentTuple(self
):
201 doc
= self
.createBlankTextDocument()
202 index
= getContentIndexInstance(doc
)
205 with self
.assertRaises(TypeError):
206 index
.LevelParagraphStyles
[0:2] = (
214 # obj[0:3:2] = val1,val2 # Replace by extended slice
216 # Cases requiring sequence type coercion
217 def test_XIndexReplace_ReplaceExtendedSlice(self
):
219 doc
= self
.createBlankTextDocument()
220 for j
in [x
for x
in range(-12, 13)] + [None]:
221 for k
in [x
for x
in range(-12, 13)] + [None]:
222 for l
in [-2, -1, 2]:
224 for m
in range(assign_max
):
225 assign
= [y
+100 for y
in range(m
)]
226 expected
= list(range(10))
228 expected
[key
] = assign
229 except Exception as e
:
231 self
.assignValuesTestFixture(doc
, key
, assign
, expected
)
235 if __name__
== '__main__':
238 # vim:set shiftwidth=4 softtabstop=4 expandtab: