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 testcollections_base
import CollectionsTestBase
15 # Tests behaviour of objects implementing XNameAccess using the new-style
16 # collection accessors
17 # The objects chosen have no special meaning, they just happen to implement the
20 class TestXNameAccess(CollectionsTestBase
):
23 # num = len(obj) # Number of keys
26 def test_XNameAccess_Len(self
):
28 drw
= self
.createBlankDrawing()
31 length
= len(drw
.Links
)
34 self
.assertEqual(2, length
)
39 # val = obj[key] # Access by key
42 def test_XNameAccess_ReadKey(self
):
44 drw
= self
.createBlankDrawing()
45 drw
.DrawPages
[0].Name
= 'foo'
48 link
= drw
.Links
['foo']
51 self
.assertEqual('foo', link
.getName())
56 # val = obj[key] # Access by key
59 def test_XNameAccess_ReadKey_Missing(self
):
61 drw
= self
.createBlankDrawing()
64 with self
.assertRaises(KeyError):
65 link
= drw
.Links
['foo']
70 # val = obj[key] # Access by key
72 # Invalid key type (None)
73 def test_XNameAccess_ReadKey_Invalid_None(self
):
75 drw
= self
.createBlankDrawing()
78 with self
.assertRaises(TypeError):
79 link
= drw
.Links
[None]
84 # val = obj[key] # Access by key
86 # Invalid key type (float)
87 def test_XNameAccess_ReadKey_Invalid_Float(self
):
89 drw
= self
.createBlankDrawing()
92 with self
.assertRaises(TypeError):
93 link
= drw
.Links
[12.34]
98 # val = obj[key] # Access by key
100 # Invalid key type (tuple)
101 def test_XNameAccess_ReadKey_Invalid_Tuple(self
):
103 drw
= self
.createBlankDrawing()
106 with self
.assertRaises(TypeError):
107 link
= drw
.Links
[(1, 2)]
112 # val = obj[key] # Access by key
114 # Invalid key type (list)
115 def test_XNameAccess_ReadKey_Invalid_List(self
):
117 drw
= self
.createBlankDrawing()
120 with self
.assertRaises(TypeError):
121 link
= drw
.Links
[[1, 2]]
126 # val = obj[key] # Access by key
128 # Invalid key type (dict)
129 def test_XNameAccess_ReadKey_Invalid_Dict(self
):
131 drw
= self
.createBlankDrawing()
134 with self
.assertRaises(TypeError):
135 link
= drw
.Links
[{'a': 'b'}]
140 # if key in obj: ... # Test key presence
143 def test_XNameAccess_In(self
):
145 drw
= self
.createBlankDrawing()
146 drw
.DrawPages
[0].Name
= 'foo'
149 present
= 'foo' in drw
.Links
152 self
.assertTrue(present
)
157 # for key in obj: ... # Implicit iterator (keys)
160 def test_XNameAccess_ForIn(self
):
162 drw
= self
.createBlankDrawing()
164 for name
in drw
.Links
.getElementNames():
165 drw
.Links
.getByName(name
).Name
= 'foo' + str(i
)
170 for link
in drw
.Links
:
171 read_links
.append(link
)
174 self
.assertEqual(['foo0', 'foo1'], read_links
)
179 # itr = iter(obj) # Named iterator (keys)
182 def test_XNameAccess_Iter(self
):
184 drw
= self
.createBlankDrawing()
187 itr
= iter(drw
.Links
)
190 self
.assertIsNotNone(next(itr
))
191 self
.assertIsNotNone(next(itr
))
192 with self
.assertRaises(StopIteration):
198 if __name__
== '__main__':
201 # vim:set shiftwidth=4 softtabstop=4 expandtab: