Use o3tl::convert in Math
[LibreOffice.git] / pyuno / qa / pytests / testcollections_XIndexReplace.py
blob78361cbcaaee8671ea5aa8a5e491e9bd2d44459a
1 #!/usr/bin/env python
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/.
10 import unittest
11 import uno
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
24 # tested interfaces
26 class TestXIndexReplace(CollectionsTestBase):
28 def generateTestContentIndex(self, doc):
29 index = getContentIndexInstance(doc)
30 for i in range(10):
31 styles = ('n'+str(i),)
32 uno.invoke(index.LevelParagraphStyles, "replaceByIndex", (i, uno.Any("[]string", styles)))
33 return index
35 def generateTestTuple(self, values):
36 properties = []
37 for i in values:
38 properties.append(('n'+str(i),),)
39 return tuple(properties)
41 def assignValuesTestFixture(self, doc, key, values, expected):
42 # Given
43 index = self.generateTestContentIndex(doc)
44 to_assign = self.generateTestTuple(values)
45 if not (isinstance(expected, Exception)):
46 toCompare = self.generateTestTuple(expected)
48 # When
49 captured = None
50 try:
51 index.LevelParagraphStyles[key] = to_assign
52 except Exception as e:
53 captured = e
55 # Then
56 if isinstance(expected, Exception):
57 # expected is exception
58 self.assertNotEqual(None, captured)
59 self.assertEqual(type(expected).__name__, type(captured).__name__)
60 else:
61 # expected is list
62 self.assertEqual(None, captured)
63 for i in range(10):
64 self.assertEqual(toCompare[i][0],
65 index.LevelParagraphStyles[i][0])
67 # Tests syntax:
68 # obj[0] = val # Replace by index
69 # For:
70 # Cases requiring sequence type coercion
71 def test_XIndexReplace_ReplaceIndex(self):
72 # Given
73 doc = self.createBlankTextDocument()
74 index = getContentIndexInstance(doc)
76 # When
77 index.LevelParagraphStyles[0] = ('Caption',)
79 # Then
80 self.assertEqual(('Caption',), index.LevelParagraphStyles[0])
82 doc.close(True)
84 # Tests syntax:
85 # obj[0] = val # Replace by index
86 # For:
87 # Invalid value (None)
88 def test_XIndexReplace_ReplaceIndex_Invalid_None(self):
89 # Given
90 doc = self.createBlankTextDocument()
91 index = getContentIndexInstance(doc)
93 # When / Then
94 with self.assertRaises(TypeError):
95 index.LevelParagraphStyles[0] = None
97 doc.close(True)
99 # Tests syntax:
100 # obj[0] = val # Replace by index
101 # For:
102 # Invalid value (String)
103 def test_XIndexReplace_ReplaceIndex_Invalid_String(self):
104 # Given
105 doc = self.createBlankTextDocument()
106 index = getContentIndexInstance(doc)
108 # When / Then
109 with self.assertRaises(TypeError):
110 index.LevelParagraphStyles[0] = 'foo'
112 doc.close(True)
114 # Tests syntax:
115 # obj[0] = val # Replace by index
116 # For:
117 # Invalid value (Float)
118 def test_XIndexReplace_ReplaceIndex_Invalid_Float(self):
119 # Given
120 doc = self.createBlankTextDocument()
121 index = getContentIndexInstance(doc)
123 # When / Then
124 with self.assertRaises(TypeError):
125 index.LevelParagraphStyles[0] = 12.34
127 doc.close(True)
129 # Tests syntax:
130 # obj[0] = val # Replace by index
131 # For:
132 # Invalid value (List)
133 def test_XIndexReplace_ReplaceIndex_Invalid_List(self):
134 # Given
135 doc = self.createBlankTextDocument()
136 index = getContentIndexInstance(doc)
138 # When / Then
139 with self.assertRaises(TypeError):
140 index.LevelParagraphStyles[0] = [0, 1]
142 doc.close(True)
144 # Tests syntax:
145 # obj[0] = val # Replace by index
146 # For:
147 # Invalid value (Dict)
148 def test_XIndexReplace_ReplaceIndex_Invalid_Dict(self):
149 # Given
150 doc = self.createBlankTextDocument()
151 index = getContentIndexInstance(doc)
153 # When / Then
154 with self.assertRaises(TypeError):
155 index.LevelParagraphStyles[0] = {'a': 'b'}
157 doc.close(True)
159 # Tests syntax:
160 # obj[0] = val # Replace by index
161 # For:
162 # Invalid value (inconsistently typed tuple)
163 def test_XIndexReplace_ReplaceIndex_Invalid_InconsistentTuple(self):
164 # Given
165 doc = self.createBlankTextDocument()
166 index = getContentIndexInstance(doc)
168 # When / Then
169 with self.assertRaises(TypeError):
170 index.LevelParagraphStyles[0] = ('Caption', ())
172 doc.close(True)
174 # Tests syntax:
175 # obj[2:4] = val1,val2 # Replace by slice
176 # For:
177 # Cases requiring sequence type coercion
178 def test_XIndexReplace_ReplaceSlice(self):
179 assign_max = 12
180 doc = self.createBlankTextDocument()
181 t = tuple(range(10))
182 for j in [x for x in range(-12, 13)] + [None]:
183 for k in [x for x in range(-12, 13)] + [None]:
184 key = slice(j, k)
185 for l in range(assign_max):
186 assign = [y+100 for y in range(l)]
187 expected = list(range(10))
188 try:
189 expected[key] = assign
190 except Exception as e:
191 expected = e
192 if (len(expected) != 10):
193 expected = ValueError()
194 self.assignValuesTestFixture(doc, key, assign, expected)
195 doc.close(True)
197 # Tests syntax:
198 # obj[2:4] = val1,val2 # Replace by slice
199 # For:
200 # Invalid values (inconsistently value types in tuple)
201 def test_XIndexReplace_ReplaceSlice_Invalid_InconsistentTuple(self):
202 # Given
203 doc = self.createBlankTextDocument()
204 index = getContentIndexInstance(doc)
206 # When / Then
207 with self.assertRaises(TypeError):
208 index.LevelParagraphStyles[0:2] = (
209 ('Caption',),
210 12.34
213 doc.close(True)
215 # Tests syntax:
216 # obj[0:3:2] = val1,val2 # Replace by extended slice
217 # For:
218 # Cases requiring sequence type coercion
219 def test_XIndexReplace_ReplaceExtendedSlice(self):
220 assign_max = 12
221 doc = self.createBlankTextDocument()
222 t = tuple(range(10))
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]:
226 key = slice(j, k, l)
227 for m in range(assign_max):
228 assign = [y+100 for y in range(m)]
229 expected = list(range(10))
230 try:
231 expected[key] = assign
232 except Exception as e:
233 expected = e
234 self.assignValuesTestFixture(doc, key, assign, expected)
235 doc.close(True)
238 if __name__ == '__main__':
239 unittest.main()
241 # vim:set shiftwidth=4 softtabstop=4 expandtab: