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/.
13 from testcollections_base
import CollectionsTestBase
14 from com
.sun
.star
.beans
import PropertyValue
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
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
)))
36 def generateTestTuple(self
, 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
):
44 property_values
= self
.generateTestPropertyValues(count
)
45 if type(values
) is list:
46 to_assign
= self
.generateTestTuple(values
)
49 if not (isinstance(expected
, Exception)):
50 to_compare
= self
.generateTestTuple(expected
)
55 property_values
[key
] = to_assign
56 except Exception as e
:
60 if isinstance(expected
, Exception):
61 # expected is exception
62 self
.assertNotEqual(None, captured
)
63 self
.assertEqual(type(expected
).__name
__, type(captured
).__name
__)
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
):
73 property_values
= self
.generateTestPropertyValues(count
)
74 if not (isinstance(expected
, Exception)):
75 to_compare
= self
.generateTestTuple(expected
)
80 del property_values
[key
]
81 except Exception as e
:
85 if isinstance(expected
, Exception):
86 # expected is exception
87 self
.assertNotEqual(None, captured
)
88 self
.assertEqual(type(expected
).__name
__, type(captured
).__name
__)
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
)
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)
103 # Cases requiring sequence type coercion
104 def test_XIndexContainer_AssignSlice(self
):
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]:
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
)
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
122 # Cases not requiring sequence type coercion
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())
133 # obj[2:2] = (val,) # Insert by slice
135 # Cases not requiring sequence type coercion
136 def test_XIndexContainer_AssignSlice_NoCoercion(self
):
138 doc
= self
.createBlankTextDocument()
139 form
= getDataFormInstance(doc
)
143 doc
.DrawPage
.Forms
[0:0] = (form
,)
146 self
.assertEqual('foo', doc
.DrawPage
.Forms
[0].Name
)
151 # obj[0:3:2] = val1,val2 # Replace by extended slice
153 # Cases requiring sequence type coercion
154 def test_XIndexContainer_AssignExtendedSlice(self
):
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]:
162 for m
in range(assign_max
):
163 assign
= [y
+100 for y
in range(m
)]
164 expected
= list(range(i
))
166 expected
[key
] = assign
167 except Exception as e
:
170 self
.assignValuesTestFixture(i
, key
, assign
, expected
)
173 # del obj[0] # Delete by index
174 def test_XIndexContainer_DelIndex(self
):
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
))
181 except Exception as e
:
183 self
.deleteValuesTestFixture(i
, j
, expected
)
186 # del obj[2:4] # Delete by slice
187 def test_XIndexContainer_DelSlice(self
):
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]:
193 expected
= list(range(i
))
196 except Exception as e
:
198 self
.deleteValuesTestFixture(i
, key
, expected
)
201 if __name__
== '__main__':
204 # vim:set shiftwidth=4 softtabstop=4 expandtab: