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()
181 for j
in [x
for x
in range(-12, 13)] + [None]:
182 for k
in [x
for x
in range(-12, 13)] + [None]:
184 for l
in range(assign_max
):
185 assign
= [y
+100 for y
in range(l
)]
186 expected
= list(range(10))
188 expected
[key
] = assign
189 except Exception as e
:
191 if (len(expected
) != 10):
192 expected
= ValueError()
193 self
.assignValuesTestFixture(doc
, key
, assign
, expected
)
197 # obj[2:4] = val1,val2 # Replace by slice
199 # Invalid values (inconsistently value types in tuple)
200 def test_XIndexReplace_ReplaceSlice_Invalid_InconsistentTuple(self
):
202 doc
= self
.createBlankTextDocument()
203 index
= getContentIndexInstance(doc
)
206 with self
.assertRaises(TypeError):
207 index
.LevelParagraphStyles
[0:2] = (
215 # obj[0:3:2] = val1,val2 # Replace by extended slice
217 # Cases requiring sequence type coercion
218 def test_XIndexReplace_ReplaceExtendedSlice(self
):
220 doc
= self
.createBlankTextDocument()
222 for j
in [x
for x
in range(-12, 13)] + [None]:
223 for k
in [x
for x
in range(-12, 13)] + [None]:
224 for l
in [-2, -1, 2]:
226 for m
in range(assign_max
):
227 assign
= [y
+100 for y
in range(m
)]
228 expected
= list(range(10))
230 expected
[key
] = assign
231 except Exception as e
:
233 self
.assignValuesTestFixture(doc
, key
, assign
, expected
)
237 if __name__
== '__main__':
240 # vim:set shiftwidth=4 softtabstop=4 expandtab: