tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / pyuno / qa / pytests / testcollections_XIndexReplace.py
blob0075883543d315779af0a4c693fb1d13693473d1
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
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
23 # tested interfaces
25 class TestXIndexReplace(CollectionsTestBase):
27 def generateTestContentIndex(self, doc):
28 index = getContentIndexInstance(doc)
29 for i in range(10):
30 styles = ('n'+str(i),)
31 uno.invoke(index.LevelParagraphStyles, "replaceByIndex", (i, uno.Any("[]string", styles)))
32 return index
34 def generateTestTuple(self, values):
35 properties = []
36 for i in values:
37 properties.append(('n'+str(i),),)
38 return tuple(properties)
40 def assignValuesTestFixture(self, doc, key, values, expected):
41 # Given
42 index = self.generateTestContentIndex(doc)
43 to_assign = self.generateTestTuple(values)
44 if not (isinstance(expected, Exception)):
45 toCompare = self.generateTestTuple(expected)
47 # When
48 captured = None
49 try:
50 index.LevelParagraphStyles[key] = to_assign
51 except Exception as e:
52 captured = e
54 # Then
55 if isinstance(expected, Exception):
56 # expected is exception
57 self.assertNotEqual(None, captured)
58 self.assertEqual(type(expected).__name__, type(captured).__name__)
59 else:
60 # expected is list
61 self.assertEqual(None, captured)
62 for i in range(10):
63 self.assertEqual(toCompare[i][0],
64 index.LevelParagraphStyles[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 for j in [x for x in range(-12, 13)] + [None]:
181 for k in [x for x in range(-12, 13)] + [None]:
182 key = slice(j, k)
183 for l in range(assign_max):
184 assign = [y+100 for y in range(l)]
185 expected = list(range(10))
186 try:
187 expected[key] = assign
188 except Exception as e:
189 expected = e
190 if (len(expected) != 10):
191 expected = ValueError()
192 self.assignValuesTestFixture(doc, key, assign, expected)
193 doc.close(True)
195 # Tests syntax:
196 # obj[2:4] = val1,val2 # Replace by slice
197 # For:
198 # Invalid values (inconsistently value types in tuple)
199 def test_XIndexReplace_ReplaceSlice_Invalid_InconsistentTuple(self):
200 # Given
201 doc = self.createBlankTextDocument()
202 index = getContentIndexInstance(doc)
204 # When / Then
205 with self.assertRaises(TypeError):
206 index.LevelParagraphStyles[0:2] = (
207 ('Caption',),
208 12.34
211 doc.close(True)
213 # Tests syntax:
214 # obj[0:3:2] = val1,val2 # Replace by extended slice
215 # For:
216 # Cases requiring sequence type coercion
217 def test_XIndexReplace_ReplaceExtendedSlice(self):
218 assign_max = 12
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]:
223 key = slice(j, k, l)
224 for m in range(assign_max):
225 assign = [y+100 for y in range(m)]
226 expected = list(range(10))
227 try:
228 expected[key] = assign
229 except Exception as e:
230 expected = e
231 self.assignValuesTestFixture(doc, key, assign, expected)
232 doc.close(True)
235 if __name__ == '__main__':
236 unittest.main()
238 # vim:set shiftwidth=4 softtabstop=4 expandtab: