Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / pyuno / qa / pytests / testcollections_XNameAccess.py
blob79d66a2e1bc2d87f33431bff12094a06bdc449dd
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 = len(drw.Links)
33 # Then
34 self.assertEqual(2, length)
36 drw.close(True)
38 # Tests syntax:
39 # val = obj[key] # Access by key
40 # For:
41 # 1/2 elements
42 def test_XNameAccess_ReadKey(self):
43 # Given
44 drw = self.createBlankDrawing()
45 drw.DrawPages[0].Name = 'foo'
47 # When
48 link = drw.Links['foo']
50 # Then
51 self.assertEqual('foo', link.getName())
53 drw.close(True)
55 # Tests syntax:
56 # val = obj[key] # Access by key
57 # For:
58 # Missing key
59 def test_XNameAccess_ReadKey_Missing(self):
60 # Given
61 drw = self.createBlankDrawing()
63 # When / Then
64 with self.assertRaises(KeyError):
65 link = drw.Links['foo']
67 drw.close(True)
69 # Tests syntax:
70 # val = obj[key] # Access by key
71 # For:
72 # Invalid key type (None)
73 def test_XNameAccess_ReadKey_Invalid_None(self):
74 # Given
75 drw = self.createBlankDrawing()
77 # When / Then
78 with self.assertRaises(TypeError):
79 link = drw.Links[None]
81 drw.close(True)
83 # Tests syntax:
84 # val = obj[key] # Access by key
85 # For:
86 # Invalid key type (float)
87 def test_XNameAccess_ReadKey_Invalid_Float(self):
88 # Given
89 drw = self.createBlankDrawing()
91 # When / Then
92 with self.assertRaises(TypeError):
93 link = drw.Links[12.34]
95 drw.close(True)
97 # Tests syntax:
98 # val = obj[key] # Access by key
99 # For:
100 # Invalid key type (tuple)
101 def test_XNameAccess_ReadKey_Invalid_Tuple(self):
102 # Given
103 drw = self.createBlankDrawing()
105 # When / Then
106 with self.assertRaises(TypeError):
107 link = drw.Links[(1, 2)]
109 drw.close(True)
111 # Tests syntax:
112 # val = obj[key] # Access by key
113 # For:
114 # Invalid key type (list)
115 def test_XNameAccess_ReadKey_Invalid_List(self):
116 # Given
117 drw = self.createBlankDrawing()
119 # When / Then
120 with self.assertRaises(TypeError):
121 link = drw.Links[[1, 2]]
123 drw.close(True)
125 # Tests syntax:
126 # val = obj[key] # Access by key
127 # For:
128 # Invalid key type (dict)
129 def test_XNameAccess_ReadKey_Invalid_Dict(self):
130 # Given
131 drw = self.createBlankDrawing()
133 # When / Then
134 with self.assertRaises(TypeError):
135 link = drw.Links[{'a': 'b'}]
137 drw.close(True)
139 # Tests syntax:
140 # if key in obj: ... # Test key presence
141 # For:
142 # 1/2 elements
143 def test_XNameAccess_In(self):
144 # Given
145 drw = self.createBlankDrawing()
146 drw.DrawPages[0].Name = 'foo'
148 # When
149 present = 'foo' in drw.Links
151 # Then
152 self.assertTrue(present)
154 drw.close(True)
156 # Tests syntax:
157 # for key in obj: ... # Implicit iterator (keys)
158 # For:
159 # 2 elements
160 def test_XNameAccess_ForIn(self):
161 # Given
162 drw = self.createBlankDrawing()
163 i = 0
164 for name in drw.Links.getElementNames():
165 drw.Links.getByName(name).Name = 'foo' + str(i)
166 i += 1
168 # When
169 read_links = []
170 for link in drw.Links:
171 read_links.append(link)
173 # Then
174 self.assertEqual(['foo0', 'foo1'], read_links)
176 drw.close(True)
178 # Tests syntax:
179 # itr = iter(obj) # Named iterator (keys)
180 # For:
181 # 2 elements
182 def test_XNameAccess_Iter(self):
183 # Given
184 drw = self.createBlankDrawing()
186 # When
187 itr = iter(drw.Links)
189 # Then
190 self.assertIsNotNone(next(itr))
191 self.assertIsNotNone(next(itr))
192 with self.assertRaises(StopIteration):
193 next(itr)
195 drw.close(True)
198 if __name__ == '__main__':
199 unittest.main()
201 # vim:set shiftwidth=4 softtabstop=4 expandtab: