1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
#!/usr/bin/env python
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
import unittest
import uno
from testcollections_base import CollectionsTestBase
# ContentIndex instance factory
def getContentIndexInstance(doc):
return doc.createInstance("com.sun.star.text.ContentIndex")
# Tests behaviour of objects implementing XIndexReplace using the new-style
# collection accessors
# The objects chosen have no special meaning, they just happen to implement the
# tested interfaces
class TestXIndexReplace(CollectionsTestBase):
def generateTestContentIndex(self, doc):
index = getContentIndexInstance(doc)
for i in range(10):
styles = ('n'+str(i),)
uno.invoke(index.LevelParagraphStyles, "replaceByIndex", (i, uno.Any("[]string", styles)))
return index
def generateTestTuple(self, values):
properties = []
for i in values:
properties.append(('n'+str(i),),)
return tuple(properties)
def assignValuesTestFixture(self, doc, key, values, expected):
# Given
index = self.generateTestContentIndex(doc)
to_assign = self.generateTestTuple(values)
if not (isinstance(expected, Exception)):
toCompare = self.generateTestTuple(expected)
# When
captured = None
try:
index.LevelParagraphStyles[key] = to_assign
except Exception as e:
captured = e
# Then
if isinstance(expected, Exception):
# expected is exception
self.assertNotEqual(None, captured)
self.assertEqual(type(expected).__name__, type(captured).__name__)
else:
# expected is list
self.assertEqual(None, captured)
for i in range(10):
self.assertEqual(toCompare[i][0],
index.LevelParagraphStyles[i][0])
# Tests syntax:
# obj[0] = val # Replace by index
# For:
# Cases requiring sequence type coercion
def test_XIndexReplace_ReplaceIndex(self):
# Given
doc = self.createBlankTextDocument()
index = getContentIndexInstance(doc)
# When
index.LevelParagraphStyles[0] = ('Caption',)
# Then
self.assertEqual(('Caption',), index.LevelParagraphStyles[0])
doc.close(True)
# Tests syntax:
# obj[0] = val # Replace by index
# For:
# Invalid value (None)
def test_XIndexReplace_ReplaceIndex_Invalid_None(self):
# Given
doc = self.createBlankTextDocument()
index = getContentIndexInstance(doc)
# When / Then
with self.assertRaises(TypeError):
index.LevelParagraphStyles[0] = None
doc.close(True)
# Tests syntax:
# obj[0] = val # Replace by index
# For:
# Invalid value (String)
def test_XIndexReplace_ReplaceIndex_Invalid_String(self):
# Given
doc = self.createBlankTextDocument()
index = getContentIndexInstance(doc)
# When / Then
with self.assertRaises(TypeError):
index.LevelParagraphStyles[0] = 'foo'
doc.close(True)
# Tests syntax:
# obj[0] = val # Replace by index
# For:
# Invalid value (Float)
def test_XIndexReplace_ReplaceIndex_Invalid_Float(self):
# Given
doc = self.createBlankTextDocument()
index = getContentIndexInstance(doc)
# When / Then
with self.assertRaises(TypeError):
index.LevelParagraphStyles[0] = 12.34
doc.close(True)
# Tests syntax:
# obj[0] = val # Replace by index
# For:
# Invalid value (List)
def test_XIndexReplace_ReplaceIndex_Invalid_List(self):
# Given
doc = self.createBlankTextDocument()
index = getContentIndexInstance(doc)
# When / Then
with self.assertRaises(TypeError):
index.LevelParagraphStyles[0] = [0, 1]
doc.close(True)
# Tests syntax:
# obj[0] = val # Replace by index
# For:
# Invalid value (Dict)
def test_XIndexReplace_ReplaceIndex_Invalid_Dict(self):
# Given
doc = self.createBlankTextDocument()
index = getContentIndexInstance(doc)
# When / Then
with self.assertRaises(TypeError):
index.LevelParagraphStyles[0] = {'a': 'b'}
doc.close(True)
# Tests syntax:
# obj[0] = val # Replace by index
# For:
# Invalid value (inconsistently typed tuple)
def test_XIndexReplace_ReplaceIndex_Invalid_InconsistentTuple(self):
# Given
doc = self.createBlankTextDocument()
index = getContentIndexInstance(doc)
# When / Then
with self.assertRaises(TypeError):
index.LevelParagraphStyles[0] = ('Caption', ())
doc.close(True)
# Tests syntax:
# obj[2:4] = val1,val2 # Replace by slice
# For:
# Cases requiring sequence type coercion
def test_XIndexReplace_ReplaceSlice(self):
assign_max = 12
doc = self.createBlankTextDocument()
for j in [x for x in range(-12, 13)] + [None]:
for k in [x for x in range(-12, 13)] + [None]:
key = slice(j, k)
for l in range(assign_max):
assign = [y+100 for y in range(l)]
expected = list(range(10))
try:
expected[key] = assign
except Exception as e:
expected = e
if (len(expected) != 10):
expected = ValueError()
self.assignValuesTestFixture(doc, key, assign, expected)
doc.close(True)
# Tests syntax:
# obj[2:4] = val1,val2 # Replace by slice
# For:
# Invalid values (inconsistently value types in tuple)
def test_XIndexReplace_ReplaceSlice_Invalid_InconsistentTuple(self):
# Given
doc = self.createBlankTextDocument()
index = getContentIndexInstance(doc)
# When / Then
with self.assertRaises(TypeError):
index.LevelParagraphStyles[0:2] = (
('Caption',),
12.34
)
doc.close(True)
# Tests syntax:
# obj[0:3:2] = val1,val2 # Replace by extended slice
# For:
# Cases requiring sequence type coercion
def test_XIndexReplace_ReplaceExtendedSlice(self):
assign_max = 12
doc = self.createBlankTextDocument()
for j in [x for x in range(-12, 13)] + [None]:
for k in [x for x in range(-12, 13)] + [None]:
for l in [-2, -1, 2]:
key = slice(j, k, l)
for m in range(assign_max):
assign = [y+100 for y in range(m)]
expected = list(range(10))
try:
expected[key] = assign
except Exception as e:
expected = e
self.assignValuesTestFixture(doc, key, assign, expected)
doc.close(True)
if __name__ == '__main__':
unittest.main()
# vim:set shiftwidth=4 softtabstop=4 expandtab:
|