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/.
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
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
):
36 doc
.Text
.setString('')
37 self
.insertTestFootnotes(doc
, count
)
42 actual
= doc
.Footnotes
[key
]
43 except Exception as e
:
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:
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
)
60 self
.assertEqual(None, captured
)
61 self
.assertTrue(type(actual
) is not tuple)
62 self
.assertEqual('n'+str(expected
), actual
.Label
)
65 # num = len(obj) # Number of elements
68 def test_XIndexAccess_Len_0(self
):
70 doc
= self
.createBlankTextDocument()
73 count
= len(doc
.Footnotes
)
76 self
.assertEqual(0, count
)
81 # num = len(obj) # Number of elements
84 def test_XIndexAccess_Len_1(self
):
86 doc
= self
.createBlankTextDocument()
87 cursor
= doc
.Text
.createTextCursor()
88 footnote
= getFootnoteInstance(doc
)
89 doc
.Text
.insertTextContent(cursor
, footnote
, 0)
92 count
= len(doc
.Footnotes
)
95 self
.assertEqual(1, count
)
100 # val = obj[0] # Access by index
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)
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)
135 # val1,val2 = obj[2:4] # Access by slice
136 def test_XIndexAccess_ReadSlice(self
):
137 doc
= self
.createBlankTextDocument()
139 for i
in range(test_max
):
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]:
145 self
.readValuesTestFixture(doc
, i
, key
, expected
)
149 # val1,val2 = obj[0:3:2] # Access by extended slice
150 def test_XIndexAccess_ReadExtendedSlice(self
):
151 doc
= self
.createBlankTextDocument()
153 for i
in range(test_max
):
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]:
160 self
.readValuesTestFixture(doc
, i
, key
, expected
)
164 # if val in obj: ... # Test value presence
167 def test_XIndexAccess_In_Present(self
):
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]
177 present
= footnote
in doc
.Footnotes
180 self
.assertTrue(present
)
185 # if val in obj: ... # Test value presence
188 def test_XIndexAccess_In_None(self
):
190 doc
= self
.createBlankTextDocument()
193 present
= None in doc
.Footnotes
196 self
.assertFalse(present
)
201 # if val in obj: ... # Test value presence
203 # Absent element (string)
204 def test_XIndexAccess_In_String(self
):
206 doc
= self
.createBlankTextDocument()
209 present
= "foo" in doc
.Footnotes
212 self
.assertFalse(present
)
217 # if val in obj: ... # Test value presence
219 # Absent element (dict)
220 def test_XIndexAccess_In_Dict(self
):
222 doc
= self
.createBlankTextDocument()
225 with self
.assertRaises(TypeError):
226 _
= {} in doc
.Footnotes
231 # for val in obj: ... # Implicit iterator (values)
234 def test_XIndexAccess_ForIn_0(self
):
236 doc
= self
.createBlankTextDocument()
240 for f
in doc
.Footnotes
:
241 read_footnotes
.append(f
)
244 self
.assertEqual(0, len(read_footnotes
))
249 # for val in obj: ... # Implicit iterator (values)
252 def test_XIndexAccess_ForIn_1(self
):
254 doc
= self
.createBlankTextDocument()
255 cursor
= doc
.Text
.createTextCursor()
256 footnote
= getFootnoteInstance(doc
)
257 footnote
.setLabel('foo')
258 doc
.Text
.insertTextContent(cursor
, footnote
, 0)
262 for f
in doc
.Footnotes
:
263 read_footnotes
.append(f
)
266 self
.assertEqual(1, len(read_footnotes
))
267 self
.assertEqual('foo', read_footnotes
[0].Label
)
272 # for val in obj: ... # Implicit iterator (values)
275 def test_XIndexAccess_ForIn_2(self
):
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)
288 for f
in doc
.Footnotes
:
289 read_footnotes
.append(f
)
292 self
.assertEqual(2, len(read_footnotes
))
293 self
.assertEqual('foo', read_footnotes
[0].Label
)
294 self
.assertEqual('bar', read_footnotes
[1].Label
)
299 # itr = iter(obj) # Named iterator (values)
302 def test_XIndexAccess_Iter_0(self
):
304 doc
= self
.createBlankTextDocument()
305 cursor
= doc
.Text
.createTextCursor()
306 footnote
= getFootnoteInstance(doc
)
307 footnote
.setLabel('foo')
308 doc
.Text
.insertTextContent(cursor
, footnote
, 0)
311 itr
= iter(doc
.Footnotes
)
314 self
.assertIsNotNone(next(itr
))
315 with self
.assertRaises(StopIteration):
321 if __name__
== '__main__':
324 # vim:set shiftwidth=4 softtabstop=4 expandtab: