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 # Tests behaviour of objects implementing XNameAccess using the new-style
18 # collection accessors
19 # The objects chosen have no special meaning, they just happen to implement the
22 class TestXNameAccess(CollectionsTestBase
):
25 # num = len(obj) # Number of keys
28 def test_XNameAccess_Len(self
):
30 drw
= self
.createBlankDrawing()
33 length
= len(drw
.Links
)
36 self
.assertEqual(2, length
)
41 # val = obj[key] # Access by key
44 def test_XNameAccess_ReadKey(self
):
46 drw
= self
.createBlankDrawing()
47 drw
.DrawPages
[0].Name
= 'foo'
50 link
= drw
.Links
['foo']
53 self
.assertEqual('foo', link
.getName())
58 # val = obj[key] # Access by key
61 def test_XNameAccess_ReadKey_Missing(self
):
63 drw
= self
.createBlankDrawing()
66 with self
.assertRaises(KeyError):
67 link
= drw
.Links
['foo']
72 # val = obj[key] # Access by key
74 # Invalid key type (None)
75 def test_XNameAccess_ReadKey_Invalid_None(self
):
77 drw
= self
.createBlankDrawing()
80 with self
.assertRaises(TypeError):
81 link
= drw
.Links
[None]
86 # val = obj[key] # Access by key
88 # Invalid key type (float)
89 def test_XNameAccess_ReadKey_Invalid_Float(self
):
91 drw
= self
.createBlankDrawing()
94 with self
.assertRaises(TypeError):
95 link
= drw
.Links
[12.34]
100 # val = obj[key] # Access by key
102 # Invalid key type (tuple)
103 def test_XNameAccess_ReadKey_Invalid_Tuple(self
):
105 drw
= self
.createBlankDrawing()
108 with self
.assertRaises(TypeError):
109 link
= drw
.Links
[(1, 2)]
114 # val = obj[key] # Access by key
116 # Invalid key type (list)
117 def test_XNameAccess_ReadKey_Invalid_List(self
):
119 drw
= self
.createBlankDrawing()
122 with self
.assertRaises(TypeError):
123 link
= drw
.Links
[[1, 2]]
128 # val = obj[key] # Access by key
130 # Invalid key type (dict)
131 def test_XNameAccess_ReadKey_Invalid_Dict(self
):
133 drw
= self
.createBlankDrawing()
136 with self
.assertRaises(TypeError):
137 link
= drw
.Links
[{'a': 'b'}]
142 # if key in obj: ... # Test key presence
145 def test_XNameAccess_In(self
):
147 drw
= self
.createBlankDrawing()
148 drw
.DrawPages
[0].Name
= 'foo'
151 present
= 'foo' in drw
.Links
154 self
.assertTrue(present
)
159 # for key in obj: ... # Implicit iterator (keys)
162 def test_XNameAccess_ForIn(self
):
164 drw
= self
.createBlankDrawing()
166 for name
in drw
.Links
.getElementNames():
167 drw
.Links
.getByName(name
).Name
= 'foo' + str(i
)
172 for link
in drw
.Links
:
173 read_links
.append(link
)
176 self
.assertEqual(['foo0', 'foo1'], read_links
)
181 # itr = iter(obj) # Named iterator (keys)
184 def test_XNameAccess_Iter(self
):
186 drw
= self
.createBlankDrawing()
189 itr
= iter(drw
.Links
)
192 self
.assertIsNotNone(next(itr
))
193 self
.assertIsNotNone(next(itr
))
194 with self
.assertRaises(StopIteration):
200 if __name__
== '__main__':
203 # vim:set shiftwidth=4 softtabstop=4 expandtab: