nss: upgrade to release 3.73
[LibreOffice.git] / pyuno / qa / pytests / testcollections_XNameAccess.py
blob498e17e280652151398307fa74a2c2f47f568891
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
11 import uno
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
20 # tested interfaces
22 class TestXNameAccess(CollectionsTestBase):
24 # Tests syntax:
25 # num = len(obj) # Number of keys
26 # For:
27 # 2 elements
28 def test_XNameAccess_Len(self):
29 # Given
30 drw = self.createBlankDrawing()
32 # When
33 length = len(drw.Links)
35 # Then
36 self.assertEqual(2, length)
38 drw.close(True)
40 # Tests syntax:
41 # val = obj[key] # Access by key
42 # For:
43 # 1/2 elements
44 def test_XNameAccess_ReadKey(self):
45 # Given
46 drw = self.createBlankDrawing()
47 drw.DrawPages[0].Name = 'foo'
49 # When
50 link = drw.Links['foo']
52 # Then
53 self.assertEqual('foo', link.getName())
55 drw.close(True)
57 # Tests syntax:
58 # val = obj[key] # Access by key
59 # For:
60 # Missing key
61 def test_XNameAccess_ReadKey_Missing(self):
62 # Given
63 drw = self.createBlankDrawing()
65 # When / Then
66 with self.assertRaises(KeyError):
67 link = drw.Links['foo']
69 drw.close(True)
71 # Tests syntax:
72 # val = obj[key] # Access by key
73 # For:
74 # Invalid key type (None)
75 def test_XNameAccess_ReadKey_Invalid_None(self):
76 # Given
77 drw = self.createBlankDrawing()
79 # When / Then
80 with self.assertRaises(TypeError):
81 link = drw.Links[None]
83 drw.close(True)
85 # Tests syntax:
86 # val = obj[key] # Access by key
87 # For:
88 # Invalid key type (float)
89 def test_XNameAccess_ReadKey_Invalid_Float(self):
90 # Given
91 drw = self.createBlankDrawing()
93 # When / Then
94 with self.assertRaises(TypeError):
95 link = drw.Links[12.34]
97 drw.close(True)
99 # Tests syntax:
100 # val = obj[key] # Access by key
101 # For:
102 # Invalid key type (tuple)
103 def test_XNameAccess_ReadKey_Invalid_Tuple(self):
104 # Given
105 drw = self.createBlankDrawing()
107 # When / Then
108 with self.assertRaises(TypeError):
109 link = drw.Links[(1, 2)]
111 drw.close(True)
113 # Tests syntax:
114 # val = obj[key] # Access by key
115 # For:
116 # Invalid key type (list)
117 def test_XNameAccess_ReadKey_Invalid_List(self):
118 # Given
119 drw = self.createBlankDrawing()
121 # When / Then
122 with self.assertRaises(TypeError):
123 link = drw.Links[[1, 2]]
125 drw.close(True)
127 # Tests syntax:
128 # val = obj[key] # Access by key
129 # For:
130 # Invalid key type (dict)
131 def test_XNameAccess_ReadKey_Invalid_Dict(self):
132 # Given
133 drw = self.createBlankDrawing()
135 # When / Then
136 with self.assertRaises(TypeError):
137 link = drw.Links[{'a': 'b'}]
139 drw.close(True)
141 # Tests syntax:
142 # if key in obj: ... # Test key presence
143 # For:
144 # 1/2 elements
145 def test_XNameAccess_In(self):
146 # Given
147 drw = self.createBlankDrawing()
148 drw.DrawPages[0].Name = 'foo'
150 # When
151 present = 'foo' in drw.Links
153 # Then
154 self.assertTrue(present)
156 drw.close(True)
158 # Tests syntax:
159 # for key in obj: ... # Implicit iterator (keys)
160 # For:
161 # 2 elements
162 def test_XNameAccess_ForIn(self):
163 # Given
164 drw = self.createBlankDrawing()
165 i = 0
166 for name in drw.Links.getElementNames():
167 drw.Links.getByName(name).Name = 'foo' + str(i)
168 i += 1
170 # When
171 read_links = []
172 for link in drw.Links:
173 read_links.append(link)
175 # Then
176 self.assertEqual(['foo0', 'foo1'], read_links)
178 drw.close(True)
180 # Tests syntax:
181 # itr = iter(obj) # Named iterator (keys)
182 # For:
183 # 2 elements
184 def test_XNameAccess_Iter(self):
185 # Given
186 drw = self.createBlankDrawing()
188 # When
189 itr = iter(drw.Links)
191 # Then
192 self.assertIsNotNone(next(itr))
193 self.assertIsNotNone(next(itr))
194 with self.assertRaises(StopIteration):
195 next(itr)
197 drw.close(True)
200 if __name__ == '__main__':
201 unittest.main()
203 # vim:set shiftwidth=4 softtabstop=4 expandtab: