tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / pyuno / qa / pytests / testcollections_XIndexAccess.py
blob6dd85e39af746429e0438a9fbf6b734d3ae2b5f0
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
12 from inspect import isclass
13 from testcollections_base import CollectionsTestBase
16 # Footnote instance factory
17 def getFootnoteInstance(doc):
18 return doc.createInstance("com.sun.star.text.Footnote")
20 # Tests behaviour of objects implementing XIndexAccess 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 TestXIndexAccess(CollectionsTestBase):
27 def insertTestFootnotes(self, doc, count):
28 cursor = doc.Text.createTextCursor()
29 for i in range(count):
30 footnote = getFootnoteInstance(doc)
31 footnote.Label = 'n'+str(i)
32 doc.Text.insertTextContent(cursor, footnote, 0)
34 def readValuesTestFixture(self, doc, count, key, expected):
35 # Given
36 doc.Text.setString('')
37 self.insertTestFootnotes(doc, count)
39 # When
40 captured = None
41 try:
42 actual = doc.Footnotes[key]
43 except Exception as e:
44 captured = e
46 # Then
47 if isclass(expected) and issubclass(expected, Exception):
48 # expected is exception
49 self.assertNotEqual(None, captured)
50 self.assertEqual(expected.__name__, type(captured).__name__)
51 elif type(expected) is tuple:
52 # expected is tuple
53 self.assertEqual(None, captured)
54 self.assertTrue(type(actual) is tuple)
55 self.assertEqual(len(expected), len(actual))
56 for i in range(len(expected)):
57 self.assertEqual('n'+str(expected[i]), actual[i].Label)
58 else:
59 # expected is value
60 self.assertEqual(None, captured)
61 self.assertTrue(type(actual) is not tuple)
62 self.assertEqual('n'+str(expected), actual.Label)
64 # Tests syntax:
65 # num = len(obj) # Number of elements
66 # For:
67 # length = 0
68 def test_XIndexAccess_Len_0(self):
69 # Given
70 doc = self.createBlankTextDocument()
72 # When
73 count = len(doc.Footnotes)
75 # Then
76 self.assertEqual(0, count)
78 doc.close(True)
80 # Tests syntax:
81 # num = len(obj) # Number of elements
82 # For:
83 # length = 1
84 def test_XIndexAccess_Len_1(self):
85 # Given
86 doc = self.createBlankTextDocument()
87 cursor = doc.Text.createTextCursor()
88 footnote = getFootnoteInstance(doc)
89 doc.Text.insertTextContent(cursor, footnote, 0)
91 # When
92 count = len(doc.Footnotes)
94 # Then
95 self.assertEqual(1, count)
97 doc.close(True)
99 # Tests syntax:
100 # val = obj[0] # Access by index
101 # For:
102 # Single indices
103 def test_XIndexAccess_ReadIndex_Single(self):
104 doc = self.createBlankTextDocument()
105 self.readValuesTestFixture(doc, 0, -1, IndexError)
106 self.readValuesTestFixture(doc, 0, 0, IndexError)
107 self.readValuesTestFixture(doc, 0, 1, IndexError)
108 self.readValuesTestFixture(doc, 1, -3, IndexError)
109 self.readValuesTestFixture(doc, 1, -2, IndexError)
110 self.readValuesTestFixture(doc, 1, -1, 0)
111 self.readValuesTestFixture(doc, 1, 0, 0)
112 self.readValuesTestFixture(doc, 1, 1, IndexError)
113 self.readValuesTestFixture(doc, 1, 2, IndexError)
114 self.readValuesTestFixture(doc, 2, -4, IndexError)
115 self.readValuesTestFixture(doc, 2, -3, IndexError)
116 self.readValuesTestFixture(doc, 2, -2, 0)
117 self.readValuesTestFixture(doc, 2, -1, 1)
118 self.readValuesTestFixture(doc, 2, 0, 0)
119 self.readValuesTestFixture(doc, 2, 1, 1)
120 self.readValuesTestFixture(doc, 2, 2, IndexError)
121 self.readValuesTestFixture(doc, 2, 3, IndexError)
122 doc.close(True)
124 def test_XIndexAccess_ReadIndex_Single_Invalid(self):
125 doc = self.createBlankTextDocument()
126 self.readValuesTestFixture(doc, 0, None, TypeError)
127 self.readValuesTestFixture(doc, 0, 'foo', TypeError)
128 self.readValuesTestFixture(doc, 0, 12.34, TypeError)
129 self.readValuesTestFixture(doc, 0, (0, 1), TypeError)
130 self.readValuesTestFixture(doc, 0, [0, 1], TypeError)
131 self.readValuesTestFixture(doc, 0, {'a': 'b'}, TypeError)
132 doc.close(True)
134 # Tests syntax:
135 # val1,val2 = obj[2:4] # Access by slice
136 def test_XIndexAccess_ReadSlice(self):
137 doc = self.createBlankTextDocument()
138 test_max = 4
139 for i in range(test_max):
140 t = tuple(range(i))
141 for j in [x for x in range(-test_max-2, test_max+3)] + [None]:
142 for k in [x for x in range(-test_max-2, test_max+3)] + [None]:
143 key = slice(j, k)
144 expected = t[key]
145 self.readValuesTestFixture(doc, i, key, expected)
146 doc.close(True)
148 # Tests syntax:
149 # val1,val2 = obj[0:3:2] # Access by extended slice
150 def test_XIndexAccess_ReadExtendedSlice(self):
151 doc = self.createBlankTextDocument()
152 test_max = 4
153 for i in range(test_max):
154 t = tuple(range(i))
155 for j in [x for x in range(-test_max-2, test_max+3)] + [None]:
156 for k in [x for x in range(-test_max-2, test_max+3)] + [None]:
157 for l in [-2, -1, 2]:
158 key = slice(j, k, l)
159 expected = t[key]
160 self.readValuesTestFixture(doc, i, key, expected)
161 doc.close(True)
163 # Tests syntax:
164 # if val in obj: ... # Test value presence
165 # For:
166 # Present element
167 def test_XIndexAccess_In_Present(self):
168 # Given
169 doc = self.createBlankTextDocument()
170 cursor = doc.Text.createTextCursor()
171 footnote = getFootnoteInstance(doc)
172 footnote.setLabel('foo')
173 doc.Text.insertTextContent(cursor, footnote, 0)
174 footnote = doc.Footnotes[0]
176 # When
177 present = footnote in doc.Footnotes
179 # Then
180 self.assertTrue(present)
182 doc.close(True)
184 # Tests syntax:
185 # if val in obj: ... # Test value presence
186 # For:
187 # None
188 def test_XIndexAccess_In_None(self):
189 # Given
190 doc = self.createBlankTextDocument()
192 # When
193 present = None in doc.Footnotes
195 # Then
196 self.assertFalse(present)
198 doc.close(True)
200 # Tests syntax:
201 # if val in obj: ... # Test value presence
202 # For:
203 # Absent element (string)
204 def test_XIndexAccess_In_String(self):
205 # Given
206 doc = self.createBlankTextDocument()
208 # When / Then
209 present = "foo" in doc.Footnotes
211 # Then
212 self.assertFalse(present)
214 doc.close(True)
216 # Tests syntax:
217 # if val in obj: ... # Test value presence
218 # For:
219 # Absent element (dict)
220 def test_XIndexAccess_In_Dict(self):
221 # Given
222 doc = self.createBlankTextDocument()
224 # When / Then
225 with self.assertRaises(TypeError):
226 _ = {} in doc.Footnotes
228 doc.close(True)
230 # Tests syntax:
231 # for val in obj: ... # Implicit iterator (values)
232 # For:
233 # 0 elements
234 def test_XIndexAccess_ForIn_0(self):
235 # Given
236 doc = self.createBlankTextDocument()
238 # When
239 read_footnotes = []
240 for f in doc.Footnotes:
241 read_footnotes.append(f)
243 # Then
244 self.assertEqual(0, len(read_footnotes))
246 doc.close(True)
248 # Tests syntax:
249 # for val in obj: ... # Implicit iterator (values)
250 # For:
251 # 1 element
252 def test_XIndexAccess_ForIn_1(self):
253 # Given
254 doc = self.createBlankTextDocument()
255 cursor = doc.Text.createTextCursor()
256 footnote = getFootnoteInstance(doc)
257 footnote.setLabel('foo')
258 doc.Text.insertTextContent(cursor, footnote, 0)
260 # When
261 read_footnotes = []
262 for f in doc.Footnotes:
263 read_footnotes.append(f)
265 # Then
266 self.assertEqual(1, len(read_footnotes))
267 self.assertEqual('foo', read_footnotes[0].Label)
269 doc.close(True)
271 # Tests syntax:
272 # for val in obj: ... # Implicit iterator (values)
273 # For:
274 # 2 elements
275 def test_XIndexAccess_ForIn_2(self):
276 # Given
277 doc = self.createBlankTextDocument()
278 cursor = doc.Text.createTextCursor()
279 footnote1 = getFootnoteInstance(doc)
280 footnote2 = getFootnoteInstance(doc)
281 footnote1.setLabel('foo')
282 footnote2.setLabel('bar')
283 doc.Text.insertTextContent(cursor, footnote1, 0)
284 doc.Text.insertTextContent(cursor, footnote2, 0)
286 # When
287 read_footnotes = []
288 for f in doc.Footnotes:
289 read_footnotes.append(f)
291 # Then
292 self.assertEqual(2, len(read_footnotes))
293 self.assertEqual('foo', read_footnotes[0].Label)
294 self.assertEqual('bar', read_footnotes[1].Label)
296 doc.close(True)
298 # Tests syntax:
299 # itr = iter(obj) # Named iterator (values)
300 # For:
301 # 1 element
302 def test_XIndexAccess_Iter_0(self):
303 # Given
304 doc = self.createBlankTextDocument()
305 cursor = doc.Text.createTextCursor()
306 footnote = getFootnoteInstance(doc)
307 footnote.setLabel('foo')
308 doc.Text.insertTextContent(cursor, footnote, 0)
310 # When
311 itr = iter(doc.Footnotes)
313 # Then
314 self.assertIsNotNone(next(itr))
315 with self.assertRaises(StopIteration):
316 next(itr)
318 doc.close(True)
321 if __name__ == '__main__':
322 unittest.main()
324 # vim:set shiftwidth=4 softtabstop=4 expandtab: