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_categories
= len(drw
.Links
)
32 length_slides
= len(drw
.Links
['Slide'].Links
)
33 length_master
= len(drw
.Links
['Master Page'].Links
)
36 self
.assertEqual(4, length_categories
)
37 self
.assertEqual(1, length_slides
)
38 self
.assertEqual(1, length_master
)
43 # val = obj[key] # Access by key
46 def test_XNameAccess_ReadKey(self
):
48 drw
= self
.createBlankDrawing()
49 drw
.DrawPages
[0].Name
= 'foo'
52 link
= drw
.Links
['Slide'].Links
['foo']
55 self
.assertEqual('foo', link
.getName())
60 # val = obj[key] # Access by key
63 def test_XNameAccess_ReadKey_Missing(self
):
65 drw
= self
.createBlankDrawing()
68 with self
.assertRaises(KeyError):
69 _
= drw
.Links
['Slide'].Links
['foo']
74 # val = obj[key] # Access by key
76 # Invalid key type (None)
77 def test_XNameAccess_ReadKey_Invalid_None(self
):
79 drw
= self
.createBlankDrawing()
82 with self
.assertRaises(TypeError):
88 # val = obj[key] # Access by key
90 # Invalid key type (float)
91 def test_XNameAccess_ReadKey_Invalid_Float(self
):
93 drw
= self
.createBlankDrawing()
96 with self
.assertRaises(TypeError):
102 # val = obj[key] # Access by key
104 # Invalid key type (tuple)
105 def test_XNameAccess_ReadKey_Invalid_Tuple(self
):
107 drw
= self
.createBlankDrawing()
110 with self
.assertRaises(TypeError):
111 _
= drw
.Links
[(1, 2)]
116 # val = obj[key] # Access by key
118 # Invalid key type (list)
119 def test_XNameAccess_ReadKey_Invalid_List(self
):
121 drw
= self
.createBlankDrawing()
124 with self
.assertRaises(TypeError):
125 _
= drw
.Links
[[1, 2]]
130 # val = obj[key] # Access by key
132 # Invalid key type (dict)
133 def test_XNameAccess_ReadKey_Invalid_Dict(self
):
135 drw
= self
.createBlankDrawing()
138 with self
.assertRaises(TypeError):
139 _
= drw
.Links
[{'a': 'b'}]
144 # if key in obj: ... # Test key presence
147 def test_XNameAccess_In(self
):
149 drw
= self
.createBlankDrawing()
150 drw
.DrawPages
[0].Name
= 'foo'
153 present
= 'foo' in drw
.Links
['Slide'].Links
156 self
.assertTrue(present
)
161 # for key in obj: ... # Implicit iterator (keys)
164 def test_XNameAccess_ForIn(self
):
166 drw
= self
.createBlankDrawing()
168 for name
in drw
.Links
['Slide'].Links
.getElementNames():
169 drw
.Links
['Slide'].Links
.getByName(name
).Name
= 'foo' + str(i
)
174 for link
in drw
.Links
['Slide'].Links
:
175 read_links
.append(link
)
178 self
.assertEqual(['foo0'], read_links
)
183 # itr = iter(obj) # Named iterator (keys)
186 def test_XNameAccess_Iter(self
):
188 drw
= self
.createBlankDrawing()
191 itr
= iter(drw
.Links
['Slide'].Links
)
194 self
.assertIsNotNone(next(itr
))
195 with self
.assertRaises(StopIteration):
201 if __name__
== '__main__':
204 # vim:set shiftwidth=4 softtabstop=4 expandtab: