tdf#125181 maxY is 50000 in prstGeom for star24 and star32
[LibreOffice.git] / pyuno / qa / pytests / testcollections_XIndexReplace.py
blobbbf424f0bdfbb7632de41c023202eaa0aabdc96c
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], index.LevelParagraphStyles.getByIndex(i)[0])
66 # Tests syntax:
67 # obj[0] = val # Replace by index
68 # For:
69 # Cases requiring sequence type coercion
70 def test_XIndexReplace_ReplaceIndex(self):
71 # Given
72 doc = self.createBlankTextDocument()
73 index = getContentIndexInstance(doc)
75 # When
76 index.LevelParagraphStyles[0] = ('Caption',)
78 # Then
79 self.assertEqual(('Caption',), index.LevelParagraphStyles[0])
81 doc.close(True)
83 # Tests syntax:
84 # obj[0] = val # Replace by index
85 # For:
86 # Invalid value (None)
87 def test_XIndexReplace_ReplaceIndex_Invalid_None(self):
88 # Given
89 doc = self.createBlankTextDocument()
90 index = getContentIndexInstance(doc)
92 # When / Then
93 with self.assertRaises(TypeError):
94 index.LevelParagraphStyles[0] = None
96 doc.close(True)
98 # Tests syntax:
99 # obj[0] = val # Replace by index
100 # For:
101 # Invalid value (String)
102 def test_XIndexReplace_ReplaceIndex_Invalid_String(self):
103 # Given
104 doc = self.createBlankTextDocument()
105 index = getContentIndexInstance(doc)
107 # When / Then
108 with self.assertRaises(TypeError):
109 index.LevelParagraphStyles[0] = 'foo'
111 doc.close(True)
113 # Tests syntax:
114 # obj[0] = val # Replace by index
115 # For:
116 # Invalid value (Float)
117 def test_XIndexReplace_ReplaceIndex_Invalid_Float(self):
118 # Given
119 doc = self.createBlankTextDocument()
120 index = getContentIndexInstance(doc)
122 # When / Then
123 with self.assertRaises(TypeError):
124 index.LevelParagraphStyles[0] = 12.34
126 doc.close(True)
128 # Tests syntax:
129 # obj[0] = val # Replace by index
130 # For:
131 # Invalid value (List)
132 def test_XIndexReplace_ReplaceIndex_Invalid_List(self):
133 # Given
134 doc = self.createBlankTextDocument()
135 index = getContentIndexInstance(doc)
137 # When / Then
138 with self.assertRaises(TypeError):
139 index.LevelParagraphStyles[0] = [0, 1]
141 doc.close(True)
143 # Tests syntax:
144 # obj[0] = val # Replace by index
145 # For:
146 # Invalid value (Dict)
147 def test_XIndexReplace_ReplaceIndex_Invalid_Dict(self):
148 # Given
149 doc = self.createBlankTextDocument()
150 index = getContentIndexInstance(doc)
152 # When / Then
153 with self.assertRaises(TypeError):
154 index.LevelParagraphStyles[0] = {'a': 'b'}
156 doc.close(True)
158 # Tests syntax:
159 # obj[0] = val # Replace by index
160 # For:
161 # Invalid value (inconsistently typed tuple)
162 def test_XIndexReplace_ReplaceIndex_Invalid_InconsistentTuple(self):
163 # Given
164 doc = self.createBlankTextDocument()
165 index = getContentIndexInstance(doc)
167 # When / Then
168 with self.assertRaises(TypeError):
169 index.LevelParagraphStyles[0] = ('Caption', ())
171 doc.close(True)
173 # Tests syntax:
174 # obj[2:4] = val1,val2 # Replace by slice
175 # For:
176 # Cases requiring sequence type coercion
177 def test_XIndexReplace_ReplaceSlice(self):
178 assign_max = 12
179 doc = self.createBlankTextDocument()
180 t = tuple(range(10))
181 for j in [x for x in range(-12, 13)] + [None]:
182 for k in [x for x in range(-12, 13)] + [None]:
183 key = slice(j, k)
184 for l in range(assign_max):
185 assign = [y+100 for y in range(l)]
186 expected = list(range(10))
187 try:
188 expected[key] = assign
189 except Exception as e:
190 expected = e
191 if (len(expected) != 10):
192 expected = ValueError()
193 self.assignValuesTestFixture(doc, key, assign, expected)
194 doc.close(True)
196 # Tests syntax:
197 # obj[2:4] = val1,val2 # Replace by slice
198 # For:
199 # Invalid values (inconsistently value types in tuple)
200 def test_XIndexReplace_ReplaceSlice_Invalid_InconsistentTuple(self):
201 # Given
202 doc = self.createBlankTextDocument()
203 index = getContentIndexInstance(doc)
205 # When / Then
206 with self.assertRaises(TypeError):
207 index.LevelParagraphStyles[0:2] = (
208 ('Caption',),
209 12.34
212 doc.close(True)
214 # Tests syntax:
215 # obj[0:3:2] = val1,val2 # Replace by extended slice
216 # For:
217 # Cases requiring sequence type coercion
218 def test_XIndexReplace_ReplaceExtendedSlice(self):
219 assign_max = 12
220 doc = self.createBlankTextDocument()
221 t = tuple(range(10))
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]:
225 key = slice(j, k, l)
226 for m in range(assign_max):
227 assign = [y+100 for y in range(m)]
228 expected = list(range(10))
229 try:
230 expected[key] = assign
231 except Exception as e:
232 expected = e
233 self.assignValuesTestFixture(doc, key, assign, expected)
234 doc.close(True)
237 if __name__ == '__main__':
238 unittest.main()
240 # vim:set shiftwidth=4 softtabstop=4 expandtab: