cid#1607171 Data race condition
[LibreOffice.git] / pyuno / qa / pytests / testcollections_XIndexContainer.py
blob45cf509889a40221faf0c419f486c07fa774d529
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
16 # DataForm factory
17 def getDataFormInstance(doc):
18 return doc.createInstance("com.sun.star.form.component.DataForm")
21 # Tests behaviour of objects implementing XIndexContainer 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 TestXIndexContainer(CollectionsTestBase):
28 def generateTestPropertyValues(self, count):
29 sm = self.context.ServiceManager
30 values = sm.createInstanceWithContext("com.sun.star.document.IndexedPropertyValues", self.context)
31 for i in range(count):
32 properties = (PropertyValue(Name='n'+str(i), Value='v'+str(i)),)
33 uno.invoke(values, "insertByIndex", (i, uno.Any("[]com.sun.star.beans.PropertyValue", properties)))
34 return values
36 def generateTestTuple(self, values):
37 properties = []
38 for i in values:
39 properties.append((PropertyValue(Name='n'+str(i), Value='v'+str(i)),))
40 return tuple(properties)
42 def assignValuesTestFixture(self, count, key, values, expected):
43 # Given
44 property_values = self.generateTestPropertyValues(count)
45 if type(values) is list:
46 to_assign = self.generateTestTuple(values)
47 else:
48 to_assign = values
49 if not (isinstance(expected, Exception)):
50 to_compare = self.generateTestTuple(expected)
52 # When
53 captured = None
54 try:
55 property_values[key] = to_assign
56 except Exception as e:
57 captured = e
59 # Then
60 if isinstance(expected, Exception):
61 # expected is exception
62 self.assertNotEqual(None, captured)
63 self.assertEqual(type(expected).__name__, type(captured).__name__)
64 else:
65 # expected is list
66 self.assertEqual(None, captured)
67 self.assertEqual(len(expected), len(property_values))
68 for i in range(len(property_values)):
69 self.assertEqual(to_compare[i][0].Name, property_values[i][0].Name)
71 def deleteValuesTestFixture(self, count, key, expected):
72 # Given
73 property_values = self.generateTestPropertyValues(count)
74 if not (isinstance(expected, Exception)):
75 to_compare = self.generateTestTuple(expected)
77 # When
78 captured = None
79 try:
80 del property_values[key]
81 except Exception as e:
82 captured = e
84 # Then
85 if isinstance(expected, Exception):
86 # expected is exception
87 self.assertNotEqual(None, captured)
88 self.assertEqual(type(expected).__name__, type(captured).__name__)
89 else:
90 # expected is list
91 self.assertEqual(None, captured)
92 self.assertEqual(len(expected), len(property_values))
93 for i in range(len(property_values)):
94 self.assertEqual(to_compare[i][0].Name, property_values[i][0].Name)
96 # Tests syntax:
97 # obj[2:4] = val1,val2 # Replace by slice
98 # obj[2:3] = val1,val2 # Insert/replace by slice
99 # obj[2:2] = (val,) # Insert by slice
100 # obj[2:4] = (val,) # Replace/delete by slice
101 # obj[2:3] = () # Delete by slice (implicit)
102 # For:
103 # Cases requiring sequence type coercion
104 def test_XIndexContainer_AssignSlice(self):
105 base_max = 5
106 assign_max = 5
107 for i in range(base_max):
108 for j in [x for x in range(-base_max-2, base_max+3)] + [None]:
109 for k in [x for x in range(-base_max-2, base_max+3)] + [None]:
110 key = slice(j, k)
111 for l in range(assign_max):
112 assign = [y+100 for y in range(l)]
113 expected = list(range(i))
114 expected[key] = assign
115 self.assignValuesTestFixture(i, key, assign, expected)
117 # Tests syntax:
118 # obj[2:4] = val1,val2 # Replace by slice
119 # obj[2:3] = val1,val2 # Insert/replace by slice
120 # obj[2:2] = (val,) # Insert by slice
121 # For:
122 # Cases not requiring sequence type coercion
123 # Invalid values
124 def test_XIndexContainer_AssignSlice_Invalid(self):
125 self.assignValuesTestFixture(2, slice(0, 2), None, TypeError())
126 self.assignValuesTestFixture(2, slice(0, 2), 'foo', TypeError())
127 self.assignValuesTestFixture(2, slice(0, 2), 12.34, TypeError())
128 self.assignValuesTestFixture(2, slice(0, 2), {'a': 'b'}, TypeError())
129 self.assignValuesTestFixture(2, slice(0, 2), ('foo',), TypeError())
130 self.assignValuesTestFixture(2, slice(0, 2), ('foo', 'foo'), TypeError())
132 # Tests syntax:
133 # obj[2:2] = (val,) # Insert by slice
134 # For:
135 # Cases not requiring sequence type coercion
136 def test_XIndexContainer_AssignSlice_NoCoercion(self):
137 # Given
138 doc = self.createBlankTextDocument()
139 form = getDataFormInstance(doc)
140 form.Name = 'foo'
142 # When
143 doc.DrawPage.Forms[0:0] = (form,)
145 # Then
146 self.assertEqual('foo', doc.DrawPage.Forms[0].Name)
148 doc.close(True)
150 # Tests syntax:
151 # obj[0:3:2] = val1,val2 # Replace by extended slice
152 # For:
153 # Cases requiring sequence type coercion
154 def test_XIndexContainer_AssignExtendedSlice(self):
155 base_max = 5
156 assign_max = 5
157 for i in range(base_max):
158 for j in [x for x in range(-base_max-2, base_max+3)] + [None]:
159 for k in [x for x in range(-base_max-2, base_max+3)] + [None]:
160 for l in [-2, -1, 1, 2]:
161 key = slice(j, k, l)
162 for m in range(assign_max):
163 assign = [y+100 for y in range(m)]
164 expected = list(range(i))
165 try:
166 expected[key] = assign
167 except Exception as e:
168 expected = e
170 self.assignValuesTestFixture(i, key, assign, expected)
172 # Tests syntax:
173 # del obj[0] # Delete by index
174 def test_XIndexContainer_DelIndex(self):
175 base_max = 5
176 for i in range(base_max):
177 for j in [x for x in range(-base_max-2, base_max+3)]:
178 expected = list(range(i))
179 try:
180 del expected[j]
181 except Exception as e:
182 expected = e
183 self.deleteValuesTestFixture(i, j, expected)
185 # Tests syntax:
186 # del obj[2:4] # Delete by slice
187 def test_XIndexContainer_DelSlice(self):
188 baseMax = 5
189 for i in range(baseMax):
190 for j in [x for x in range(-baseMax-2, baseMax+3)] + [None]:
191 for k in [x for x in range(-baseMax-2, baseMax+3)] + [None]:
192 key = slice(j, k)
193 expected = list(range(i))
194 try:
195 del expected[key]
196 except Exception as e:
197 expected = e
198 self.deleteValuesTestFixture(i, key, expected)
201 if __name__ == '__main__':
202 unittest.main()
204 # vim:set shiftwidth=4 softtabstop=4 expandtab: