tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / pyuno / qa / pytests / testcollections_XNameAccess.py
blob155029b9fadf8654b1cf1f1cbf11c5b865d9cff9
1 #!/usr/bin/env python
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/.
10 import unittest
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
18 # tested interfaces
20 class TestXNameAccess(CollectionsTestBase):
22 # Tests syntax:
23 # num = len(obj) # Number of keys
24 # For:
25 # 2 elements
26 def test_XNameAccess_Len(self):
27 # Given
28 drw = self.createBlankDrawing()
30 # When
31 length_categories = len(drw.Links)
32 length_slides = len(drw.Links['Slide'].Links)
33 length_master = len(drw.Links['Master Page'].Links)
35 # Then
36 self.assertEqual(4, length_categories)
37 self.assertEqual(1, length_slides)
38 self.assertEqual(1, length_master)
40 drw.close(True)
42 # Tests syntax:
43 # val = obj[key] # Access by key
44 # For:
45 # 1/2 elements
46 def test_XNameAccess_ReadKey(self):
47 # Given
48 drw = self.createBlankDrawing()
49 drw.DrawPages[0].Name = 'foo'
51 # When
52 link = drw.Links['Slide'].Links['foo']
54 # Then
55 self.assertEqual('foo', link.getName())
57 drw.close(True)
59 # Tests syntax:
60 # val = obj[key] # Access by key
61 # For:
62 # Missing key
63 def test_XNameAccess_ReadKey_Missing(self):
64 # Given
65 drw = self.createBlankDrawing()
67 # When / Then
68 with self.assertRaises(KeyError):
69 _ = drw.Links['Slide'].Links['foo']
71 drw.close(True)
73 # Tests syntax:
74 # val = obj[key] # Access by key
75 # For:
76 # Invalid key type (None)
77 def test_XNameAccess_ReadKey_Invalid_None(self):
78 # Given
79 drw = self.createBlankDrawing()
81 # When / Then
82 with self.assertRaises(TypeError):
83 _ = drw.Links[None]
85 drw.close(True)
87 # Tests syntax:
88 # val = obj[key] # Access by key
89 # For:
90 # Invalid key type (float)
91 def test_XNameAccess_ReadKey_Invalid_Float(self):
92 # Given
93 drw = self.createBlankDrawing()
95 # When / Then
96 with self.assertRaises(TypeError):
97 _ = drw.Links[12.34]
99 drw.close(True)
101 # Tests syntax:
102 # val = obj[key] # Access by key
103 # For:
104 # Invalid key type (tuple)
105 def test_XNameAccess_ReadKey_Invalid_Tuple(self):
106 # Given
107 drw = self.createBlankDrawing()
109 # When / Then
110 with self.assertRaises(TypeError):
111 _ = drw.Links[(1, 2)]
113 drw.close(True)
115 # Tests syntax:
116 # val = obj[key] # Access by key
117 # For:
118 # Invalid key type (list)
119 def test_XNameAccess_ReadKey_Invalid_List(self):
120 # Given
121 drw = self.createBlankDrawing()
123 # When / Then
124 with self.assertRaises(TypeError):
125 _ = drw.Links[[1, 2]]
127 drw.close(True)
129 # Tests syntax:
130 # val = obj[key] # Access by key
131 # For:
132 # Invalid key type (dict)
133 def test_XNameAccess_ReadKey_Invalid_Dict(self):
134 # Given
135 drw = self.createBlankDrawing()
137 # When / Then
138 with self.assertRaises(TypeError):
139 _ = drw.Links[{'a': 'b'}]
141 drw.close(True)
143 # Tests syntax:
144 # if key in obj: ... # Test key presence
145 # For:
146 # 1/2 elements
147 def test_XNameAccess_In(self):
148 # Given
149 drw = self.createBlankDrawing()
150 drw.DrawPages[0].Name = 'foo'
152 # When
153 present = 'foo' in drw.Links['Slide'].Links
155 # Then
156 self.assertTrue(present)
158 drw.close(True)
160 # Tests syntax:
161 # for key in obj: ... # Implicit iterator (keys)
162 # For:
163 # 2 elements
164 def test_XNameAccess_ForIn(self):
165 # Given
166 drw = self.createBlankDrawing()
167 i = 0
168 for name in drw.Links['Slide'].Links.getElementNames():
169 drw.Links['Slide'].Links.getByName(name).Name = 'foo' + str(i)
170 i += 1
172 # When
173 read_links = []
174 for link in drw.Links['Slide'].Links:
175 read_links.append(link)
177 # Then
178 self.assertEqual(['foo0'], read_links)
180 drw.close(True)
182 # Tests syntax:
183 # itr = iter(obj) # Named iterator (keys)
184 # For:
185 # 2 elements
186 def test_XNameAccess_Iter(self):
187 # Given
188 drw = self.createBlankDrawing()
190 # When
191 itr = iter(drw.Links['Slide'].Links)
193 # Then
194 self.assertIsNotNone(next(itr))
195 with self.assertRaises(StopIteration):
196 next(itr)
198 drw.close(True)
201 if __name__ == '__main__':
202 unittest.main()
204 # vim:set shiftwidth=4 softtabstop=4 expandtab: