Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes
test.test_os.EnvironTests Class Reference
Inheritance diagram for test.test_os.EnvironTests:
Inheritance graph
[legend]
Collaboration diagram for test.test_os.EnvironTests:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def setUp
def tearDown
def test_update2
def test_os_popen_iter
def test_keyvalue_types
def test_items
def test___repr__
def test_get_exec_path
def test_environb
def test_read
def test_write
def test_constructor
def test_bool
def test_keys
def test_values
def test_len
def test_getitem
def test_update
def test_get
def test_setdefault
def test_popitem
def test_pop

Public Attributes

 reference
 other
 inmapping
 d
 i

Static Public Attributes

 type2test = None

Private Member Functions

def _reference
def _empty_mapping

Private Attributes

 __save
 __saveb

Detailed Description

check that os.environ object conform to mapping protocol

Definition at line 299 of file test_os.py.


Member Function Documentation

def test.test_os.EnvironTests._empty_mapping (   self) [private]
Return an empty mapping object

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 320 of file test_os.py.

00320 
00321     def _empty_mapping(self):
00322         os.environ.clear()
00323         return os.environ

def test.test_os.EnvironTests._reference (   self) [private]
Return a dictionary of values which are invariant by storage
in the object under test.

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 317 of file test_os.py.

00317 
00318     def _reference(self):
00319         return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"}

Definition at line 303 of file test_os.py.

00303 
00304     def setUp(self):
00305         self.__save = dict(os.environ)
00306         if os.supports_bytes_environ:
00307             self.__saveb = dict(os.environb)
00308         for key, value in self._reference().items():
00309             os.environ[key] = value

Here is the caller graph for this function:

Definition at line 310 of file test_os.py.

00310 
00311     def tearDown(self):
00312         os.environ.clear()
00313         os.environ.update(self.__save)
00314         if os.supports_bytes_environ:
00315             os.environb.clear()
00316             os.environb.update(self.__saveb)

Here is the caller graph for this function:

Check that the repr() of os.environ looks like environ({...}).

Definition at line 355 of file test_os.py.

00355 
00356     def test___repr__(self):
00357         """Check that the repr() of os.environ looks like environ({...})."""
00358         env = os.environ
00359         self.assertEqual(repr(env), 'environ({{{}}})'.format(', '.join(
00360             '{!r}: {!r}'.format(key, value)
00361             for key, value in env.items())))

Here is the call graph for this function:

Reimplemented in test.mapping_tests.TestMappingProtocol.

Definition at line 135 of file mapping_tests.py.

00135 
00136     def test_bool(self):
00137         self.assertTrue(not self._empty_mapping())
00138         self.assertTrue(self.reference)
00139         self.assertTrue(bool(self._empty_mapping()) is False)
00140         self.assertTrue(bool(self.reference) is True)

Here is the call graph for this function:

Reimplemented in test.mapping_tests.TestMappingProtocol.

Definition at line 132 of file mapping_tests.py.

00132 
00133     def test_constructor(self):
00134         self.assertEqual(self._empty_mapping(), self._empty_mapping())

Here is the call graph for this function:

Definition at line 405 of file test_os.py.

00405 
00406     def test_environb(self):
00407         # os.environ -> os.environb
00408         value = 'euro\u20ac'
00409         try:
00410             value_bytes = value.encode(sys.getfilesystemencoding(),
00411                                        'surrogateescape')
00412         except UnicodeEncodeError:
00413             msg = "U+20AC character is not encodable to %s" % (
00414                 sys.getfilesystemencoding(),)
00415             self.skipTest(msg)
00416         os.environ['unicode'] = value
00417         self.assertEqual(os.environ['unicode'], value)
00418         self.assertEqual(os.environb[b'unicode'], value_bytes)
00419 
00420         # os.environb -> os.environ
00421         value = b'\xff'
00422         os.environb[b'bytes'] = value
00423         self.assertEqual(os.environb[b'bytes'], value)
00424         value_str = value.decode(sys.getfilesystemencoding(), 'surrogateescape')
00425         self.assertEqual(os.environ['bytes'], value_str)

Here is the call graph for this function:

Reimplemented in test.mapping_tests.TestMappingProtocol.

Definition at line 269 of file mapping_tests.py.

00269 
00270     def test_get(self):
00271         d = self._empty_mapping()
00272         self.assertTrue(d.get(list(self.other.keys())[0]) is None)
00273         self.assertEqual(d.get(list(self.other.keys())[0], 3), 3)
00274         d = self.reference
00275         self.assertTrue(d.get(list(self.other.keys())[0]) is None)
00276         self.assertEqual(d.get(list(self.other.keys())[0], 3), 3)
00277         self.assertEqual(d.get(list(self.inmapping.keys())[0]),
00278                          list(self.inmapping.values())[0])
00279         self.assertEqual(d.get(list(self.inmapping.keys())[0], 3),
00280                          list(self.inmapping.values())[0])
00281         self.assertRaises(TypeError, d.get)
00282         self.assertRaises(TypeError, d.get, None, None, None)

Here is the call graph for this function:

Definition at line 362 of file test_os.py.

00362 
00363     def test_get_exec_path(self):
00364         defpath_list = os.defpath.split(os.pathsep)
00365         test_path = ['/monty', '/python', '', '/flying/circus']
00366         test_env = {'PATH': os.pathsep.join(test_path)}
00367 
00368         saved_environ = os.environ
00369         try:
00370             os.environ = dict(test_env)
00371             # Test that defaulting to os.environ works.
00372             self.assertSequenceEqual(test_path, os.get_exec_path())
00373             self.assertSequenceEqual(test_path, os.get_exec_path(env=None))
00374         finally:
00375             os.environ = saved_environ
00376 
00377         # No PATH environment variable
00378         self.assertSequenceEqual(defpath_list, os.get_exec_path({}))
00379         # Empty PATH environment variable
00380         self.assertSequenceEqual(('',), os.get_exec_path({'PATH':''}))
00381         # Supplied PATH environment variable
00382         self.assertSequenceEqual(test_path, os.get_exec_path(test_env))
00383 
00384         if os.supports_bytes_environ:
00385             # env cannot contain 'PATH' and b'PATH' keys
00386             try:
00387                 # ignore BytesWarning warning
00388                 with warnings.catch_warnings(record=True):
00389                     mixed_env = {'PATH': '1', b'PATH': b'2'}
00390             except BytesWarning:
00391                 # mixed_env cannot be created with python -bb
00392                 pass
00393             else:
00394                 self.assertRaises(ValueError, os.get_exec_path, mixed_env)
00395 
00396             # bytes key and/or value
00397             self.assertSequenceEqual(os.get_exec_path({b'PATH': b'abc'}),
00398                 ['abc'])
00399             self.assertSequenceEqual(os.get_exec_path({b'PATH': 'abc'}),
00400                 ['abc'])
00401             self.assertSequenceEqual(os.get_exec_path({'PATH': b'abc'}),
00402                 ['abc'])

Here is the call graph for this function:

Reimplemented in test.mapping_tests.TestHashMappingProtocol, and test.mapping_tests.TestMappingProtocol.

Definition at line 165 of file mapping_tests.py.

00165 
00166     def test_getitem(self):
00167         d = self.reference
00168         self.assertEqual(d[list(self.inmapping.keys())[0]],
00169                          list(self.inmapping.values())[0])
00170 
00171         self.assertRaises(TypeError, d.__getitem__)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 350 of file test_os.py.

00350 
00351     def test_items(self):
00352         for key, value in self._reference().items():
00353             self.assertEqual(os.environ.get(key), value)

Here is the call graph for this function:

Reimplemented in test.mapping_tests.TestMappingProtocol.

Definition at line 141 of file mapping_tests.py.

00141 
00142     def test_keys(self):
00143         d = self._empty_mapping()
00144         self.assertEqual(list(d.keys()), [])
00145         d = self.reference
00146         self.assertIn(list(self.inmapping.keys())[0], d.keys())
00147         self.assertNotIn(list(self.other.keys())[0], d.keys())
00148         self.assertRaises(TypeError, d.keys, None)

Here is the call graph for this function:

Definition at line 345 of file test_os.py.

00345 
00346     def test_keyvalue_types(self):
00347         for key, val in os.environ.items():
00348             self.assertEqual(type(key), str)
00349             self.assertEqual(type(val), str)

Here is the call graph for this function:

Reimplemented in test.mapping_tests.TestMappingProtocol.

Definition at line 161 of file mapping_tests.py.

00161 
00162     def test_len(self):
00163         d = self._empty_mapping()
00164         self.assertEqual(len(d), 0)

Here is the call graph for this function:

Definition at line 333 of file test_os.py.

00333 
00334     def test_os_popen_iter(self):
00335         if os.path.exists("/bin/sh"):
00336             with os.popen(
00337                 "/bin/sh -c 'echo \"line1\nline2\nline3\"'") as popen:
00338                 it = iter(popen)
00339                 self.assertEqual(next(it), "line1\n")
00340                 self.assertEqual(next(it), "line2\n")
00341                 self.assertEqual(next(it), "line3\n")
00342                 self.assertRaises(StopIteration, next, it)

Here is the call graph for this function:

Reimplemented in test.mapping_tests.TestHashMappingProtocol, and test.mapping_tests.TestMappingProtocol.

Definition at line 292 of file mapping_tests.py.

00292 
00293     def test_pop(self):
00294         d = self._empty_mapping()
00295         k, v = list(self.inmapping.items())[0]
00296         d[k] = v
00297         self.assertRaises(KeyError, d.pop, list(self.other.keys())[0])
00298 
00299         self.assertEqual(d.pop(k), v)
00300         self.assertEqual(len(d), 0)
00301 
00302         self.assertRaises(KeyError, d.pop, k)
00303 

Here is the call graph for this function:

Reimplemented in test.test_collections.SubclassMappingTests, test.test_collections.GeneralMappingTests, and test.mapping_tests.TestMappingProtocol.

Definition at line 287 of file mapping_tests.py.

00287 
00288     def test_popitem(self):
00289         d = self._empty_mapping()
00290         self.assertRaises(KeyError, d.popitem)
00291         self.assertRaises(TypeError, d.popitem, 42)

Here is the call graph for this function:

Definition at line 42 of file mapping_tests.py.

00042 
00043     def test_read(self):
00044         # Test for read only operations on mapping
00045         p = self._empty_mapping()
00046         p1 = dict(p) #workaround for singleton objects
00047         d = self._full_mapping(self.reference)
00048         if d is p:
00049             p = p1
00050         #Indexing
00051         for key, value in self.reference.items():
00052             self.assertEqual(d[key], value)
00053         knownkey = list(self.other.keys())[0]
00054         self.assertRaises(KeyError, lambda:d[knownkey])
00055         #len
00056         self.assertEqual(len(p), 0)
00057         self.assertEqual(len(d), len(self.reference))
00058         #__contains__
00059         for k in self.reference:
00060             self.assertIn(k, d)
00061         for k in self.other:
00062             self.assertNotIn(k, d)
00063         #cmp
00064         self.assertEqual(p, p)
00065         self.assertEqual(d, d)
00066         self.assertNotEqual(p, d)
00067         self.assertNotEqual(d, p)
00068         #__non__zero__
00069         if p: self.fail("Empty mapping must compare to False")
00070         if not d: self.fail("Full mapping must compare to True")
00071         # keys(), items(), iterkeys() ...
00072         def check_iterandlist(iter, lst, ref):
00073             self.assertTrue(hasattr(iter, '__next__'))
00074             self.assertTrue(hasattr(iter, '__iter__'))
00075             x = list(iter)
00076             self.assertTrue(set(x)==set(lst)==set(ref))
00077         check_iterandlist(iter(d.keys()), list(d.keys()),
00078                           self.reference.keys())
00079         check_iterandlist(iter(d), list(d.keys()), self.reference.keys())
00080         check_iterandlist(iter(d.values()), list(d.values()),
00081                           self.reference.values())
00082         check_iterandlist(iter(d.items()), list(d.items()),
00083                           self.reference.items())
00084         #get
00085         key, value = next(iter(d.items()))
00086         knownkey, knownvalue = next(iter(self.other.items()))
00087         self.assertEqual(d.get(key, knownvalue), value)
00088         self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
00089         self.assertNotIn(knownkey, d)

Here is the call graph for this function:

Reimplemented in test.mapping_tests.TestHashMappingProtocol, and test.mapping_tests.TestMappingProtocol.

Definition at line 283 of file mapping_tests.py.

00283 
00284     def test_setdefault(self):
00285         d = self._empty_mapping()
00286         self.assertRaises(TypeError, d.setdefault)

Here is the call graph for this function:

Reimplemented in test.mapping_tests.TestMappingProtocol.

Definition at line 172 of file mapping_tests.py.

00172 
00173     def test_update(self):
00174         # mapping argument
00175         d = self._empty_mapping()
00176         d.update(self.other)
00177         self.assertEqual(list(d.items()), list(self.other.items()))
00178 
00179         # No argument
00180         d = self._empty_mapping()
00181         d.update()
00182         self.assertEqual(d, self._empty_mapping())
00183 
00184         # item sequence
00185         d = self._empty_mapping()
00186         d.update(self.other.items())
00187         self.assertEqual(list(d.items()), list(self.other.items()))
00188 
00189         # Iterator
00190         d = self._empty_mapping()
00191         d.update(self.other.items())
00192         self.assertEqual(list(d.items()), list(self.other.items()))
00193 
00194         # FIXME: Doesn't work with UserDict
00195         # self.assertRaises((TypeError, AttributeError), d.update, None)
00196         self.assertRaises((TypeError, AttributeError), d.update, 42)
00197 
00198         outerself = self
00199         class SimpleUserDict:
00200             def __init__(self):
00201                 self.d = outerself.reference
00202             def keys(self):
00203                 return self.d.keys()
00204             def __getitem__(self, i):
00205                 return self.d[i]
00206         d.clear()
00207         d.update(SimpleUserDict())
00208         i1 = sorted(d.items())
00209         i2 = sorted(self.reference.items())
00210         self.assertEqual(i1, i2)
00211 
00212         class Exc(Exception): pass
00213 
00214         d = self._empty_mapping()
00215         class FailingUserDict:
00216             def keys(self):
00217                 raise Exc
00218         self.assertRaises(Exc, d.update, FailingUserDict())
00219 
00220         d.clear()
00221 
00222         class FailingUserDict:
00223             def keys(self):
00224                 class BogonIter:
00225                     def __init__(self):
00226                         self.i = 1
00227                     def __iter__(self):
00228                         return self
00229                     def __next__(self):
00230                         if self.i:
00231                             self.i = 0
00232                             return 'a'
00233                         raise Exc
00234                 return BogonIter()
00235             def __getitem__(self, key):
00236                 return key
00237         self.assertRaises(Exc, d.update, FailingUserDict())
00238 
00239         class FailingUserDict:
00240             def keys(self):
00241                 class BogonIter:
00242                     def __init__(self):
00243                         self.i = ord('a')
00244                     def __iter__(self):
00245                         return self
00246                     def __next__(self):
00247                         if self.i <= ord('z'):
00248                             rtn = chr(self.i)
00249                             self.i += 1
00250                             return rtn
00251                         raise StopIteration
00252                 return BogonIter()
00253             def __getitem__(self, key):
00254                 raise Exc
00255         self.assertRaises(Exc, d.update, FailingUserDict())
00256 
00257         d = self._empty_mapping()
00258         class badseq(object):
00259             def __iter__(self):
00260                 return self
00261             def __next__(self):
00262                 raise Exc()
00263 
00264         self.assertRaises(Exc, d.update, badseq())
00265 
00266         self.assertRaises(ValueError, d.update, [(1, 2, 3)])

Here is the call graph for this function:

Definition at line 325 of file test_os.py.

00325 
00326     def test_update2(self):
00327         os.environ.clear()
00328         if os.path.exists("/bin/sh"):
00329             os.environ.update(HELLO="World")
00330             with os.popen("/bin/sh -c 'echo $HELLO'") as popen:
00331                 value = popen.read().strip()
00332                 self.assertEqual(value, "World")

Here is the call graph for this function:

Reimplemented in test.mapping_tests.TestMappingProtocol.

Definition at line 149 of file mapping_tests.py.

00149 
00150     def test_values(self):
00151         d = self._empty_mapping()
00152         self.assertEqual(list(d.values()), [])
00153 
00154         self.assertRaises(TypeError, d.values, None)

Here is the call graph for this function:

Definition at line 90 of file mapping_tests.py.

00090 
00091     def test_write(self):
00092         # Test for write operations on mapping
00093         p = self._empty_mapping()
00094         #Indexing
00095         for key, value in self.reference.items():
00096             p[key] = value
00097             self.assertEqual(p[key], value)
00098         for key in self.reference.keys():
00099             del p[key]
00100             self.assertRaises(KeyError, lambda:p[key])
00101         p = self._empty_mapping()
00102         #update
00103         p.update(self.reference)
00104         self.assertEqual(dict(p), self.reference)
00105         items = list(p.items())
00106         p = self._empty_mapping()
00107         p.update(items)
00108         self.assertEqual(dict(p), self.reference)
00109         d = self._full_mapping(self.reference)
00110         #setdefault
00111         key, value = next(iter(d.items()))
00112         knownkey, knownvalue = next(iter(self.other.items()))
00113         self.assertEqual(d.setdefault(key, knownvalue), value)
00114         self.assertEqual(d[key], value)
00115         self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue)
00116         self.assertEqual(d[knownkey], knownvalue)
00117         #pop
00118         self.assertEqual(d.pop(knownkey), knownvalue)
00119         self.assertNotIn(knownkey, d)
00120         self.assertRaises(KeyError, d.pop, knownkey)
00121         default = 909
00122         d[knownkey] = knownvalue
00123         self.assertEqual(d.pop(knownkey, default), knownvalue)
00124         self.assertNotIn(knownkey, d)
00125         self.assertEqual(d.pop(knownkey, default), default)
00126         #popitem
00127         key, value = d.popitem()
00128         self.assertNotIn(key, d)
00129         self.assertEqual(value, self.reference[key])
00130         p=self._empty_mapping()
00131         self.assertRaises(KeyError, p.popitem)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 304 of file test_os.py.

Definition at line 306 of file test_os.py.

Reimplemented in test.mapping_tests.TestMappingProtocol.

Definition at line 200 of file mapping_tests.py.

Definition at line 225 of file mapping_tests.py.

Definition at line 39 of file mapping_tests.py.

Definition at line 35 of file mapping_tests.py.

Definition at line 31 of file mapping_tests.py.

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 301 of file test_os.py.


The documentation for this class was generated from the following file: